Submission #1174912
Source Code Expand
#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;
// // Max flow
// // !! Be care of double and INF !!
// struct Maxflow {
// typedef int TT;
// int n;
// vi to, next, head, dist, it;
// vector<TT> lim;
// Maxflow(){}
// Maxflow(int n):n(n),head(n,-1),it(n){}
// void add(int a, int b, TT c=1) {
// next.pb(head[a]); head[a] = sz(to); to.pb(b); lim.pb(c);
// next.pb(head[b]); head[b] = sz(to); to.pb(a); lim.pb(0);
// }
// // void add2(int a, int b, int c=1) {
// // next.pb(head[a]); head[a] = sz(to); to.pb(b); lim.pb(c);
// // next.pb(head[b]); head[b] = sz(to); to.pb(a); lim.pb(c);
// // }
// void bfs(int sv) {
// dist = vi(n,INF); // INF !!
// queue<int> q;
// dist[sv] = 0; q.push(sv);
// while (!q.empty()){
// int v = q.front(); q.pop();
// for (int i = head[v]; i != -1; i = next[i]) {
// if (lim[i] && dist[to[i]] == INF) { // double INF !!
// dist[to[i]] = dist[v]+1; q.push(to[i]);
// }
// }
// }
// }
// TT dfs(int v, int tv, TT nf=INF) { // INF !!
// if (v == tv) return nf;
// for (; it[v] != -1; it[v] = next[it[v]]) {
// int u = to[it[v]]; TT f;
// if (!lim[it[v]] || dist[v] >= dist[u]) continue;
// if (f = dfs(u, tv, min(nf, lim[it[v]])), f) { // double !!
// lim[it[v]] -= f;
// lim[it[v]^1] += f;
// return f;
// }
// }
// return 0;
// }
// int solve(int sv, int tv) {
// TT flow = 0, f;
// while (1) {
// bfs(sv);
// if (dist[tv] == INF) return flow; // INF !!
// rep(i,n) it[i] = head[i];
// while (f = dfs(sv,tv), f) flow += f;
// }
// }
// };
// //
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() {
vs res;
box = vi((h-1)*(w-1)); iota(rng(box),0);
// while (1) {
rep(ti,1000) {
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] = (wi+wj)*1000000+i*10001+j*10000;
ps.pb(P(nd[k],k));
// if (nd[k]) 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;
rep(gv,4) {
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 (nd[k] <= dist(s[k],t[u])) 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;
}
}
// cout<<now<<endl;
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 a3 = checker(t,2);
// {
// FILE* fp = fopen("int.txt", "w");
// fprintf(fp,"%d %d %d %d\n",h,w,n,10000);
// rep(i,n) {
// fprintf(fp,"%d %d ",s[i].i+1,s[i].j+1);
// fprintf(fp,"%d %d\n",t[i].i+1,t[i].j+1);
// }
// fclose(fp);
// }
vs a1 = checker(s,0);
vs a3 = checker(t,2);
vs a2 = solve();
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;
}
// vs solve() {
// vs res;
// box = vi((h-1)*(w-1)); iota(rng(box),0);
// auto f = [&](vi& id) {
// int sum = 0;
// rep(i,2) sum += dist(s[id[i]],t[id[i]])*(i+1);
// return sum;
// };
// while (1) {
// 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;
// boxr();
// for (int xp : box) {
// int i = xp/(w-1), j = xp%(w-1);
// bool ok = true;
// vi id;
// rep(ei,2)rep(ej,2) {
// int no = o[i+ei][j+ej];
// 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];
// if (t[b] < t[a]) id = {b,a};
// int co = f(id);
// swap(s[a],s[b]);
// int nco = f(id);
// swap(s[a],s[b]);
// if (nco <= co) {
// rep(ei,2)rep(ej,2) {
// o[i+ei][j+ej] = -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]);
// }
// }
// cout<<now<<endl;
// if (now == string(n,'-')) continue;
// res.pb(now);
// res.pb(now2);
// }
// return res;
// }
// vs solve() {
// vs res;
// int sv = h*w, tv = sv+1;
// rep(tti,100) {
// bool end = true;
// rep(k,n) end &= (s[k] == t[k]);
// // rep(k,n) if (s[k] == t[k]); else cerr<<k<<endl;
// if (end) break;
// rep(ti,2) {
// Maxflow g(tv+1);
// rep(i,h)rep(j,w) {
// if (((i+j)&1) != ti) {
// g.add(sv,i*w+j);
// } else {
// g.add(i*w+j,tv);
// }
// }
// for (int cs = -1; cs <= 1; ++cs) {
// rep(k,n) {
// int i = s[k].i, j = s[k].j;
// int nv = i*w+j;
// rep(v,4) {
// int ni = i+di[v], nj = j+dj[v];
// if (ni<0||nj<0||ni>=h||nj>=w) continue;
// V ns(ni,nj);
// if (dist(ns,t[k])-dist(s[k],t[k]) != cs) continue;
// int nu = ni*w+nj;
// g.add(nv,nu);
// }
// }
// g.solve(sv,tv);
// }
// string now(n,'-');
// rep(k,n) {
// int i = s[k].i, j = s[k].j;
// int nv = i*w+j;
// for (int ei = g.head[nv]; ei != -1; ei = g.next[ei]) {
// if (ei&1) continue;
// if (g.lim[ei] == 0) {
// int ni = g.to[ei]/w, nj = g.to[ei]%w;
// rep(v,4) {
// if (i+di[v] == ni && j+dj[v] == nj) {
// s[k].add(v);
// now[k] = dc[v];
// break;
// }
// }
// break;
// }
// }
// }
// // cout<<now<<endl;
// res.pb(now);
// }
// }
// return res;
// }
Submission Info
Submission Time
2017-03-21 01:15:40+0900
Task
B - 日本橋大渋滞
User
snuke
Language
C++14 (GCC 5.4.1)
Score
1221102
Code Size
11963 Byte
Status
AC
Exec Time
7 ms
Memory
512 KB
Compile Error
./Main.cpp: In function ‘V inV()’:
./Main.cpp:147: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:293: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);
^
Judge Result
Set Name
test_01
test_02
test_03
test_04
test_05
test_06
test_07
test_08
test_09
test_10
test_11
test_12
test_13
test_14
test_15
test_16
test_17
test_18
test_19
test_20
test_21
test_22
test_23
test_24
test_25
test_26
test_27
test_28
test_29
test_30
Score / Max Score
41323 / 50000
40323 / 50000
40519 / 50000
40984 / 50000
40552 / 50000
40850 / 50000
41460 / 50000
40951 / 50000
41018 / 50000
38941 / 50000
40784 / 50000
40421 / 50000
39278 / 50000
40884 / 50000
41051 / 50000
41702 / 50000
41018 / 50000
40817 / 50000
41323 / 50000
40193 / 50000
40850 / 50000
41221 / 50000
40717 / 50000
39589 / 50000
40784 / 50000
39002 / 50000
41255 / 50000
40850 / 50000
41357 / 50000
41085 / 50000
Status
Set Name
Test Cases
test_01
subtask_01_01.txt
test_02
subtask_01_02.txt
test_03
subtask_01_03.txt
test_04
subtask_01_04.txt
test_05
subtask_01_05.txt
test_06
subtask_01_06.txt
test_07
subtask_01_07.txt
test_08
subtask_01_08.txt
test_09
subtask_01_09.txt
test_10
subtask_01_10.txt
test_11
subtask_01_11.txt
test_12
subtask_01_12.txt
test_13
subtask_01_13.txt
test_14
subtask_01_14.txt
test_15
subtask_01_15.txt
test_16
subtask_01_16.txt
test_17
subtask_01_17.txt
test_18
subtask_01_18.txt
test_19
subtask_01_19.txt
test_20
subtask_01_20.txt
test_21
subtask_01_21.txt
test_22
subtask_01_22.txt
test_23
subtask_01_23.txt
test_24
subtask_01_24.txt
test_25
subtask_01_25.txt
test_26
subtask_01_26.txt
test_27
subtask_01_27.txt
test_28
subtask_01_28.txt
test_29
subtask_01_29.txt
test_30
subtask_01_30.txt
Case Name
Status
Exec Time
Memory
subtask_01_01.txt
AC
6 ms
512 KB
subtask_01_02.txt
AC
7 ms
512 KB
subtask_01_03.txt
AC
7 ms
512 KB
subtask_01_04.txt
AC
6 ms
512 KB
subtask_01_05.txt
AC
7 ms
512 KB
subtask_01_06.txt
AC
6 ms
512 KB
subtask_01_07.txt
AC
6 ms
512 KB
subtask_01_08.txt
AC
6 ms
512 KB
subtask_01_09.txt
AC
6 ms
512 KB
subtask_01_10.txt
AC
7 ms
512 KB
subtask_01_11.txt
AC
7 ms
512 KB
subtask_01_12.txt
AC
7 ms
512 KB
subtask_01_13.txt
AC
7 ms
512 KB
subtask_01_14.txt
AC
6 ms
512 KB
subtask_01_15.txt
AC
6 ms
512 KB
subtask_01_16.txt
AC
6 ms
512 KB
subtask_01_17.txt
AC
6 ms
512 KB
subtask_01_18.txt
AC
6 ms
512 KB
subtask_01_19.txt
AC
6 ms
512 KB
subtask_01_20.txt
AC
7 ms
512 KB
subtask_01_21.txt
AC
6 ms
512 KB
subtask_01_22.txt
AC
6 ms
512 KB
subtask_01_23.txt
AC
7 ms
512 KB
subtask_01_24.txt
AC
7 ms
512 KB
subtask_01_25.txt
AC
6 ms
512 KB
subtask_01_26.txt
AC
7 ms
512 KB
subtask_01_27.txt
AC
6 ms
512 KB
subtask_01_28.txt
AC
6 ms
512 KB
subtask_01_29.txt
AC
6 ms
512 KB
subtask_01_30.txt
AC
6 ms
512 KB