제출 #587938

#제출 시각아이디문제언어결과실행 시간메모리
587938LastRoninRail (IOI14_rail)C++14
100 / 100
114 ms39568 KiB
#include "rail.h"
#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define ll long long
using namespace std;
/*
typedef struct Station {
  int index;
  int type;
  int location;
  int L,R;
}STATION;
long long cnt;
static int S,SUBTASK;
static STATION stations[10004];
 
int cmp_fun_1(const void *a,const void *b)
{
	STATION c,d;
	c = *(STATION*)(a);
	d = *(STATION*)(b);
  	return c.location < d.location ? -1 : 1;
}
 
int cmp_fun_2(const void *a,const void *b)
{
	STATION c,d;
	c = *(STATION*)(a);
	d = *(STATION*)(b);
  	return c.index < d.index ? -1 : 1;
}
 
void now_I_want_to_getLR(){
  int now = stations[S-1].index,i;
  for(i=S-2;i>=0;i--){
    stations[i].R = now;
    if(stations[i].type==2)	now = stations[i].index;
  }
  now = stations[0].index;
  for(i=1;i<S;i++){
    stations[i].L = now;
    if(stations[i].type==1)	now = stations[i].index;
  }
}
 
int getDistance(int x,int y)
{
  cnt++;
  if(x==y)	return 0;
  if(x<0 || x>=S || y<0 || y>=S)    return -1;
  if(stations[x].location > stations[y].location){
  	int tmp = x;
	x = y;
	y = tmp;
  }
  int ret = 0;
  if(stations[x].type==1 && stations[y].type==1){
    ret = stations[stations[y].R].location-stations[x].location+stations[stations[y].R].location-stations[y].location;
  }else if(stations[x].type==1 && stations[y].type==2){
    ret = stations[y].location-stations[x].location;
  }else if(stations[x].type==2 && stations[y].type==2){
    ret = stations[x].location-stations[stations[x].L].location+stations[y].location-stations[stations[x].L].location;
  }else if(stations[x].type==2 && stations[y].type==1){
    ret = stations[x].location-stations[stations[x].L].location+stations[stations[y].R].location
      -stations[stations[x].L].location+stations[stations[y].R].location-stations[y].location;
  }
  return ret;
}
 
 
void getInput()
{
  int g;
  g = scanf("%d",&SUBTASK);
  g = scanf("%d",&S);
  int s;
  for (s = 0; s < S; s++) {
    int type, location;
    g = scanf(" %d %d",&type,&location);
    stations[s].index = s;
    stations[s].location = location;
    stations[s].type = type;
    stations[s].L = -1;
    stations[s].R = -1;
  }
  qsort(stations, S, sizeof(STATION), cmp_fun_1);
  now_I_want_to_getLR();
  qsort(stations, S, sizeof(STATION), cmp_fun_2);
}
 
int serverGetStationNumber()
{
  return S;
}
 
int serverGetSubtaskNumber()
{
  return SUBTASK;
}
 
int serverGetFirstStationLocation()
{
  return stations[0].location;
}
/**/
 
const int N = 5e3 + 100;
 
int cont = 0;
bool le[N];
int dist[N][N];
bool was[N][N];
 
int get(int a, int b) {
	if(was[a][b])return dist[a][b];
	cont++;
	was[a][b] = 1;
	dist[a][b] = getDistance(a, b);
	return dist[a][b];
}
 
