Codeforces 798D:Mike and distribution

Codeforces 798D:Mike and distributio

题目链接:http://codeforces.com/problemset/problem/798/D

题目大意:给出两个大小为$n$的数列$A,B$,现要求从这两个数列相同位置取出$K(K \leqslant n/2+1)$个数,使得$2 \times subA>sumA$且$2 \times subB>sumB$.

想法题

我们需要从数列$A$和数列$B$中取出$K$个数,使得这$K$个数的和比剩下$n-K$个数的和大,显然$K=n/2+1$.

考虑一个大小为偶数$m$的数组,将数组分成$m/2$组,每两个数取较大数,则取出的数的和一定比剩下的数的和大.

讨论$n$为奇数的情况,将数组$A$降序排序,先取出最大的$A_x$和相对应的$B_y$。

剩下$m=n-1$($m$为偶数)个数,按顺序两两分成一组,每两个数取对应数组$B$中较大数,保证了$subB>sumB-subB$,而由$A$数组的单调性,不难得出$subA>sumA-subA$.

同理可以证明$n$为偶数时算法的正确性.

复杂度为$O(nlgn)$.

代码如下:

 1 #include <cstdio>
 2 #include <algorithm>
 3 #define N 100005
 4 using namespace std;
 5 typedef long long ll;
 6 int n,a[N],b[N],p[N];
 7 bool cmp(int x,int y){
 8     return a[x]>a[y];
 9 }
10 int main(void){
11     scanf("%d",&n);
12     for(int i=0;i<n;++i)scanf("%d",&a[i]);
13     for(int i=0;i<n;++i)scanf("%d",&b[i]);
14     for(int i=0;i<n;++i)p[i]=i;
15     sort(p,p+n,cmp);
16     printf("%d\n",n/2+1);
17     printf("%d",p[0]+1);
18     for(int i=1;2*i<n;++i){
19         int x=p[2*i-1],y=p[2*i];
20         if(b[x]<b[y])printf(" %d",y+1);
21         else printf(" %d",x+1);
22     }
23     if(n%2==0)printf(" %d",p[n-1]+1);
24 }
时间: 2024-10-18 20:30:00

Codeforces 798D:Mike and distribution的相关文章

CodeForces 547E:Mike and Friends(AC自动机+DFS序+主席树)

What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercase English letters. What is double strange that a phone number can be associated with several bears! In that country there is a rock band called CF con

Codeforces 798D Mike and distribution(贪心或随机化)

题目链接 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_

CF Round410 D. Mike and distribution

D. Mike and distribution 构造法 798D - Mike and distribution In the beginning, it's quite easy to notice that the condition " 2·(ap1?+?...?+?apk) is greater than the sum of all elements in A " is equivalent to " ap1?+?...?+?apk is greater than

Codeforces 449D:Jzzhu and Numbers

Codeforces 449D:Jzzhu and Numbers 题目链接:http://codeforces.com/problemset/status?friends=on 题目大意:给出$n$个数,求有多少种组合使得$a_{i_1}\&a_{i_2}\&...\&a_{i_k}=0(0 \leqslant i < n)$,答案对$10^9+7$取模. 容斥原理+DP 设位与$(\&)$后二进制表示中有$k$个$1$的组合数为$A_k$,则有, $A_0=$所有

Codeforces 757B:Bash&#39;s Big Day(分解因子+Hash)

http://codeforces.com/problemset/problem/757/B 题意:给出n个数,求一个最大的集合并且这个集合中的元素gcd的结果不等于1. 思路:一开始把素数表打出来,发现有9k+个数,不能暴力枚举.发现O(sqrt(n)*n)似乎可行,就枚举因子,然后出现过的因子就在Hash[]加1,最后枚举素数表里的元素,找出现次数最多的,因为那些数都可以映射在素数表里面.注意最少的ans是1. 1 #include <cstdio> 2 #include <algo

Codeforces 754E:Dasha and cyclic table

Codeforces 754E:Dasha and cyclic table 题目链接:http://codeforces.com/problemset/problem/754/E 题目大意:$A$矩阵($size(A)=n \times m$,仅含'a'-'z')在整个平面做周期延拓,问$B$矩阵($size(B)=r \times c$,包含'a'-'z'及'?','?'为通配符)在哪些位置能与$A$矩阵匹配.输出$n \times m$的01矩阵,1表示在该位置匹配. 枚举+bitset常

[转帖]SAP一句话入门:Sales and Distribution

SAP一句话入门:Sales and Distribution http://blog.vsharing.com/MilesForce/A616565.html SD是Sales and Distribution的简称.在SAP系统中,销售与分销模块处在供应链下游,关注从客户订单到向客户收款的全过程. SD模块中的Sales好理解,而Distribution却容易被忽视.用直白的话来讲,SD模块就是解决“卖”的问题,它关注的范畴有: 1,谁来卖? 2,卖什么? 3,卖给谁? 4,什么样的价格?

Codeforces 855B:Marvolo Gaunt&#39;s Ring(枚举,前后缀)

B. Marvolo Gaunt's Ring Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is sti

Codeforces 798D 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 = [a1, a2, ..., an] and B = [b1,