-
H - Секретный шифр
- Gym - 101967H
- 题意:给你0-9个数的 每个数字的可用个数,然后从这里面挑一些构造最大的一个数,这个数满足 任意连续三位
- 能够整除3,但是 当数字长度为1或2时是无所谓的,直接按大小输出即可,还有特判一下全为0的情况
-
#include<bits/stdc++.h> using namespace std; #define maxn 123 string str[maxn]; char sbhk,sblhk; int a[maxn],orz,cnt,b[maxn]; void dfs(int md,int len) { int tmod; for(int tq=9; tq>=0; tq--) if((tq+md)%3==0&&a[tq]>0) { a[tq]--; sbhk=tq+'0'; str[cnt]+=sbhk; tmod=(tq+str[cnt][len-1]-'0')%3; dfs(tmod,len+1); return; } return ; } struct node { string hk; int len; bool operator<(const node &c)const { if(len==c.len)return hk>c.hk; return len>c.len; } } ok[maxn]; int main() { for(int i=0; i<10; i++) { cin>>a[i]; b[i]=a[i]; } for(int i=1; i<=9; i++) { for(int lhk=0; lhk<10; lhk++) a[lhk]=b[lhk]; if(a[i]==0)continue; b[i]--; sblhk='0'+i; for(int j=0; j<=9; j++) { str[++cnt]+=sblhk; for(int lhk=0; lhk<10; lhk++) a[lhk]=b[lhk]; if(a[j]==0)continue; a[j]--; sbhk='0'+j; str[cnt]+=sbhk; orz=(i+j)%3; dfs(orz,2); } b[i]++; } for(int i=1; i<=cnt; i++) { ok[i].hk=str[i]; ok[i].len=str[i].size(); } if(cnt==0)cout<<0<<endl; else { sort(ok+1,ok+cnt+1); cout<<ok[1].hk<<endl; } return 0; }
Problem H. Secret Code-构造-贪心
最新推荐文章于 2024-07-09 01:32:36 发布