CF798D Mike and distribution
CF798D 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=[a_{1},a_{2},...,a_{n}]A=[a1,a2,...,a**n] and B=[b_{1},b_{2},...,b_{n}]B=[b1,b2,...,b**n] of length nn each which he uses to ask people some quite peculiar questions.
To test you on how good are you at spotting inequality in life, he wants you to find an "unfair" subset of the original sequence. To be more precise, he wants you to select kk numbers P=[p_{1},p_{2},...,p_{k}]P=[p1,p2,...,p**k] such that 1<=p_{i}<=n1<=p**i<=n for 1<=i<=k1<=i<=k and elements in PP are distinct. Sequence PP will represent indices of elements that you'll select from both sequences. He calls such a subset PP "unfair" if and only if the following conditions are satisfied: 2·(a_{p1}+...+a_{pk})2⋅(a**p1+...+apk) is greater than the sum of all elements from sequence AA , and 2·(b_{p1}+...+b_{pk})2⋅(b**p1+...+bpk) is greater than the sum of all elements from the sequence BB . Also, kk should be smaller or equal to because it will be to easy to find sequence PP if he allowed you to select too many elements!
Mike guarantees you that a solution will always exist given the conditions described above, so please help him satisfy his curiosity!
输入格式
The first line contains integer nn ( 1<=n<=10^{5}1<=n<=105 ) — the number of elements in the sequences.
On the second line there are nn space-separated integers a_{1},...,a_{n}a1,...,a**n ( 1<=a_{i}<=10^{9}1<=a**i<=109 ) — elements of sequence AA .
On the third line there are also nn space-separated integers b_{1},...,b_{n}b1,...,b**n ( 1<=b_{i}<=10^{9}1<=b**i<=109 ) — elements of sequence BB .
输出格式
On the first line output an integer kk which represents the size of the found subset. kk should be less or equal to .
On the next line print kk integers p_{1},p_{2},...,p_{k}p1,p2,...,p**k ( 1<=p_{i}<=n1<=p**i<=n ) — the elements of sequence PP . You can print the numbers in any order you want. Elements in sequence PP should be distinct.
题意翻译
题意:给两个长度为n的数列A,B,要求至多选择n/2+1个下标,使得A数组中选出的数的和的两倍大于sumA,B数组中选出的数的和的两倍大于sumB
输入输出样例
输入 #1复制
输出 #1复制
题解:
2019.10.22模拟赛40分场
一看就是道贪心的题。这自然不必说。重点在于如何建立贪心策略和维护贪心的正确性(也是把本蒟蒻卡的死死的点)。
先来理解一下题意:要选出\(n/2+1\)对数,使得这些数的二倍比原数列和还大。
于是我们得到了一个结论:这些数是按对选的,也就是绑定选择。
根据这个思路,我们想到了构建结构体解决问题。
因为我们输出的时候要输出数组下标,所以我们不仅维护每个位置的数(a和b),而且还要构建一个映射关系,即要把数对的位置也存进结构体。这些都是基础操作,我就不多说了。
考场上直接写了爆搜加部分分骗了40
后来看了正解,加上一些自己的理解和证明,得出了这篇辣鸡博客。
我曾经\(YY\)过,贪心总是和排序结合在一起的,这道题当然不会例外。但是我们这道题的排序有一些困难,难点还是在于绑定选择。我们能通过对\(A\)数列或\(B\)数列排序来维护有解的情况,但是我们并无法构建一种可行的排序,使得在维护\(A\)序列的同时还维护\(B\)序列的正确性。
于是我们“退而求其次”,我们只维护\(A\)序列,然后乱搞搞尽量维护\(B\)序列的正确性,能行就行,不行拉倒。
然后我们建一个结构体\(num[i].a.b.id\),分别存\(A,B\)序列的值以及序号。之后按照\(A\)序列进行排序,根据贪心原则,我们选靠前的项一定对答案贡献更大。那我们就直接考虑\(B\)序列。
这里的贪心策略与正常的贪心略有不同。对于这种题目,我们经常会被拐进:对于每个物品,选还是不选的误区。于是我们便走进了一个死胡同,发现好像怎么证明都无法得出正确的答案。所以,我们提出一种全新的贪心选择的思路(应该也比较常见):对于一对物品,选第一个还是第二个。
于是,我们先选\(num[1]\)(因为个数是\(n/2+1\)),然后对于之后的每对物品,选择其中\(num[i].b\)值更大的那个。这就是正确的贪心决策了。
怎么证明呢?
因为我们是按\(A\)关键词排序,所以得出的序列肯定是\(A\)大的在前面,因为我们是捆绑选择。所以我们在\(A\)序列有序的基础上按对选择\(B\),得出的答案一定是最优的。
代码:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e5+1;
int n,tot,ans[maxn];
struct node
{
int a,b,id;
}num[maxn];
bool cmp(node a,node b)
{
return a.a>b.a;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&num[i].a);
num[i].id=i;
}
for(int i=1;i<=n;i++)
scanf("%d",&num[i].b);
sort(num+1,num+n+1,cmp);
int cnt=n/2+1;
ans[++tot]=num[1].id;
for(int i=2;i<=n;i+=2)
{
if(num[i].b>num[i+1].b)
ans[++tot]=num[i].id;
else
ans[++tot]=num[i+1].id;
}
printf("%d\n",cnt);
for(int i=1;i<=cnt;i++)
printf("%d ",ans[i]);
return 0;
}
CF798D Mike and distribution的更多相关文章
- 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 ...
- #410div2D. Mike and distribution
D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- D. Mike and distribution 首先学习了一个玄学的东西
http://codeforces.com/contest/798/problem/D D. Mike and distribution time limit per test 2 seconds m ...
- Codeforces 798D Mike and distribution(贪心或随机化)
题目链接 Mike and distribution 题目意思很简单,给出$a_{i}$和$b_{i}$,我们需要在这$n$个数中挑选最多$n/2+1$个,使得挑选出来的 $p_{1}$,$p_{2} ...
- CF410div2 D. Mike and distribution
/* CF410div2 D. Mike and distribution http://codeforces.com/contest/798/problem/D 构造 题意:给出两个数列a,b,求选 ...
- [CF798D]Mike and distribution_贪心
Mike and distribution 题目链接:http://codeforces.com/problemset/problem/798/D 数据范围:略. 题解: 太难了吧这个题..... 这 ...
- Codeforces 798D Mike and distribution - 贪心
Mike has always been thinking about the harshness of social inequality. He's so obsessed with it tha ...
- 【算法系列学习】codeforces D. Mike and distribution 二维贪心
http://codeforces.com/contest/798/problem/D http://blog.csdn.net/yasola/article/details/70477816 对于二 ...
随机推荐
- C++ 标准库,可变参数模板。可变参数数量,可变参数类型【转】
#include <iostream> // 可变模板参数 // 此例:可以构造可变数量,可变类型的函数输入. // 摘自:https://www.cnblogs.com/qicosmos ...
- css3中calc、vw、vh、vmin、vmax 属性的应用及兼容性详解
一. calc()的使用什么是calc()? calc()从字面我们可以把他理解为一个函数function.其实calc是英文单词calculate(计算)的缩写,是css3的一个新增的功能,用来指定 ...
- ReactNative: ReactNative初始项目的结构
一.介绍 初学RN,一切皆新.在上篇中成功地创建并运行了一个React-Native项目,这个demo的基本结构都是系统已经创建好的,开发者在此结构下完成自己的开发即可.分别用Xcode和WebSto ...
- IT兄弟连 HTML5教程 HTML语言的语法 2
HTML颜色值的设置 大多数浏览器都支持颜色名集合,颜色值是一个关键字或一个RGB格式的数字,在网页中用得很多.仅仅有16种颜色名被W3C的HTML 4.0标准所支持,它们是:aqua.black.b ...
- 14-scrapy框架(CrawlSpider)
CrawlSpider介绍 CrawlSpider是Spider的一个子类,意味着拥有Spider的方法,以及自己的方法,更加高效简洁.其中最显著的功能就是"LinkExtractors&q ...
- appium应用切换以及toast弹出框处理
一.应用切换 应用切换的方法很简单,直接调用driver.start_activity()方法,传入app_package和app_activity参数,示例代码如下: from appium imp ...
- css文本省略号
这里记录下如何用CSS实现单行.多行文本溢出容器的时候用省略号代替溢出部分. 单行文本溢出容器时显示省略号的CSS实现方法 /* 规定当内容溢出元素框(容器)时隐藏 */ overflow: hidd ...
- Android高可用移动网络连接---(转载自http://wingjay.com/2019/01/16/mobile-network-connection/)
读者好,前面我们在 <Android 架构之网络连接与加速> 和<Android 架构之长连接技术>两篇文章中,讲解了 Http 短连接.TCP 长连接.连接复用与速度优化.数 ...
- 前端之移动端库和框架bootstrap
学习移动端场景下的js事件:制作移动端特效常用的js库:介绍移动端常用开发框架Bootstrap:介绍动态样式语言less.sass.stylus的基本使用. 移动端js事件 移动端的操作方式和PC端 ...
- 如何搭建属于自己的Web服务器
如今随着计算机和互联网技术的发展,上网现在已经不再是什么难事,打开浏览器,我们可以访问各种论坛站点,比如CSDN.博客园等,各种视频网站,例如爱奇艺,B站等.在网上我们可以写文章,看视频,购物,打游戏 ...