답안 #77219

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
77219 2018-09-24T03:12:48 Z shoemakerjo Roller Coaster Railroad (IOI16_railroad) C++14
30 / 100
1289 ms 84256 KB
#include "railroad.h"
#include <bits/stdc++.h>

#define ll long long
using namespace std;

#define maxv 400010
#define mp make_pair

vector<ll> nums;
vector<int> adj[maxv];
int par[maxv];

const int inf = 1000000010;

int findset(int u) {
	if (par[u] == u) return u;
	return par[u] = findset(par[u]);
}

void unionset(int u, int v) {
	u = findset(u);
	v = findset(v);
	if (u != v) {
		if (rand() % 2) { //lazy dsu
			par[u] = v;
		}
		else par[v] = u;
	}
}

map<ll, int> indo;
int delt[maxv];


ll plan_roller_coaster(vector<int> s, vector<int> t) {
	srand(23);
    int n = (int) s.size();
    set<int> og;
    for (int v : s)  og.insert(v);
   	for (int v : t) og.insert(v);
   	// og.insert(1);
   	// og.insert(inf);
   	for (int v : og) nums.push_back(v);

   	for (int i = 0; i < nums.size(); i++) {
   		indo[nums[i]] = i;
   		par[i] = i; //setting up the dsu for later
   	}

   	// cout << "here 1" << endl; 

   	for (int i = 0; i < s.size(); i++) {
   		adj[indo[s[i]]].push_back(indo[t[i]]);
   		// cout << s[i] << " to " << t[i] << " is: " << endl;
   		// cout << "   " << indo[s[i]] << " to " << indo[t[i]] << endl;
   	}
   	// adj[indo[inf]].push_back(indo[1]);
   	// delt[0]--;
   	// cout << "last guy is " << indo[inf] << " to " << indo[1] << endl;

   	ll ans = 0LL;

   	// cout << "here 2 " << endl;

   	for (int i = 0; i < nums.size(); i++) {
   		for (int v : adj[i]) {
   			delt[i]++;
   			delt[v]--;
   		}
   		
   	}

   	// cout << "here 3" << endl;

   	int curval = 0;
   	for (int i = 0; i < nums.size()-1; i++) {
   		curval += delt[i];
   		// cout << i << " " << nums[i] << " : " << curval << endl;
   		// cout << "    " << delt[i]  << endl;

   		if (curval > 1) {
   			// cout << i << " is positive! " << endl;
   			ans += (curval-1LL) * 1LL * (nums[i]-nums[i-1]);
   			adj[i+1].push_back(i);
   		}
   		else if (curval < 1) {
   			adj[i].push_back(i+1);
   		}
   	}

   	vector<pair<ll, int>> edges; 
   	//the first is the lenght, the second is the starting guy
   	for (int i = 0; i < nums.size()-1; i++) {
   		edges.push_back(mp(nums[i+1]-nums[i], i));
   	}
   	sort(edges.begin(), edges.end());

   	for (int i = 0; i < nums.size(); i++) {
   		for (int v : adj[i]) unionset(i, v);
   	}

   	for (int i = 0; i < edges.size(); i++) {
   		if (findset(edges[i].second) != findset(edges[i].second+1)) {
   			unionset(edges[i].second, edges[i].second+1);
   			ans += edges[i].first;
   		}
   	}

    return ans;
}

Compilation message

railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:46:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < nums.size(); i++) {
                     ~~^~~~~~~~~~~~~
railroad.cpp:53:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < s.size(); i++) {
                     ~~^~~~~~~~~~
railroad.cpp:66:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < nums.size(); i++) {
                     ~~^~~~~~~~~~~~~
railroad.cpp:77:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < nums.size()-1; i++) {
                     ~~^~~~~~~~~~~~~~~
railroad.cpp:94:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < nums.size()-1; i++) {
                     ~~^~~~~~~~~~~~~~~
railroad.cpp:99:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < nums.size(); i++) {
                     ~~^~~~~~~~~~~~~
railroad.cpp:103:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < edges.size(); i++) {
                     ~~^~~~~~~~~~~~~~
railroad.cpp:38:9: warning: unused variable 'n' [-Wunused-variable]
     int n = (int) s.size();
         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9720 KB n = 2
2 Correct 9 ms 9724 KB n = 2
3 Correct 10 ms 9784 KB n = 2
4 Correct 10 ms 9992 KB n = 2
5 Correct 10 ms 9992 KB n = 2
6 Incorrect 10 ms 9992 KB answer is not correct: 69049296 instead of 523688153
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9720 KB n = 2
2 Correct 9 ms 9724 KB n = 2
3 Correct 10 ms 9784 KB n = 2
4 Correct 10 ms 9992 KB n = 2
5 Correct 10 ms 9992 KB n = 2
6 Incorrect 10 ms 9992 KB answer is not correct: 69049296 instead of 523688153
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1283 ms 83952 KB n = 199999
2 Correct 1289 ms 84148 KB n = 199991
3 Correct 1277 ms 84148 KB n = 199993
4 Correct 917 ms 84148 KB n = 152076
5 Correct 460 ms 84148 KB n = 93249
6 Correct 1048 ms 84148 KB n = 199910
7 Correct 1161 ms 84148 KB n = 199999
8 Correct 1020 ms 84148 KB n = 199997
9 Correct 1078 ms 84148 KB n = 171294
10 Correct 799 ms 84148 KB n = 140872
11 Correct 1089 ms 84148 KB n = 199886
12 Correct 1139 ms 84148 KB n = 199996
13 Correct 1043 ms 84148 KB n = 200000
14 Correct 1129 ms 84148 KB n = 199998
15 Correct 1057 ms 84148 KB n = 200000
16 Correct 1158 ms 84148 KB n = 199998
17 Correct 1109 ms 84148 KB n = 200000
18 Correct 1130 ms 84148 KB n = 190000
19 Correct 982 ms 84148 KB n = 177777
20 Correct 508 ms 84148 KB n = 100000
21 Correct 1233 ms 84208 KB n = 200000
22 Correct 1246 ms 84208 KB n = 200000
23 Correct 1226 ms 84256 KB n = 200000
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 9720 KB n = 2
2 Correct 9 ms 9724 KB n = 2
3 Correct 10 ms 9784 KB n = 2
4 Correct 10 ms 9992 KB n = 2
5 Correct 10 ms 9992 KB n = 2
6 Incorrect 10 ms 9992 KB answer is not correct: 69049296 instead of 523688153
7 Halted 0 ms 0 KB -