Code forces798D(随机排列函数random_shuffle的应用)

来源:互联网 发布:百度地图js api离线 编辑:程序博客网 时间:2024/06/05 19:45

利用random_shuffle进行答案的枚举,头文件是algorithm,找到符合的就


#include<bits/stdc++.h>typedef long long ll;using namespace std;const int maxn = 1e5 + 10;int a[maxn];int b[maxn];int c[maxn];int main(){    int n;    while( ~ scanf("%d",&n))    {        ll sum1 = 0,sum2 = 0;        for(int i = 1; i <= n; i ++)            scanf("%d",&a[i]),sum1 += a[i];        for(int i = 1; i <= n; i ++)            scanf("%d",&b[i]),sum2 += b[i];        for(int i = 1; i <= n; i ++)c[i] = i;        while(true)        {            int m = n /2 + 1;            ll cnt1 = 0,cnt2 = 0;            for(int i = 1; i <= m; i ++)            {//                c[i] = rand() % n + 1;                cnt1 += a[c[i]];cnt2 += b[c[i]];            }            if(cnt1 * 2 > sum1 && cnt2 * 2 > sum2)            {                printf("%d\n",m);                for(int i = 1; i <= m; i ++)                    printf("%d%c",c[i],i < m ? ' ': '\n');break;            }            random_shuffle(c + 1,c + n + 1);        }    }    return 0;}


原创粉丝点击