void findLocation(int n, int first, int location[], int stype[]) {
	location[0] = first;
	stype[0] = 1;
	vector<pair<int, int>> z;
	for(int i = 1; i < n; i++) {
		dist[0][i] = get(0, i);
		was[0][i] = 1;
		z.pb(mp(dist[0][i], i));		
	}
	sort(z.begin(), z.end());
	ll L = first, Lid = 0;
	ll R = first + z[0].f, Rid = z[0].s;
	location[Rid] = R;
	stype[Rid] = 2;
	vector<int> C, D;
	C.pb(Lid);
	D.pb(Rid);	
	for(int j = 1; j < n - 1; j++) {
		for(auto u : C) {
			if(location[u] < L)
				L = location[u], Lid = u;
		}
		for(auto u : D) {
			if(location[u] > R)
				R = location[u], Rid = u;
		}
		if(Lid == 0) {       
			ll a = z[j].f;
			ll b = get(Rid, z[j].s);
			ll pos = R - b;
			ll pos1 = R - b;// type C
			ll pos2 = L + a;// type D
			ll ind = -1, bliz = 1e9;
			for(int j = 0; j < D.size(); j++) {
				if(location[D[j]] > pos1) {
					if(bliz > location[D[j]]) {
						bliz = location[D[j]];
						ind = D[j];
					}
				}
			}
			//cout << ind << " kek " << z[j].s << '\n';
			if(ind == Rid) {
				if((R - L) + b == a) {
					location[z[j].s] = pos1;
					stype[z[j].s] = 1;
					C.pb(z[j].s);
					if(pos1 < L)
						L = pos1, Lid = z[j].s;
				} else {
					location[z[j].s] = pos2;
					stype[z[j].s] = 2;
					D.pb(z[j].s);
					if(pos2 > R)
						R = pos2, Rid = z[j].s;
				}       
			}
			else {
				assert(ind != -1);
				ll di = get(ind, z[j].s);
				if(di + dist[0][ind] == dist[0][z[j].s]) {
					location[z[j].s] = pos1;
					stype[z[j].s] = 1;
					C.pb(z[j].s);
					if(pos1 < L)
						L = pos1, Lid = z[j].s;
				} else {
					location[z[j].s] = pos2;
					stype[z[j].s] = 2;
					D.pb(z[j].s);
					if(pos2 > R)
						R = pos2, Rid = z[j].s;
				}       
			}
		} else {
			ll d1 = get(Lid, z[j].s);
			ll d2 = get(Rid, z[j].s);
			ll pos1 = L + d1;// type D
			ll pos2 = R - d2;// type C
			bool c = 0;
			if(pos1 < R) {
				for(auto u : C) {
					if(location[u] < pos1 && (pos1 - location[u]) + (R - location[u]) == d2)
						c = 1;				
				}
				if(c) {
					location[z[j].s] = pos1;
					stype[z[j].s] = 2;
					D.pb(z[j].s);
					continue;
				} else {
				 	location[z[j].s] = pos2;
				 	stype[z[j].s] = 1;
				 	C.pb(z[j].s);
				 	continue;				
				}
			}
			if(pos2 > L) {
				for(auto u : D) {
					if(location[u] > pos2 && (location[u] - pos2) + (location[u] - L) == d1) 
						c = 1;
				}
				if(c) {
				 	location[z[j].s] = pos2;
				 	stype[z[j].s] = 1;
				 	C.pb(z[j].s);
				 	continue;
				}  else {
					location[z[j].s] = pos1;
					stype[z[j].s] = 2;
					D.pb(z[j].s);
					continue;
				}
			}
			
			if(location[0] - L + dist[0][z[j].s] == d1) {
				
				if(d1 - (location[z[0].s] - L) + (R - location[z[0].s]) != d2) {
					location[z[j].s] = pos1;
					stype[z[j].s] = 2;
					D.pb(z[j].s);					
				} else {
					location[z[j].s] = pos2;
					stype[z[j].s] = 1;
					C.pb(z[j].s);
				}
			} else {
				location[z[j].s] = pos2;
				stype[z[j].s] = 1;
				C.pb(z[j].s);
				continue;				
			}
 
		}
   	}
	assert(cont <= n * (n - 1)/2);
}
/*
int main()
{
  int i;
  getInput();
  cnt = 0;
  
  int location[10005];
  int type[10005];
  int ok = 1;
  findLocation(S, serverGetFirstStationLocation(),location, type);
  if(SUBTASK==3 && cnt>S*(S-1))	ok = 0;
  if(SUBTASK==4 && cnt>3*(S-1))	ok = 0;
  
  
  for (i = 0; i < S; i++)
    if(type[i]!=stations[i].type || location[i]!=stations[i].location)
      ok = 0;
  if(ok==0)	printf("Incorrect");
  else	printf("Correct");
  return 0;
}
/*
3
5
1 2 
1 0
2 3
2 4
2 5 
 
 
*/

컴파일 시 표준 에러 (stderr) 메시지

rail.cpp:108:1: warning: "/*" within comment [-Wcomment]
  108 | /**/
      |  
rail.cpp:284:1: warning: "/*" within comment [-Wcomment]
  284 | /*
      |  
rail.cpp: In function 'void findLocation(int, int, int*, int*)':
rail.cpp:158:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |    for(int j = 0; j < D.size(); j++) {
      |                   ~~^~~~~~~~~~
rail.cpp:154:7: warning: unused variable 'pos' [-Wunused-variable]
  154 |    ll pos = R - b;
      |       ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...