제출 #139247

#제출 시각아이디문제언어결과실행 시간메모리
139247evpipis철로 (IOI14_rail)C++14
30 / 100
213 ms1468 KiB
//#define TEST

#ifndef TEST
#include "rail.h"
#endif // TEST

#include <bits/stdc++.h>
using namespace std;

#ifdef TEST
void findLocation(int n, int first, int loc[], int stype[]);

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;
}

int main()
{
    //freopen("sample-2-1.in", "r", stdin);
  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;
}
#endif // TEST


#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;

const int len = 5005;
map<ii, int> mym;
int s, l, r;
vector<int> vec;

//int hello_cnt = 0;

int dis(int a, int b){
    if (a == b) return 0;
    if (a > b) swap(a, b);
    if (mym.count(mp(a, b))) return mym[mp(a, b)];

    //hello_cnt++;
    //printf("asked: a = %d, b = %d\n", a, b);
    return mym[mp(a, b)] = getDistance(a, b);
}

bool comp(int a, int b){
    return (dis(s, a) < dis(s, b));
}

void findLocation(int n, int first, int loc[], int stype[]){
    loc[0] = first;
    stype[0] = 1;

    if (n == 1)
        return;

    for (int i = 0; i < n; i++)
        stype[i] = 0;

    s = 0, l = 0, r = -1;
    for (int i = 1; i < n; i++)
        if (r == -1 || dis(s, i) < dis(s, r))
            r = i;

    loc[s] = first;
    stype[s] = 1;

    loc[r] = loc[s]+dis(s, r);
    stype[r] = 2;

    for (int i = 0; i < n; i++)
        if (i != l && i != r)
            vec.pb(i);
    sort(vec.begin(), vec.end(), comp);

    for (int i = 0; i < vec.size(); i++){
        int nex = vec[i], lef = dis(l, nex), rig = dis(r, nex), can = 0;

        //printf("l = %d, r = %d, nex = %d, lef = %d, rig = %d\n", l, r, nex, lef, rig);
        if (lef < loc[s]-loc[l]){
            int pos = l;
            for (int i = 0; i < n; i++)
                if (stype[i] == 1 && loc[i] < loc[l]+lef && loc[i] > loc[pos])
                    pos = i;

            if (rig == loc[r]-loc[pos] + loc[l]+lef-loc[pos])
                can = 1;
        }
        if (lef > loc[r]-loc[l]){
            int pos = l;
            for (int i = 0; i < n; i++)
                if (stype[i] == 1 && loc[i] < loc[r] && loc[i] > loc[pos])
                    pos = i;

            if (rig == loc[r]-loc[pos] + loc[l]+lef-loc[pos])
                can = 1;
        }

        //printf("can = %d\n", can);

        if (can){
            loc[nex] = loc[l]+lef;
            stype[nex] = 2;
            if (loc[nex] > loc[r])
                r = nex;
        }
        else{
            loc[nex] = loc[r]-rig;
            stype[nex] = 1;
            if (loc[nex] < loc[l])
                l = nex;
        }
    }

    //printf("used: %d\n", hello_cnt);
    //for (int i = 0; i < n; i++)
      //  printf("i = %d, tp = %d, loc = %d\n", i, stype[i], loc[i]);
}

/*
test cases:

4
1
1 10

4
10
1 0
2 2
2 5
2 6
1 9
1 10
1 11
2 15
2 20
2 21
*/

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

rail.cpp: In function 'void findLocation(int, int, int*, int*)':
rail.cpp:188:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < vec.size(); i++){
                     ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...