RCO presents 日本橋ハーフマラソン 本戦 (オープン)

Submission #1175885

Source codeソースコード

#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <numeric>
#include <cctype>
#include <bitset>
#include <cassert>
#include <random>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define df(x) int x = in()
#define dame { puts("-1"); return 0;}
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,vector<T>,greater<T> >
#define bn(x) ((1<<x)-1)
#define newline puts("")
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() { int x; scanf("%d",&x); return x;}
inline void priv(vi a) { rep(i,sz(a)) printf("%d%c",a[i],i==sz(a)-1?'\n':' ');}
template<typename T>istream& operator>>(istream&i,vector<T>&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const vector<T>&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,const vector<T>&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,const pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
const int MX = 100005, INF = 1001001001;
const ll LINF = 1e18;
const double eps = 1e-10;
typedef vector<string> vs;
const int di[] = {-1,0,1,0,0}, dj[] = {0,-1,0,1,0}; //^<v>
const int gi[] = {-1,-1,1,1}, gj[] = {-1,1,-1,1};
const string dc = "ULDR-";
const int MH = 30;

int h, w, n;
struct V {
  int i, j;
  V(int i = 0, int j = 0):i(i),j(j){}
  bool operator==(const V& a)const{ return i==a.i&&j==a.j;}
  bool operator<(const V& a)const{ return i==a.i?j<a.j:i<a.i;}
  void add(int v) {
    i += di[v];
    j += dj[v];
  }
};
int HX, WX;
int dist(V& a, V& b) {
  int dx = abs(a.i-b.i), dy = abs(a.j-b.j);
  return dx*dx*dx+dy*dy*dy;
  // return abs(a.i-b.i)+abs(a.j-b.j);
}
typedef vector<V> vv;
vv s, t;
V inV() {
  int i,j;
  scanf("%d%d",&i,&j);
  return V(i-1,j-1);
}
vvi o;
int& ov(V& v) { return o[v.i][v.j];}
vi box;
void boxr() { random_shuffle(rng(box));}
void prii(vvi& data) { for (vi nd : data) cout<<nd<<endl;}

vs checker(vv& p, int rv) {
  vs res;
  while (1) {
    string now(n,'-');
    auto mv = [&](int k, int v) {
      ov(p[k]) = INF/2;
      now[k] = dc[v^rv];
      p[k].add(v);
      ov(p[k]) = n+k;
    };
    o = vvi(h,vi(w,INF));
    rep(k,n) ov(p[k]) = k;
    {//
      vi c(MH,-MH/2);
      rep(k,n) c[p[k].i]++;
      rep(i,h-1) c[i+1] += c[i];
      rep(i,h-1) {
        if (c[i] == 0) continue;
        if (c[i] < 0) {
          boxr();
          for (int j : box) {
            if (o[i+1][j] < n && o[i][j] == INF) {
              int k = o[i+1][j];
              mv(k,0);
              c[i]++;
              if (!c[i]) break;
            }
          }
        } else {
          boxr();
          for (int j : box) {
            if (o[i][j] < n && o[i+1][j] == INF) {
              int k = o[i][j];
              mv(k,2);
              c[i]--;
              if (!c[i]) break;
            }
          }
        }
      }
    }
    rep(i,h) {//
      vi c(MH);
      rep(j,h) {
        c[j] = (o[i][j]<INF/2)-((i^j)&1);
      }
      rep(j,w-1) c[j+1] += c[j];
      rep(j,w-1) {
        if (!c[j]) continue;
        if (c[j] < 0) {
          if (o[i][j+1] < n && o[i][j] == INF) {
            int k = o[i][j+1];
            mv(k,1);
          }
        } else {
          if (o[i][j] < n && o[i][j+1] == INF) {
            int k = o[i][j];
            mv(k,3);
          }
        }
      }
    }
    if (now == string(n,'-')) break;
    res.pb(now);
  }
  if (rv) reverse(rng(res));
  return res;
}


vs solve(vv s, vv t, bool rndv=false) {
  vs res;
  box = vi(4); iota(rng(box),0);
  rep(ti,500) {
    bool end = true;
    rep(i,n) if (!(s[i]==t[i])) end = false;
    if (end) break;
    string now(n,'-');
    string now2 = now;
    o = vvi(h,vi(w,INF));
    rep(k,n) ov(s[k]) = k;
    vi nd(n);
    vp ps;
    rep(k,n) {
      int i = t[k].i, j = t[k].j;
      int wi = min(i,h-1-i), wj = min(j,w-1-j);
      nd[k] = min(wi,wj)*1000000+(wi+wj)*10000+i*101+j*100;
      ps.pb(P(nd[k],k));
    }
    sort(rng(ps));
    for (P p : ps) {
      int k = p.se;
      int i = s[k].i, j = s[k].j;
      if (o[i][j] == -1) continue;
      if (rndv) boxr();
      for (int gv : box) {
        int mi = i+gi[gv];
        int mj = j+gj[gv];
        if (mi<0||mj<0||mi>=h||mj>=w) continue;
        int u = o[mi][mj];
        if (u == -1) continue;
        if (nd[u] < nd[k]) continue;
        if (dist(s[k],t[k]) <= dist(s[u],t[k])) continue;
        bool ok = true;
        vi id;
        rep(ei,2)rep(ej,2) {
          int no = o[i+ei*gi[gv]][j+ej*gj[gv]];
          if (no == -1) ok = false;
          else if (no < n) id.pb(no);
        }
        if (!ok) continue;
        if (sz(id) != 2) continue;
        int a = id[0], b = id[1];
        rep(ei,2)rep(ej,2) {
          o[i+ei*gi[gv]][j+ej*gj[gv]] = -1;
        }
        now[a] = 'U';
        now[b] = 'D';
        if (s[a].i < s[b].i) swap(now[a],now[b]);
        now2[a] = 'L';
        now2[b] = 'R';
        if (s[a].j < s[b].j) swap(now2[a],now2[b]);
        swap(s[a],s[b]);
        break;
      }
    }
    if (now == string(n,'-')) continue;
    res.pb(now);
    res.pb(now2);
  }
  return res;
}

int main() {
  scanf("%d%d%d%*d",&h,&w,&n);
  s = t = vv(n);
  rep(i,n) {
    s[i] = inV();
    t[i] = inV();
  }
  box = vi(h); iota(rng(box),0);
  vs a1 = checker(s,0);
  vs a3 = checker(t,2);

  vs a2 = solve(s,t);
  rep(ti,100) {
    vs na = solve(s,t,true);
    if (sz(na) < sz(a2)) {
      a2 = na;
    }
  }

  vs ans(a1);
  ans.insert(ans.end(),rng(a2));
  ans.insert(ans.end(),rng(a3));
  cout<<sz(ans)<<endl;
  for (string ns : ans) cout<<ns<<endl;
  return 0;
}





Submission

Task問題 B - 日本橋大渋滞
User nameユーザ名 ຣസںƙᘓ
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 AC
Score得点 1251462
Source lengthソースコード長 6848 Byte
File nameファイル名
Exec time実行時間 809 ms
Memory usageメモリ使用量 640 KB

Compiler messageコンパイルメッセージ

./Main.cpp: In function ‘V inV()’:
./Main.cpp:91:22: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&i,&j);
^
./Main.cpp: In function ‘int main()’:
./Main.cpp:234:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%*d",&h,&w,&n);
^

