日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 798D Mike and distribution(构造+思维/玄学随机数)

發(fā)布時間:2024/4/11 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 798D Mike and distribution(构造+思维/玄学随机数) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出兩個長度為n的數(shù)列,現(xiàn)在要求選出n/2+1個位置,使得兩個序列中這些位置的和分別大于各自序列之和的一半

題目分析:題意換句話說,是需要讓我們從數(shù)組中選出一半,要大于另一半,這樣我們可以以數(shù)組a為基準(zhǔn)先從大到小排序,然后依次對于每兩個相鄰的b比較大小,選擇較大的那個輸出,因為此時數(shù)組a已經(jīng)是降序排列的了,只需要讓數(shù)組b滿足條件就好了

比較讓我驚艷到的是隨機數(shù)的玄學(xué)做法,直接用STL里的random_shuffle函數(shù)將原序列隨機排列,直到前n/2+1個數(shù)滿足條件輸出即可

代碼:

排序:

#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cstring> #include<algorithm> #include<stack> #include<queue> #include<map> #include<set> #include<cmath> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;struct Node {int a,b,id;bool operator<(const Node& t)const{return a>t.a;} }q[N];int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int n;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&q[i].a);q[i].id=i;}for(int i=1;i<=n;i++)scanf("%d",&q[i].b);sort(q+1,q+1+n);printf("%d\n",n/2+1);printf("%d ",q[1].id);for(int i=2;i<=n;i+=2){if(q[i].b>q[i+1].b)printf("%d ",q[i].id);elseprintf("%d ",q[i+1].id);}return 0; }

隨機數(shù):

#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cstring> #include<algorithm> #include<stack> #include<queue> #include<map> #include<set> #include<cmath> #include<sstream> #include<unordered_map> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;struct Node {int a,b,id; }q[N];int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int n;scanf("%d",&n);LL suma=0,sumb=0;for(int i=1;i<=n;i++){scanf("%d",&q[i].a);suma+=q[i].a;q[i].id=i;}for(int i=1;i<=n;i++){scanf("%d",&q[i].b);sumb+=q[i].b;}while(1){random_shuffle(q+1,q+1+n);LL tempa=0,tempb=0;for(int i=1;i<=n/2+1;i++){tempa+=q[i].a;tempb+=q[i].b;}if(tempa*2>suma&&tempb*2>sumb){printf("%d\n",n/2+1);for(int i=1;i<=n/2+1;i++)printf("%d ",q[i].id);return 0;}}return 0; }

總結(jié)

以上是生活随笔為你收集整理的CodeForces - 798D Mike and distribution(构造+思维/玄学随机数)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。