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

歡迎訪問(wèn) 生活随笔!

生活随笔

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

编程问答

【CodeForces - 768C】Jon Snow and his Favourite Number(思维,技巧,套路,数学异或,循环节,trick)

發(fā)布時(shí)間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 768C】Jon Snow and his Favourite Number(思维,技巧,套路,数学异或,循环节,trick) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

Jon Snow now has to fight with White Walkers. He has?n?rangers, each of which has his own strength. Also Jon Snow has his favourite number?x. Each ranger can fight with a white walker only if the strength of the white walker equals his strength. He however thinks that his rangers are weak and need to improve. Jon now thinks that if he takes the bitwise XOR of strengths of some of rangers with his favourite number?x, he might get soldiers of high strength. So, he decided to do the following operation?k?times:

  • Arrange all the rangers in a straight line in the order of increasing strengths.
  • Take the bitwise XOR (is written as?) of the strength of each alternate ranger with?x?and update it's strength.
  • Suppose, Jon has?5?rangers with strengths?[9,?7,?11,?15,?5]?and he performs the operation?1?time with?x?=?2. He first arranges them in the order of their strengths,[5,?7,?9,?11,?15]. Then he does the following:

  • The strength of first ranger is updated to?, i.e.?7.
  • The strength of second ranger remains the same, i.e.?7.
  • The strength of third ranger is updated to?, i.e.?11.
  • The strength of fourth ranger remains the same, i.e.?11.
  • The strength of fifth ranger is updated to?, i.e.?13.
  • The new strengths of the?5?rangers are?[7,?7,?11,?11,?13]

    Now, Jon wants to know the maximum and minimum strength of the rangers after performing the above operations?k?times. He wants your help for this task. Can you help him?

    Input

    First line consists of three integers?n,?k,?x?(1?≤?n?≤?105,?0?≤?k?≤?105,?0?≤?x?≤?103) — number of rangers Jon has, the number of times Jon will carry out the operation and Jon's favourite number respectively.

    Second line consists of?n?integers representing the strengths of the rangers?a1,?a2,?...,?an?(0?≤?ai?≤?103).

    Output

    Output two integers, the maximum and the minimum strength of the rangers after performing the operation?k?times.

    Examples

    Input

    5 1 2 9 7 11 15 5

    Output

    13 7

    Input

    2 100000 569 605 986

    Output

    986 605

    題目大意:

    ? ?給你一組數(shù),進(jìn)行k組操作,每組操作中包含兩個(gè)步驟,首先排序,然后對(duì)下標(biāo)為奇數(shù)的數(shù)字進(jìn)行異或操作。最后問(wèn)你進(jìn)行k組操作之后的數(shù)組中的最大值和最小值。

    解題報(bào)告:

    不是我就想知道這題打死也想不到會(huì)出現(xiàn)循環(huán)節(jié)的情況吧??!!?這也太、、可能這是異或運(yùn)算的一種特性?反正我是第一次遇到,題目中會(huì)猜他存在循環(huán)節(jié)然后進(jìn)行暴力的。。。然后,循環(huán)節(jié)是4不是2、所以其實(shí)代碼中的(ci-3)是不需要進(jìn)行判斷的。(所以對(duì)于這種不知道循環(huán)節(jié)是幾的這種題,保險(xiǎn)起見(jiàn)建議那種精彩代碼,直接Node結(jié)構(gòu)體記錄曾經(jīng)算過(guò)的狀態(tài),并且用vector存下來(lái)!!技巧啊)

    AC代碼:

    #include<bits/stdc++.h> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; int a[MAX]; int mx[MAX],mi[MAX]; int main() {int n,k,x,ci;cin>>n>>k>>x;mx[0]=0,mi[0]=100005;for(int i = 1; i<=n; i++) scanf("%d",a+i),mx[0] = max(mx[0],a[i]),mi[0] = min(mi[0],a[i]);for(ci = 1; ci<=k; ci++) {sort(a+1,a+n+1);mx[ci] = -100005;//a[n];是錯(cuò)的 mi[ci] = 100005;//a[1];是錯(cuò)的 for(int i = 1; i<=n; i++) {if(i&1) a[i] = a[i] ^ x;mi[ci] = min(a[i],mi[ci]);mx[ci] = max(a[i],mx[ci]);}if(ci > 4) {if(mi[ci] == mi[ci-1] && mi[ci] == mi[ci-2] && mi[ci] == mi[ci-3] && mi[ci] == mi[ci-4])if(mx[ci] == mx[ci-1] && mx[ci] == mx[ci-2] && mx[ci] == mx[ci-3] && mx[ci] == mx[ci-4]) {printf("%d %d\n",mx[ci],mi[ci]);return 0 ;}}} // sort(a+1,a+n+1);printf("%d %d\n",mx[ci-1],mi[ci-1]);return 0 ;}

    1WA代碼:

    ? ?需要i++,而不是i+=2直接做,因?yàn)槟阆氚?#xff0c;你這一輪操作的mx和mi,都是所有數(shù)字的,而不是選出來(lái)那些數(shù)的啊!!另外,不能直接每次都mx[ci] = a[n]這樣,因?yàn)檫@一輪操作之后可能就沒(méi)有這個(gè)最大值了,可能就被覆蓋成別的值了。,。。所以說(shuō)啊,寫(xiě)代碼的時(shí)候一定要注意是否代碼中存在小問(wèn)題,這些問(wèn)題疊加到一塊就很致命了!!

    #include<bits/stdc++.h> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; int a[MAX]; int mx[MAX],mi[MAX]; int main() {int n,k,x,ci;cin>>n>>k>>x;mx[0]=0,mi[0]=100005;for(int i = 1; i<=n; i++) scanf("%d",a+i),mx[0] = max(mx[0],a[i]),mi[0] = min(mi[0],a[i]);for(ci = 1; ci<=k; ci++) {sort(a+1,a+n+1);mx[ci] = -100005;//a[n];是錯(cuò)的 mi[ci] = 100005;//a[1];是錯(cuò)的 for(int i = 1; i<=n; i+=2) {//這樣寫(xiě)應(yīng)該也是錯(cuò)的 a[i] = a[i] ^ x;mi[ci] = min(a[i],mi[ci]);mx[ci] = max(a[i],mx[ci]);}if(ci > 4) {if(mi[ci] == mi[ci-1] && mi[ci] == mi[ci-2] && mi[ci] == mi[ci-3] && mi[ci] == mi[ci-4])if(mx[ci] == mx[ci-1] && mx[ci] == mx[ci-2] && mx[ci] == mx[ci-3] && mx[ci] == mx[ci-4]) {printf("%d %d\n",mx[ci],mi[ci]);return 0 ;}}}sort(a+1,a+n+1);printf("%d %d\n",a[n],a[1]);return 0 ;}

    ?

    另一個(gè)精彩的代碼:

    把每次變化的都存起來(lái)。然后得到一個(gè)新的狀態(tài)的時(shí)候,先去跟之前存起來(lái)的那些狀態(tài)比較,如果發(fā)現(xiàn)又相同的狀態(tài),那么就表示找到了循環(huán)節(jié)了。

    之后求出來(lái)循環(huán)節(jié)的長(zhǎng)度,然后取模,得到最后的答案。

    (代碼中可以直接return 0的,不需要goto,因?yàn)槭莄odeforce的題。、。。)

    #include <bits/stdc++.h>using namespace std; const int MAXN=1e5+7; int n,k,x; int cnt; struct node { //用來(lái)存儲(chǔ)已經(jīng)出現(xiàn)過(guò)的狀態(tài)int num[MAXN];int MAX;int MIN;node() {MAX=0;MIN=1e9;}bool operator ==(const node &a)const {for(int i=0; i<n; ++i) {if(a.num[i]!=num[i])return 0;}return 1;} } p; vector<node>q; int check() {for(int i=0; i<cnt; ++i) {if(q[i]==p)return i;}return -1; } int main() {int i; xx:while(~scanf("%d%d%d",&n,&k,&x)) {q.clear();p.MAX=0;p.MIN=1e9;for(i=0; i<n; ++i) {scanf("%d",&p.num[i]);p.MAX=max(p.MAX,p.num[i]);p.MIN=min(p.MIN,p.num[i]);}sort(p.num,p.num+n);q.push_back(p);cnt=0;int pos;while(cnt<k) {cnt++;p.MAX=0;p.MIN=1e9;for(i=0; i<n; i+=2) {p.num[i]^=x;}for(i=0; i<n; ++i) {p.MAX=max(p.MAX,p.num[i]);p.MIN=min(p.MIN,p.num[i]);}sort(p.num,p.num+n);pos=check();if(pos!=-1) { //找到循環(huán)節(jié)了int t=cnt-pos;//循環(huán)節(jié)長(zhǎng)度k=(k-pos)%t+pos;//取模之后的答案的下標(biāo)printf("%d %d\n",q[k].MAX,q[k].MIN);goto xx;//找到了就直接重新讀取下一組}q.push_back(p);}printf("%d %d\n",p.MAX,p.MIN);//表示沒(méi)有找到循環(huán)節(jié)}return 0; }

    ?

    總結(jié)

    以上是生活随笔為你收集整理的【CodeForces - 768C】Jon Snow and his Favourite Number(思维,技巧,套路,数学异或,循环节,trick)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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