Codeforces 798D Mike and distribution (构造)

题目链接

http://codeforces.com/contest/798/problem/D

题解

前几天的模拟赛,居然出这种智商题。。被打爆了QAQ

这个的话,考虑只有一个序列怎么做,把所有的排序取最大的当然可以,但是还有一种做法,就是两两分组之后每两个相邻的取大的!!
于是按照\(a\)排序,先取第一个,后面每两个相邻的取b较大的。做完了!

哇这怎么想出来的啊。。。。

代码

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
 
const int N = 1e5;
int a[N+3],b[N+3],permu[N+3];
vector<int> ans;
int n;
 
bool cmp(int x,int y) {return a[x]>a[y];}
 
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++) scanf("%d",&a[i]);
    for(int i=1; i<=n; i++) scanf("%d",&b[i]);
    for(int i=1; i<=n; i++) permu[i] = i;
    sort(permu+1,permu+n+1,cmp);
    ans.push_back(permu[1]);
    for(int i=2; i<=n; i+=2)
    {
        ans.push_back(b[permu[i]]>b[permu[i+1]] ? permu[i] : permu[i+1]);
    }
    printf("%d\n",ans.size());
    for(int i=0; i<ans.size(); i++) printf("%d ",ans[i]); puts("");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/suncongbo/p/11330571.html
今日推荐