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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

codeforces 1040a (回文数的舞蹈)

發(fā)布時(shí)間:2023/12/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 codeforces 1040a (回文数的舞蹈) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原題鏈接
A. Palindrome Dance
A group of n dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they’ve studied their dancing moves and can’t change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future.

On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer’s suit is the same as the color of the rightmost dancer’s suit, the 2nd left is the same as 2nd right, and so on.

The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it’s possible, what’s the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending.
題目大意:這個(gè)題目本意就是回文串的應(yīng)用,在這之間還加入了價(jià)值的運(yùn)算,思維量不大,注意中間的一些細(xì)節(jié)就好了。。
代碼如下:

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std;int f[21]; int n,a,b;int main() {while(cin>>n>>a>>b){for(int i=0;i<n;i++) cin>>f[i];int sum=0;int flag=0;for(int i=0;i<n/2;i++){if(f[i]!=f[n-i-1]){if(!(f[i]==2||f[n-i-1]==2))//1 1 2 1 0這樣的就不可以了{flag=1;break;}else {if(f[i]!=2) sum+=(f[i]==0)?a:b;if(f[n-i-1]!=2) sum+=(f[n-i-1]==0)?a:b;}}else if(f[i]==f[n-i-1]&&f[i]==2) sum+=2*min(a,b);}if(n&1)/n為奇數(shù)的時(shí)候需要特判一下{if(f[n/2]==2) sum+=min(a,b);}if(flag) cout<<"-1"<<endl;else cout<<sum<<endl;}return 0; }

總結(jié)

以上是生活随笔為你收集整理的codeforces 1040a (回文数的舞蹈)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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