Codeforces 798D 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;
}
真的让我惊呆了……
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 tha ...
- Codeforces 798D - Mike and distribution(二维贪心、(玄学)随机排列)
题目链接:http://codeforces.com/problemset/problem/798/D 题目大意:从长度为n的序列A和序列B中分别选出k个下表相同的数要求,设这两个序列中k个数和分别为 ...
- CodeForces - 798D Mike and distribution 想法题,数学证明
题意:给你两个数列a,b,你要输出k个下标,使得这些下标对应的a的和大于整个a数列的和的1/2.同时这些下标对应的b //题解:首先将条件换一种说法,就是要取floor(n/2)+1个数使得这些数大于 ...
- Codeforces 798D Mike and distribution (构造)
题目链接 http://codeforces.com/contest/798/problem/D 题解 前几天的模拟赛,居然出这种智商题..被打爆了QAQ 这个的话,考虑只有一个序列怎么做,把所有的排 ...
- Codeforces 798D Mike and distribution
题目链接 题目大意 给定两个序列a,b,要求找到不多于个下标,使得对于a,b这些下标所对应数的2倍大于所有数之和. N<=100000,所有输入大于0,保证有解. 因为明确的暗示,所以一定找个. ...
- 【算法系列学习】codeforces D. Mike and distribution 二维贪心
http://codeforces.com/contest/798/problem/D http://blog.csdn.net/yasola/article/details/70477816 对于二 ...
- CF798D Mike and distribution 贪心
我感觉这道题挺神的~ 假设 $a[i]=b[i]$,那么我们可以将 $a$ 降序排序,然后你发现只要你按照 $1,3,5......n$ 这么取一定是合法的. 而我们发现 $2$ 比取 $3$ 优,取 ...
- codeforces 798 D. Mike and distribution
D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- [CF798D]Mike and distribution_贪心
Mike and distribution 题目链接:http://codeforces.com/problemset/problem/798/D 数据范围:略. 题解: 太难了吧这个题..... 这 ...
随机推荐
- python 中 * 与**的使用
1.参数中使用如果是函数定义中参数前的*表示的是将调用时的多个参数放入元组中,**则表示将调用函数时的关键字参数放入一个字典中 如定义以下函数 def func(*args): print(args) ...
- Down the Pyramid
Do you like number pyramids? Given a number sequence that represents the base, you are usually suppo ...
- THUSC2019游记
Day 0 完全没有明明是最后一次机会的紧张感.大概是滚粗的预兆. 住在西郊.房间好小. 和thupc前一样又有一场cometoj,好像又有小裙子了.upd:改成星空棒棒糖! Day 1 早餐有点棒. ...
- 64位程序调用32DLL解决方案
最近做一个.NETCore项目,需要调用以前用VB6写的老程序,原本想重写,但由于其调用了大量32DLL,重写后还需要编译为32位才能运行,于是干脆把老代码整个封装为32DLL,然后准备在64位程序中 ...
- ogre3D学习基础10 -- 键盘控制与鼠标控制(直接控制)
要实现键盘,鼠标对场景的控制,首先要帧监听,就是在每一帧的渲染前后对它进行操作.这里的操作没有用到缓冲区,只是简单的直接获取. 1.这些步骤和前面的一样,直接上代码,操作还是在createScene函 ...
- Selenium WebDriver-通过键盘事件操作浏览器
#encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...
- 正在创建模型,此时不可使用上下文“的解决办法。 正在创建模型,此时不可使用上下文。如果在 OnModelCreating 方法内使用上下文或如果多个线程同时访问同一上下文实例,可能引发此异常。请注意不
//默认为: Database.SetInitializer<testContext>(null);//这里报错, 检查原因:catch(Exception ex) 错误提示: 基础连接未 ...
- 【USACO09FEB】 庙会班车 Fair Shuttle 贪心+线段树
Although Farmer John has no problems walking around the fair to collect prizes or see the shows, his ...
- TOJ4483: Common Digit Pairs
4483: Common Digit Pairs Time Limit(Common/Java):3000MS/9000MS Memory Limit:65536KByteTotal Sub ...
- 九度oj 题目1177:查找
题目描述: 读入一组字符串(待操作的),再读入一个int n记录记下来有几条命令,总共有2中命令:1.翻转 从下标为i的字符开始到i+len-1之间的字符串倒序:2.替换 命中如果第一位为1,用命 ...