题目链接 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(贪心或随机化)的更多相关文章

  1. Codeforces 798D Mike and distribution - 贪心

    Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...

  2. Codeforces 798D - Mike and distribution(二维贪心、(玄学)随机排列)

    题目链接:http://codeforces.com/problemset/problem/798/D 题目大意:从长度为n的序列A和序列B中分别选出k个下表相同的数要求,设这两个序列中k个数和分别为 ...

  3. CodeForces - 798D Mike and distribution 想法题,数学证明

    题意:给你两个数列a,b,你要输出k个下标,使得这些下标对应的a的和大于整个a数列的和的1/2.同时这些下标对应的b //题解:首先将条件换一种说法,就是要取floor(n/2)+1个数使得这些数大于 ...

  4. Codeforces 798D Mike and distribution (构造)

    题目链接 http://codeforces.com/contest/798/problem/D 题解 前几天的模拟赛,居然出这种智商题..被打爆了QAQ 这个的话,考虑只有一个序列怎么做,把所有的排 ...

  5. Codeforces 798D Mike and distribution

    题目链接 题目大意 给定两个序列a,b,要求找到不多于个下标,使得对于a,b这些下标所对应数的2倍大于所有数之和. N<=100000,所有输入大于0,保证有解. 因为明确的暗示,所以一定找个. ...

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

    http://codeforces.com/contest/798/problem/D http://blog.csdn.net/yasola/article/details/70477816 对于二 ...

  7. CF798D Mike and distribution 贪心

    我感觉这道题挺神的~ 假设 $a[i]=b[i]$,那么我们可以将 $a$ 降序排序,然后你发现只要你按照 $1,3,5......n$ 这么取一定是合法的. 而我们发现 $2$ 比取 $3$ 优,取 ...

  8. codeforces 798 D. Mike and distribution

    D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  9. [CF798D]Mike and distribution_贪心

    Mike and distribution 题目链接:http://codeforces.com/problemset/problem/798/D 数据范围:略. 题解: 太难了吧这个题..... 这 ...

随机推荐

  1. 记一次低级错误导致的mysql(111)

    今天下午配好的双主多从服务器,两台主机+主机内安装好的6台虚拟机,两台Mysql master各授权好其slave的远程登录,原本好端端的能远程登录,晚上回来时候就发现其中一台master登录不上其s ...

  2. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  3. Git命令大总结(纯手办)

    Git完整命令手册地址:http://git-scm.com/docs PDF版命令手册地址:github-git-cheat-sheet.pdf 1.git config -l查看全局用户信息配置 ...

  4. pidgin中使用QQ

    安装     实现的功能:   可以发送静态表情 可以发送动态表情 可以发图片? 可以添加好友 可以添加群?  

  5. goalng导出excel(csv格式)

    最近项目中有个小需求,需要将查询结果导出到excel.之间前java比较容易,使用POI很容易就能实现,查了下golang的文档,发现golang下边并没有导出excel的包,但是却有一个encodi ...

  6. Netcore 基础之TagHelper知识

    饮水思源,来自:http://www.cnblogs.com/liontone 的BLOG中关于taghelper中的内容 概要 TagHelper是ASP.NET 5的一个新特性.也许在你还没有听说 ...

  7. loj2276 「HAOI2017」新型城市化

    给出的图是一个二分图(显然--吗),一个图的最大团=其补图的最大独立集,因此二分图的最大独立集就是补图的最大团. 欲使补图最大团变大,则要最大独立集变大.二分图最大独立集=点数-最小点覆盖.最小点覆盖 ...

  8. 命令行下修改postgres密码

    1. 修改PostgreSQL数据库默认用户postgres的密码 PostgreSQL数据库创建一个postgres用户作为数据库的管理员,密码随机,所以需要修改密码,方式如下: 步骤一:登录Pos ...

  9. Leetcode 424.替换后的最长重复字符

    替换后的最长重复字符 给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度. 注意:字符串长度 和 ...

  10. Django中间件、Auth认证

    中间件 一:什么是中间件 是介于request与response处理之间的一道处理过程 二:中间件的作用 如果你想修改请求,例如被传送到view中的HttpRequest对象. 或者你想修改view返 ...