Codeforces 798D:Mike and distribution
2017-04-22 11:53
295 查看
[b]Codeforces 798D:Mike and distributio[/b]
题目链接: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)$.
代码如下:
题目链接: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)$.
代码如下:
#include <cstdio> #include <algorithm> #define N 100005 using namespace std; typedef long long ll; int n,a ,b ,p ; bool cmp(int x,int y){ return a[x]>a[y]; } int main(void){ scanf("%d",&n); for(int i=0;i<n;++i)scanf("%d",&a[i]); for(int i=0;i<n;++i)scanf("%d",&b[i]); for(int i=0;i<n;++i)p[i]=i; sort(p,p+n,cmp); printf("%d\n",n/2+1); printf("%d",p[0]+1); for(int i=1;2*i<n;++i){ int x=p[2*i-1],y=p[2*i]; if(b[x]<b[y])printf(" %d",y+1); else printf(" %d",x+1); } if(n%2==0)printf(" %d",p[n-1]+1); }
相关文章推荐
- 【codeforces 798D】Mike and distribution
- codeforces 798 D. Mike and distribution 二维贪心
- codeforces 798 D. Mike and distribution 二维贪心
- codeforces 798 D. Mike and distribution 二维贪心
- codeforces 798 D. Mike and distribution 二维贪心
- codeforces 798 D. Mike and distribution 二维贪心
- codeforces 798 D. Mike and distribution 二维贪心
- codeforces 798 D. Mike and distribution 二维贪心
- codeforces 798 D. Mike and distribution 二维贪心
- codeforces 798 D. Mike and distribution 二维贪心
- codeforces 798 D. Mike and distribution 二维贪心
- codeforces 798 D. Mike and distribution 二维贪心
- Codeforces-798D. Mike and distribution
- codeforces 798 D. Mike and distribution 二维贪心
- codeforces 798 D. Mike and distribution 二维贪心
- codeforces 798B - Mike and strings
- 【codeforces 798B】Mike and strings
- Codeforces 798C:Mike and gcd problem【数论+贪心】
- Codeforces 798C:Mike and gcd problem
- CF - 798D. Mike and distribution - 排序+数学