Codeforces 798D Mike and distribution(贪心或随机化)

题目链接 Mike and distribution

题目意思很简单,给出$a_{i}$和$b_{i}$,我们需要在这$n$个数中挑选最多$n/2+1$个,使得挑选出来的

$p_{1}$,$p_{2}$,$p_{3}$,...,$p_{m}$满足

$a_{p1}+a_{p2}+a_{p3}+...+a_{p_{m}}>a_{1}+a_{2}+a_{3}+...+a_{n}$

$b_{p1}+b_{p2}+b_{p3}+...+b_{p_{m}}>b_{1}+b_{2}+b_{3}+...+b_{n}$

$m <= n/2+1$

打这场比赛的时候怎么也想不到……眼睁睁看着自己掉分

后来看了别人的代码才恍然大悟。

贪心做法:

因为$a_{i}$和$b_{i}$都是正数,那么就直接令$m = n/2+1$

先对$a$降序排序,然后排完序之后的$1$号必选

然后在剩下$n-1$个数中,每相邻两个数分成一组,在每一组中选$b_{i}$较大的那个,就可以了。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b)	for (int i(a); i >= (b); --i)

struct node{
	int x, y, id;
} a[100010];
int n;
set <int> s;

int main(){

	scanf("%d", &n);
	rep(i, 1, n) scanf("%d", &a[i].x);
	rep(i, 1, n) scanf("%d", &a[i].y);
	rep(i, 1, n) a[i].id = i;

	sort(a + 1, a + n + 1, [](node a, node b){return a.x > b.x;});

	s.insert(a[1].id);
	for (int i = 2; i <= n; i += 2){
		int j = i + 1;
		if (j <= n && a[j].y > a[i].y) s.insert(a[j].id); else s.insert(a[i].id);
	}

	printf("%d\n", n / 2 + 1);
	for (auto u : s) printf("%d\n", u);

	return 0;
}

不过这道题还有另一种解法……那就是……随机化……

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b)	for (int i(a); i >= (b); --i)
#define LL		long long

const int N = 100010;

LL a[N], b[N], A = 0, B = 0;
int p[N], n, k;

int main(){

	scanf("%d", &n);
	rep(i, 1, n) scanf("%d", a + i), A += a[i];
	rep(i, 1, n) scanf("%d", b + i), B += b[i];

	rep(i, 1, n) p[i] = i;
	srand(time(0));
	k = n / 2 + 1;

	while (true){
		LL x = 0, y = 0;
		random_shuffle(p + 1, p + n + 1);
		rep(i, 1, k) x += a[p[i]], y += b[p[i]];
		if (2 * x > A && 2 * y > B){
			printf("%d\n", k);
			rep(i, 1, k) printf("%d\n", p[i]);
			return 0;
		}
	}

	return 0;
}

真的让我惊呆了……

时间: 2024-12-28 16:20:30

Codeforces 798D Mike and distribution(贪心或随机化)的相关文章

Codeforces 798D Mike and distribution - 贪心

Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, 

【算法系列学习】codeforces D. Mike and distribution 二维贪心

http://codeforces.com/contest/798/problem/D http://blog.csdn.net/yasola/article/details/70477816 对于二维的贪心我们可以先让它变成其中一维有序,这样只需要重点考虑另一维,就会简单很多. 首先,对于题目要求的选择元素之和两倍大与所有元素之和,我们可以转化为选择元素之和大于剩下的.然后我们可以将下标按照a从大到小排序.然后选择第一个,之后每两个一组,选择b大的一个,如果n是偶数再选择最后一个. 至于这样写

798D - Mike and distribution

题意:给n(n<=100000)组数,每组数有(a,b),求从这n组数里面选出k(k<=(n/2)+1)组.这k组所有a的和大于剩下n-k组中a的和,并且这k组中所有b的和大于剩下n-k组中b的和. 思路:首先按a排序.对于a[i],选择a[i]之前没有选择过的或者a[i]总是能>=a[i+1],然后从a[i+2]中开始选择..这样就可以保证选出来的a的和始终大于另外一半.然后对于可以选择的a,我选择最大的b,这样也可以让选出来的b有一个比它小的对应值.这个值可以通过优先队列来维护.如果

CF Round410 D. Mike and distribution

D. Mike and distribution 构造法 798D - Mike and distribution In the beginning, it's quite easy to notice that the condition " 2·(ap1?+?...?+?apk) is greater than the sum of all elements in A " is equivalent to " ap1?+?...?+?apk is greater than

Codeforces 798D:Mike and distribution

Codeforces 798D:Mike and distributio 题目链接:http://codeforces.com/problemset/problem/798/D 题目大意:给出两个大小为$n$的数列$A,B$,现要求从这两个数列相同位置取出$K(K \leqslant n/2+1)$个数,使得$2 \times subA>sumA$且$2 \times subB>sumB$. 想法题 我们需要从数列$A$和数列$B$中取出$K$个数,使得这$K$个数的和比剩下$n-K$个数的和

Codeforces 442B Andrey and Problem(贪心)

题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,现在他有n个朋友,每个朋友想出题目的概率为pi,但是他可以同时向多个人寻求帮助,不过他只能要一道题,也就是如果他向两个人寻求帮助,如果两个人都成功出题,也是不可以的. 解题思路:贪心,从概率最大的人开始考虑,如果询问他使得概率变大,则要询问. #include <cstdio> #include <cstring> #include <a

Codeforces Round #300-Tourist&#39;s Notes(贪心)

Tourist's Notes Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level

Codeforces 432E Square Tiling(构造+贪心)

我们通常这么写 using (SqlDataReader drm = sqlComm.ExecuteReader()) { drm.Read();//以下把数据库中读出的Image流在图片框中显示出来. MemoryStream ms = new MemoryStream((byte[])drm["Logo"]); Image img = Image.FromStream(ms); this.pictureBox1.Image = img; } 我的写数据 private void b

codeforces 349B Color the Fence 贪心,思维

1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1-9每个字母分别要ai升油漆,问最多可画多大的数字. 贪心,也有点考思维. #include<bits/stdc++.h> using namespace std; #define LL long long #define INF 0x3f3f3f3f int main() { int v,a[1