Submission #57100

# Submission time Handle Problem Language Result Execution time Memory
57100 2018-07-14T02:36:45 Z qkxwsm Teleporters (IOI08_teleporters) C++17
100 / 100
722 ms 45028 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;
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 >> dsu[i] >> sz[i];
		countsort[dsu[i]]++;
		countsort[sz[i]]++;
	}
	for (int i = 1; i < MAXN; i++)
	{
		countsort[i] += countsort[i - 1];
	}
	for (int i = 0; i < N; i++)
	{
		dsu[i] = countsort[dsu[i]];
		sz[i] = countsort[sz[i]];
		int u = dsu[i], v = sz[i];
		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]);
	}
	sizes.reserve(2 * N + 1);
	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 i = 0; i < MAXN; i++)
	{
		countsort[i] = 0;
	}
	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:155: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);
   ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 18 ms 8184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 8184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 8232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 8288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 8456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 8456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 8536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 8576 KB Output is correct
2 Correct 21 ms 8888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 8888 KB Output is correct
2 Correct 20 ms 9280 KB Output is correct
3 Correct 26 ms 9864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 9864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 9896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 12600 KB Output is correct
2 Correct 241 ms 16924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 131 ms 16924 KB Output is correct
2 Correct 301 ms 23120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 589 ms 28512 KB Output is correct
2 Correct 597 ms 31724 KB Output is correct
3 Correct 556 ms 35288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 617 ms 35288 KB Output is correct
2 Correct 696 ms 41908 KB Output is correct
3 Correct 670 ms 41908 KB Output is correct
4 Correct 722 ms 41908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 698 ms 43552 KB Output is correct
2 Correct 718 ms 43828 KB Output is correct
3 Correct 331 ms 44948 KB Output is correct
4 Correct 671 ms 45028 KB Output is correct