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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 946E Largest Beautiful Number(贪心+模拟)

發布時間:2024/4/11 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 946E Largest Beautiful Number(贪心+模拟) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個數位長度為偶數的數字 n,需要求出一個比 n 小的,且所有數位重新排列后可以形成回文串,要求這個數字盡可能大

題目分析:從最低位開始貪心,依次枚舉每一位的數字,然后貪心去給低位重新分配以及補充即可,因為題目保證了數位的長度為偶數,所以此題中回文串的條件只是單純約束了所有數位出現次數為偶數即可,不可能為奇數

唯一需要注意的就是對于最高位的取值范圍內不能出現 0,因為不允許有前導零

代碼:
?

//#pragma GCC optimize(2) //#pragma GCC optimize("Ofast","inline","-ffast-math") //#pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=1e6+100;char s[N];int cnt[10];int main() { #ifndef ONLINE_JUDGE // freopen("data.ans.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--){scanf("%s",s+1);int n=strlen(s+1);memset(cnt,0,sizeof(cnt));for(int i=1;i<=n;i++)cnt[s[i]-'0']++;for(int i=n;i>=1;i--){cnt[s[i]-'0']--;int limit=i==1?1:0;//不能有前導零 for(int j=s[i]-'0'-1;j>=limit;j--){cnt[j]++;vector<int>node;for(int k=9;k>=0;k--)if(cnt[k]&1)node.push_back(k);if(node.size()<=n-i){for(int k=1;k<i;k++)putchar(s[k]);printf("%d",j);for(int k=1;k<=n-i-node.size();k++)putchar('9');for(auto it:node)printf("%d",it);puts("");goto end;}cnt[j]--;}}for(int i=0;i<n-2;i++)putchar('9');puts("");end:;}return 0; }

?

總結

以上是生活随笔為你收集整理的CodeForces - 946E Largest Beautiful Number(贪心+模拟)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。