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 数据范围:略. 题解: 太难了吧这个题..... 这 ...
随机推荐
- vue之列表循环
文档:https://cn.vuejs.org/v2/guide/list.html 当 Vue.js 用 v-for 正在更新已渲染过的元素列表时,它默认用“就地复用”策略.如果数据项的顺序被改变, ...
- thinkcmf5更新模板代码分析,解决模板配置json出错导致数据库保存的配置项内容丢失问题
private function updateThemeFiles($theme, $suffix = 'html') { $dir = 'themes/' . $theme; $themeDir = ...
- Java代码中的(解压7z加密版)
maven:需要加上这个下载这两个包 <dependency> <groupId>net.sf.sevenzipjbinding</groupId> <art ...
- ACM 贪心算法总结
贪心算法的本质: 就是当前状态的最优解,它并不考虑全局. 什么是当前状态的最优解? 成本问题? https://www.cnblogs.com/xuxiaojin/p/9400892.html (po ...
- 【草稿】JS中如何操作时间
如何声明时间变量 如何设置时间变量的时.分.秒.毫秒 如何根据字符串变量,声明指定的时间变量 如何比较两个时间变量 代码如下: $(function () { var d = new Date(); ...
- kettle-批量同步表数据
一.实验目标 利用kettle实现从mysql数据库中的dbf库批量同步表到dbm库(全量同步) 二.实验环境 dbf 库中表f1.f2.f3 .f1中1条数据,f2中100条数据,f3中2条数据 ...
- P1627 中位数
P1627 中位数 题目描述 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 输入输出格式 输入格式: 第一行为两个正整 ...
- 装饰器与lambda
装饰器 实际上理解装饰器的作用很简单,在看core python相关章节的时候大概就是这种感觉.只是在实际应用的时候,发现自己很难靠直觉决定如何使用装饰器,特别是带参数的装饰器,于是摊开来思考了一番, ...
- 蓝桥杯Java输入输出相关
转载自:http://blog.csdn.net/Chen_Tongsheng/article/details/53354169 一.注意点 1. 类名称必须采用public class Main方式 ...
- 常用类--Date日期类,SimpleDateFormat日期格式类,Calendar日历类,Math数学工具类,Random随机数类
Date日期类 Date表示特定的时间,精确到毫秒; 构造方法: public Data() public Date(long date) 常用方法: public long getTime() pu ...