Test case

Set

Set name Score得点 / Max score Cases
test_01 41807 / 50000 subtask_01_01.txt
test_02 41807 / 50000 subtask_01_02.txt
test_03 41737 / 50000 subtask_01_03.txt
test_04 41737 / 50000 subtask_01_04.txt
test_05 41982 / 50000 subtask_01_05.txt
test_06 41737 / 50000 subtask_01_06.txt
test_07 41667 / 50000 subtask_01_07.txt
test_08 41912 / 50000 subtask_01_08.txt
test_09 41702 / 50000 subtask_01_09.txt
test_10 41598 / 50000 subtask_01_10.txt
test_11 42017 / 50000 subtask_01_11.txt
test_12 41912 / 50000 subtask_01_12.txt
test_13 41563 / 50000 subtask_01_13.txt
test_14 41702 / 50000 subtask_01_14.txt
test_15 41529 / 50000 subtask_01_15.txt
test_16 42053 / 50000 subtask_01_16.txt
test_17 41632 / 50000 subtask_01_17.txt
test_18 41632 / 50000 subtask_01_18.txt
test_19 41667 / 50000 subtask_01_19.txt
test_20 41737 / 50000 subtask_01_20.txt
test_21 41667 / 50000 subtask_01_21.txt
test_22 41772 / 50000 subtask_01_22.txt
test_23 41737 / 50000 subtask_01_23.txt
test_24 41357 / 50000 subtask_01_24.txt
test_25 41529 / 50000 subtask_01_25.txt
test_26 41598 / 50000 subtask_01_26.txt
test_27 41737 / 50000 subtask_01_27.txt
test_28 41460 / 50000 subtask_01_28.txt
test_29 41912 / 50000 subtask_01_29.txt
test_30 41563 / 50000 subtask_01_30.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
subtask_01_01.txt AC 776 ms 512 KB
subtask_01_02.txt AC 768 ms 512 KB
subtask_01_03.txt AC 768 ms 512 KB
subtask_01_04.txt AC 752 ms 512 KB
subtask_01_05.txt AC 757 ms 512 KB
subtask_01_06.txt AC 776 ms 512 KB
subtask_01_07.txt AC 781 ms 512 KB
subtask_01_08.txt AC 754 ms 512 KB
subtask_01_09.txt AC 802 ms 512 KB
subtask_01_10.txt AC 791 ms 512 KB
subtask_01_11.txt AC 754 ms 512 KB
subtask_01_12.txt AC 762 ms 512 KB
subtask_01_13.txt AC 786 ms 512 KB
subtask_01_14.txt AC 773 ms 512 KB
subtask_01_15.txt AC 787 ms 640 KB
subtask_01_16.txt AC 743 ms 512 KB
subtask_01_17.txt AC 782 ms 512 KB
subtask_01_18.txt AC 797 ms 512 KB
subtask_01_19.txt AC 785 ms 512 KB
subtask_01_20.txt AC 793 ms 512 KB
subtask_01_21.txt AC 768 ms 512 KB
subtask_01_22.txt AC 777 ms 512 KB
subtask_01_23.txt AC 783 ms 512 KB
subtask_01_24.txt AC 804 ms 640 KB
subtask_01_25.txt AC 795 ms 640 KB
subtask_01_26.txt AC 786 ms 512 KB
subtask_01_27.txt AC 773 ms 512 KB
subtask_01_28.txt AC 809 ms 640 KB
subtask_01_29.txt AC 785 ms 512 KB
subtask_01_30.txt AC 776 ms 512 KB