답안 #57097

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
57097 2018-07-14T02:22:46 Z qkxwsm Teleporters (IOI08_teleporters) C++17
85 / 100
1000 ms 39852 KB
/*
PROG: ioi08d
LANG: C++11
    _____
  .'     '.
 /  0   0  \
|     ^     |
|  \     /  |
 \  '---'  /
  '._____.'
 */
#include <bits/stdc++.h>

using namespace std;

template<class T>
void readi(T &x)
{
	T input = 0;
	bool negative = false;
	char c = ' ';
	while (c < '-')
	{
		c = getchar();
	}
	if (c == '-')
	{
		negative = true;
		c = getchar();
	}
	while (c >= '0')
	{
		input = input * 10 + (c - '0');
		c = getchar();
	}
	if (negative)
	{
		input = -input;
	}
	x = input;
}
template<class T>
void printi(T output)
{
	if (output == 0)
	{
		putchar('0');
		return;
	}
	if (output < 0)
	{
		putchar('-');
		output = -output;
	}
	int aout[20];
	int ilen = 0;
	while(output)
	{
		aout[ilen] = ((output % 10));
		output /= 10;
		ilen++;
	}
	for (int i = ilen - 1; i >= 0; i--)
	{
		putchar(aout[i] + '0');
	}
	return;
}
template<class T>
void ckmin(T &a, T b)
{
	a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
	a = max(a, b);
}
long long randomize(long long mod)
{
	return ((1ll << 30) * rand() + (1ll << 15) * rand() + rand()) % mod;
}

#define MP make_pair
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second

const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-10;

#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 2000013

long long normalize(long long x, long long mod = INF)
{
	return (((x % mod) + mod) % mod);
}

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int N, K;
pii coor[MAXN];
vector<int> compress;
int edge[MAXN];
int dsu[MAXN];
int sz[MAXN];
int countsort[MAXN];
int cur;
vector<int> sizes;

int find_parent(int u)
{
	if (u == dsu[u]) return u;
	dsu[u] = find_parent(dsu[u]);
	return dsu[u];
}
void merge(int u, int v)
{
	u = find_parent(u);
	v = find_parent(v);
	if (u == v)
	{
		return;
	}
	if (sz[u] > sz[v])
	{
		swap(u, v);
	}
	sz[v] += sz[u];
	sz[u] = 0;
	dsu[u] = v;
	return;
}

int32_t main()
{
	ios_base::sync_with_stdio(0); 
	srand(time(0));
	//	cout << fixed << setprecision(10);	
	//	cerr << fixed << setprecision(10);
	if (fopen("ioi08d.in", "r"))
	{	
		freopen ("ioi08d.in", "r", stdin);
		//	freopen ("ioi08d.out", "w", stdout);
	}
	cin >> N >> K;
	for (int i = 0; i < N; i++)
	{
		cin >> coor[i].fi >> coor[i].se;
		compress.PB(coor[i].fi); compress.PB(coor[i].se);
	}
	sort(compress.begin(), compress.end());
	compress.erase(unique(compress.begin(), compress.end()), compress.end());
	for (int i = 0; i < N; i++)
	{
		coor[i].fi = LB(compress.begin(), compress.end(), coor[i].fi) - compress.begin();
		coor[i].se = LB(compress.begin(), compress.end(), coor[i].se) - compress.begin();
		coor[i].fi++; coor[i].se++;
	}
	for (int i = 0; i < N; i++)
	{
		int u = coor[i].fi, v = coor[i].se;
//		cerr << u << "--" << v << endl;
		edge[u] = v + 1;
		edge[v] = u + 1;
	}
	edge[0] = 1;
	edge[2 * N + 1] = 0;
	for (int i = 0; i <= 2 * N + 1; i++)
	{
		dsu[i] = i;
		sz[i] = 1;
	}
	for (int i = 0; i <= 2 * N + 1; i++)
	{
		merge(i, edge[i]);
	}
	for (int i = 0; i <= 2 * N + 1; i++)
	{
		if (find_parent(i) != i)
		{
			continue;
		}
		if (i == find_parent(0))
		{
			cur = sz[i];
		}
		else
		{
			sizes.PB(sz[i]);
		}
	}
//	cerr << cur << endl;
//	for (int i = 0; i <= 2 * N + 1; i++)
//	{
//		cerr << i << ' ' << edge[i] << endl;
//	}
	for (int x : sizes)
	{
		countsort[x]++;
	}
	sizes.clear();
	for (int i = 0; i < MAXN; i++)
	{
		for (int j = 0; j < countsort[i]; j++)
		{
			sizes.PB(i);
		}
	}
	for (int i = 0; i < K; i++)
	{
		if (sizes.empty())
		{
			sizes.PB(1);
			cur += 1;
		}
		else
		{
			cur += (sizes.back() + 2);
			sizes.pop_back();
		}
	}
	cout << cur - 2 << '\n';
	//if they're in different components, then
	//when you add x...y
	//0, 1, 5, 7, so it's pure x/2
	//	cerr << "time elapsed = " << (clock() / (CLOCKS_PER_SEC / 1000)) << " ms" << endl;
	return 0;
}

Compilation message

teleporters.cpp: In function 'int32_t main()':
teleporters.cpp:157:11: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   freopen ("ioi08d.in", "r", stdin);
   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 788 KB Output is correct
2 Correct 17 ms 1332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1332 KB Output is correct
2 Correct 17 ms 1620 KB Output is correct
3 Correct 37 ms 2660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 2660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 2660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 136 ms 6928 KB Output is correct
2 Correct 390 ms 14276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 265 ms 14276 KB Output is correct
2 Correct 681 ms 26588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 837 ms 36232 KB Output is correct
2 Execution timed out 1042 ms 39852 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1014 ms 39852 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1006 ms 39852 KB Time limit exceeded
2 Halted 0 ms 0 KB -