codeforces 798 D. Mike and distribution
D. Mike and distribution
2 seconds
256 megabytes
standard input
standard output
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] andB = [b1, b2, ..., bn] of length n 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 k numbers P = [p1, p2, ..., pk] such that 1 ≤ pi ≤ n for 1 ≤ i ≤ k and elements in Pare distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P"unfair" if and only if the following conditions are satisfied: 2·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to because it will be to easy to find sequence P 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!
2 seconds
256 megabytes
standard input
standard output
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] andB = [b1, b2, ..., bn] of length n 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 k numbers P = [p1, p2, ..., pk] such that 1 ≤ pi ≤ n for 1 ≤ i ≤ k and elements in Pare distinct. Sequence P will represent indices of elements that you'll select from both sequences. He calls such a subset P"unfair" if and only if the following conditions are satisfied: 2·(ap1 + ... + apk) is greater than the sum of all elements from sequence A, and 2·(bp1 + ... + bpk) is greater than the sum of all elements from the sequence B. Also, k should be smaller or equal to because it will be to easy to find sequence P 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 n (1 ≤ n ≤ 105) — the number of elements in the sequences.
On the second line there are n space-separated integers a1, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.
On the third line there are also n space-separated integers b1, ..., bn (1 ≤ bi ≤ 109) — elements of sequence B.
On the first line output an integer k which represents the size of the found subset. k should be less or equal to .
On the next line print k integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the elements of sequence P. You can print the numbers in any order you want. Elements in sequence P should be distinct.
5
8 7 4 8 3
4 2 5 3 7
3
1 4 5
解题思路:
这道题题意有点难描述,而且不难看懂就不多说了。
这道题难点就在于有两个数组,难以判断。 所以我们这里先讨论只有一个数组时的情况,如何使取得的数字大于剩下的?其实只要两两之间相互比较,取最大的,当序列长度为奇数时,我们可以先提出
其中一个,然后将剩余的两两比较,依旧必大于剩余的。
单个数组的问题讨论完了,然后分析两个数组,这里我们可以将其中一个数组排序用另一个数组记录排序结果(下标)id[],对下一个数组进行排序,这么说有点抽象,我把具体实现情况写一下
a[0] = 8 ,a[1] = 7,a[2] = 4, a[3] = 8,a[4] = 3;
b[0] = 4,a[1] = 2,a[2] = 5, a[3] = 3,a[4] = 7;
id[0] = 0,id[1] = 1,id[2] = 2,id[3] = 3,id[4] = 4;
先将a数组排序从大到小并用id数组记录结果(下标)
a数组排序结果 : a0 a3 a1 a2 a4
用id数组记录:id[0] = 0,id[1] = 3,id[2] = 1,id[3] = 2,id[4] = 4;
这里可以用id对b数组进行排序,因为现在id储存的是a从大到小的顺序,按这个顺序排序a必大于剩余的,
排序过程: 因为是奇数,先取出第一个 b[0] 然后取 max(b[3],b[1]),max(b[2],b[4]),这样就可以得出满足两个数组的下标序列
实现代码:
#include<bits/stdc++.h>
using namespace std;
const int Max = +;
int m,id[Max];
long long a[Max],b[Max];
inline bool cmp(const int &x,const int &y)
{
return a[x] > a[y];
}
int main()
{
int i;
cin>>m;
for(i=;i<m;i++) cin>>a[i];
for(i=;i<m;i++) cin>>b[i];
for(i=;i<m;i++) id[i] = i;
sort(id,id+m,cmp);
int n = m/ + ;
cout<<n<<endl;
cout<<id[]+;
for(i=;i<m-;i+=){
if(b[id[i]]>b[id[i+]])
cout<<" "<<id[i]+;
else
cout<<" "<<id[i+]+;
}
if(m%==)
cout<<" "<<id[m-]+;
cout<<endl;
return ;
}
codeforces 798 D. Mike and distribution的更多相关文章
- codeforces 798 D. Mike and distribution(贪心+思维)
题目链接:http://codeforces.com/contest/798/problem/D 题意:给出两串长度为n的数组a,b,然后要求长度小于等于n/2+1的p数组是的以p为下表a1-ap的和 ...
- codeforces 798 C. Mike and gcd problem(贪心+思维+数论)
题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...
- 【codeforces 798D】Mike and distribution
[题目链接]:http://codeforces.com/contest/798/problem/D [题意] 让你选一个下标集合 p1,p2,p3..pk 使得2*(a[p1]+a[p2]+..+a ...
- Codeforces 798 B. Mike and strings-String的find()函数
好久好久好久之前的一个题,今天翻cf,发现这个题没过,补一下. B. Mike and strings time limit per test 2 seconds memory limit per t ...
- Codeforces 798D Mike and distribution(贪心或随机化)
题目链接 Mike and distribution 题目意思很简单,给出$a_{i}$和$b_{i}$,我们需要在这$n$个数中挑选最多$n/2+1$个,使得挑选出来的 $p_{1}$,$p_{2} ...
- D. Mike and distribution 首先学习了一个玄学的东西
http://codeforces.com/contest/798/problem/D D. Mike and distribution time limit per test 2 seconds m ...
- CF410div2 D. Mike and distribution
/* CF410div2 D. Mike and distribution http://codeforces.com/contest/798/problem/D 构造 题意:给出两个数列a,b,求选 ...
- #410div2D. Mike and distribution
D. Mike and distribution time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces 547C/548E - Mike and Foam 题解
目录 Codeforces 547C/548E - Mike and Foam 题解 前置芝士 - 容斥原理 题意 想法(口胡) 做法 程序 感谢 Codeforces 547C/548E - Mik ...
随机推荐
- 如何利用Skyline的TerraExplorer Pro 6.5提供的API接口实现矢量图层数据的动态投影
Skyline 支持国内常见的地图投影坐标系,包括WGS84.Beijing54.西安80.2000坐标系等,也可以自定义坐标系,比如一些做过参数变换加密的坐标系等. <!DOCTYPE htm ...
- linux驱动编写之中断处理
一.中断 1.概念 学过单片机的应该非常清楚中断的概念,也就是CPU在正常执行程序过程中,出现了突发事件(中断事件),于是CPU暂停当前程序的执行,转去处理突发事件.处理完毕后,CPU又返回被中断的程 ...
- 使用gulp和bable实现实时编译ES6代码
这篇文章最初发表在我自己折腾的博客站点上:使用gulp和bable实现实时编译ES6代码,该博客用了一位前辈开源的源码,基于thinkjs和vuejs开发,欢迎大家来逛逛. 问题描述> 项目开发 ...
- QueryHelper
[1].[代码] QueryHelper.java 跳至 [1] package my.db; import java.io.Serializable; import java.math.BigInt ...
- 浅谈MySQL引擎(纯个人理解,如有错误请指正)
MySQL有很多引擎,MyISAM.InnoDB.MERGE.MEMORY(HEAP).BDB(BerkeleyDB).EXAMPLE.FEDERATED...等等 比较常用的就是InnoDB和MyI ...
- [Spark][Hive][Python][SQL]Spark 读取Hive表的小例子
[Spark][Hive][Python][SQL]Spark 读取Hive表的小例子$ cat customers.txt 1 Ali us 2 Bsb ca 3 Carls mx $ hive h ...
- python报错问题解决:'ascii' codec can't encode character
之前部署了openstack虚拟化环境,有一天在使用nova list查看虚拟机的时候,突然报错!如下: [root@linux-node1 src]# nova listERROR (Unicode ...
- 【MOOC EXP】Linux内核分析实验八报告
程涵 原创博客 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 进程的切换和系统的一般执行过程 知识点 ...
- Linux内核分析第八周总结
第八章 进程的切换和系统的一般执行过程 进程调度与进程调度的时机分析 第一种分类: I/O密集型(I/O-bound):频繁的进行I/O,通常会花费很多时间等待I/O操作的完成 CPU密集型(CPU- ...
- jeecg的下拉列表
jeecg里面下拉列表的使用 ①建立数据字典seo_id <t:dictSelect field="operationPromotionAccount" typeGroupC ...