COCI 2016/2017 Round6 Task 1 - 5 题解

Review of simulated competition

Posted by nomonick on February 22, 2021

前言

模拟赛的考试题目,整体来说不算很难,但是因为 Lemon 这个老年测评姬,导致有些题目直接炸掉(TLE 满天飞),不过还有有三个 dalao 吊打了 std

Task 1. Hindex

  • 题目翻译

  • 解析

因为要满足 “ 选择 x 篇论文,使其每一篇引文的得分都至少为 x ” 所以 h 值最小的论文的 h 值要大于选择的论文数。

依照上述规则,h 的值从大到小排序文章,直接模拟即可

  • code
#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm> // 万能头不是好习惯

using namespace std;

#define int long long

const int SIZE = (int)5e5 + 50;

inline int read()
{
	int x = 0,f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
	{
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return f * x;
}

int n,ans;
int num[SIZE];

signed main()
{
	n = read();
	for (int i = 1; i <= n; ++i)
		num[i] = read();
	sort(num+1,num+1+n,greater<int>());
	for (int i = 1; i <= n && num[i] >= i; ++i)
		ans++;
	printf("%lld\n",ans);
	return 0;
}


Task 2. Telefoni

  • 题目翻译

  • 解析

这道题用贪心的思路去解决。两个电话之间尽量的远,这样要放置的电话的数量就是最少的。如果这个桌子到前面一个有电话的桌子的距离达到了 D 那么代表了这个地方至少要有一个电话。因此直接通过这样的方式模拟就行了。

  • code
#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm> // 万能头不是好习惯

using namespace std;

#define int long long

const int SIZE = (int)5e5 + 50;

inline int read()
{
	int x = 0,f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
	{
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return f * x;
}

int n,last,limit,ans;
bool vis[SIZE];

signed main()
{
	n = read(); limit = read();
	for (int i = 1; i <= n; ++i)
		vis[i] = (read() == 1 ? true : false);
	for (int i = 1; i <= n; ++i)
		if (vis[i]) last = i; // 如果有桌子上有电话,就标记
		else if (i - last == limit) 
			ans++,last = i;
			// 当距离达 D 的时候,而且那个桌子上还没有电话,就放置一个。
	printf("%lld\n",ans);
	return 0;
}


Task 3. Turnir

  • 题目翻译

  • 解析

设起始的数列里总共有n个数字。当我们来计算X能达到的最高水平时:对于第0级,显然不可能有一个大于X的数,即大于X的数必须是0。为了达到第一级,必须有最多n/2个数字大于它的数字;否则,至少有一个大于X的数字将它击败,导致X无法进入下一级。如果有超过n/2的数字大于X,那么X应该在第一个数字以下。现在,为了使X处于第二级,大于X的数不能超过n/2+n/4。

以此类推:答案就是从大于x的数字中减去n*2^(-k),k就是减第几次。

  • code
#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm> // 万能头不是好习惯

using namespace std;

#define int long long

const int NUM = 20;
const int SIZE = (1 << NUM) + 50;

inline int read()
{
	int x = 0,f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
	{
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return f * x;
}

int n;
int num[SIZE];
std::vector<int> v;

inline int calc(int x)
{
	if (x == 0) return 0;
	for (int i = n-1; i >= 0; --i)
		if ((1 << i) < x) x -= (1 << i);
		else return n - i;
	return n;
}

signed main()
{
	n = read();
	for (int i = 1; i <= (1 << n); ++i)
		v.push_back(num[i] = read());
	sort(v.begin(), v.end());
	for (int i = 1; i <= (1 << n); ++i)
		printf("%lld ",calc(v.end()-upper_bound(v.begin(), v.end(),num[i])));
	return 0;
}

Task 4. Savrsen

  • 题目翻译

  • 解析

一一数字去寻找除数,很慢,肯定是通过不了题目。

这里应当采取一种相反的方式。他的过程就认为埃氏筛。对于每一个数 i = 1,2,3,……,b,当它作为除数,检查它是哪一个数字的除数,依照倍数向上迭代,求和。对于每一个倍数 x ,都增加他的除数 i : sum[x] += i;

总复杂度约为 O(blog(b))

  • code
#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm> // 万能头不是好习惯

using namespace std;

#define int long long

const int SIZE = (int)1e7 + 50;

inline int read()
{
	int x = 0,f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
	{
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return f * x;
}

int ls,rs,ans;
int num[SIZE];

signed main()
{
	ls = read();rs = read();
	for (int i = 1; i <= rs; ++i)
		for (int j = 2; i * j <= rs; ++j)
			num[i*j] += i;
	for (int i = ls; i <= rs; ++i)
		ans += abs(num[i]-i);
	printf("%lld\n",ans);
	return 0;
}

Task 5. Sirni

  • 题目翻译

  • 解析

在这个任务中,我们需要在一个图中找到最小生成树, 其中两个节点a和b以权min(Xa%Xb,Xb%Xa)连接。对于Xa>Xb,该表达式等于Xa%Xb,反之亦然。

如果两个节点存在相等的值,我们可以零代价连接它们,并将它们作为一个节点来观察。这样就可以将假设所有节点值都是唯一的。

之后开始建图连边,先设定求出来的余数,找到所有大于等于它的数(只有这些数才可能求出这样的余数),逐一判断是有存在可以得出这样的余数的数对。然后运用Kruskal生成一个最小生成树,即可。

  • code
#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm> // 万能头不是好习惯

using namespace std;

#define int long long

#define pd(u,v) (get_root(u) == get_root(v))

const int SIZE = (int)1e5 + 50;
const int NUM = (int)1e7 + 50;

inline int read()
{
	int x = 0,f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9')
	{
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
	{
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	return f * x;
}

int n,tot,ans;
int num[SIZE],fa[SIZE],vis[NUM];

inline int get_root(int u)
{
	return fa[u] == u ? fa[u] : get_root(fa[u]);
}

inline void merge(int u,int v)
{
	if (pd(u,v)) return;
	tot++;
	ans += min(num[u] % num[v],num[v] % num[u]);
	fa[get_root(u)] = get_root(v);
}

signed main()
{
	n = read();
	for (int i = 1; i <= n; ++i)
		num[i] = read();
	sort(num+1,num+1+n);
	n = unique(num+1,num+1+n)-num-1;
	for (int i = 1; i <= n; ++i)
		vis[num[i]] = i,fa[i] = i;
	for (int mod = 0; mod <= num[n] && tot < n - 1; ++mod)
		for (int pos = upper_bound(num+1,num+1+n,mod)-num; pos <= n && tot < n - 1; ++pos)
			for (int i = num[pos]+mod; i <= num[n] && tot < n - 1; i += num[pos])
				if (vis[i])
					merge(pos,vis[i]);
	printf("%lld\n",ans);
	return 0;
}