Educational Codeforces Round 82 (Rated for Div. 2) A-E代码(暂无记录题解)
A. Erasing Zeroes (模拟)
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 1e5+5; 5 int main(){ 6 int t;cin>>t; 7 while(t--){ 8 string s;cin>>s; 9 bool f1 = 0,f2 = 0; 10 int cnt = 0; 11 int ans = 0; 12 for(int i = 0;i<s.length();i++){ 13 if(s[i] == '1'){ 14 if(f1 == 1) { 15 ans+=cnt; 16 cnt = 0; 17 f1 = 1; 18 } 19 if(f1 == 0) f1 = 1; 20 } 21 else{ 22 if(f1 == 1) cnt++; 23 } 24 } 25 cout<<ans<<endl; 26 } 27 return 0; 28 }
B. National Project (数学题 周期计算)
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 1e5+5; 5 int main(){ 6 int t;cin>>t; 7 while(t--){ 8 ll n,g,b; 9 cin>>n>>g>>b; 10 ll tn = n; 11 if(n%2 == 1) n = n/2+1; 12 else n = n/2; 13 if(n<=g ) { 14 cout<<tn<<endl;continue; 15 } 16 else{ 17 ll t = g+b;//一个周期 18 ll c = n/g;//至少需要几个周期 19 if(n%g == 0) { 20 if(c*t-b<tn) cout<<tn<<endl; 21 else cout<<c*t-b<<endl; 22 continue; 23 } 24 ll d = n%g; 25 ll ans = c*t+d; 26 if(ans<tn) ans+=(tn-ans); 27 cout<<ans<<endl; 28 continue; 29 } 30 // 4 1 1 31 } 32 return 0; 33 }
C. Perfect Keyboard (dfs 图论)
1 #include<bits/stdc++.h> 2 using namespace std; 3 struct node{ 4 vector<int> v; 5 }g[26]; 6 string ans; 7 int vis[26]; 8 void init(){ 9 for(int i = 0;i<26;i++) g[i].v.clear(),vis[i] = 0; 10 ans = ""; 11 } 12 void dfs(int cur){ 13 vis[cur] = 1,ans+='a'+cur; 14 for(int i = 0;i<g[cur].v.size();i++){ 15 if(!vis[g[cur].v[i]]) dfs(g[cur].v[i]); 16 } 17 } 18 void solve(){ 19 string s; 20 cin>>s; 21 init(); 22 map<pair<int,int>,int > m; 23 for(int i = 1;i<s.length();i++){ 24 int a = s[i-1] - 'a',b = s[i] - 'a'; 25 if(a<b) swap(a,b); 26 if(m[{a,b}] == 0){ 27 m[{a,b}] = 1; 28 g[a].v.push_back(b); 29 g[b].v.push_back(a); 30 } 31 } 32 for(int i = 0;i<26;i++){ 33 if(g[i].v.size()>2) { 34 cout<<"NO"<<endl; 35 return; 36 } 37 } 38 for(int i = 0;i<26;i++){ 39 if(!vis[i] && g[i].v.size()<2){ 40 dfs(i); 41 } 42 } 43 if(ans.length() == 26) { 44 cout<<"YES"<<endl; 45 cout<<ans<<endl; 46 return; 47 } 48 else{ 49 cout<<"NO"<<endl; 50 } 51 } 52 int main(){ 53 int t; 54 cin>>t; 55 while(t--){ 56 solve(); 57 } 58 return 0; 59 }
D. Fill The Bag(贪心 二进制位运算 状态压缩)
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 1e5+5; 5 const int maxBit = 62; 6 int cnt[maxBit]; 7 void solve(){ 8 ll n;int m; 9 scanf("%lld%d",&n,&m); 10 memset(cnt,0,sizeof(cnt)); 11 ll sum = 0; 12 for(int i = 1;i<=m;i++) { 13 ll x; 14 scanf("%lld",&x); 15 sum+=x; 16 for(int j = 0;j<=maxBit;j++){ 17 if((x>>j)&1 == 1) { 18 cnt[j+1]++; 19 break; 20 } 21 } 22 } 23 if(sum<n) { 24 cout<<-1<<endl;return ; 25 } 26 int ans = 0; 27 for(int i = 1;i<=maxBit;i++){ 28 if((n>>(i-1))&1){ 29 if(!cnt[i]){ 30 int indx = -1; 31 for(int j = i;j<=maxBit;j++){ 32 if(cnt[j]) { 33 indx = j; 34 break; 35 } 36 } 37 while(indx!=i){ 38 cnt[indx]--,cnt[indx-1]+=2,ans++,indx--; 39 } 40 // n^=(1<<(i-1)); 41 } 42 cnt[i]--; 43 } 44 cnt[i+1] += cnt[i]/2; 45 } 46 cout<<ans<<endl; 47 } 48 // 49 int main(){ 50 int t; 51 cin>>t; 52 while(t--){ 53 solve(); 54 } 55 return 0; 56 }
E. Erase Subsequences (字符串上dp)
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn = 405; 4 int dp[maxn][maxn]; 5 bool check(string s,string t){ 6 int indx = 0; 7 for(int i = 0;i<s.length();i++){ 8 if(t[indx] == s[i]) indx++; 9 } 10 if(indx == t.length()) return true; 11 return false; 12 } 13 bool check(string s,string t1,string t2){ 14 memset(dp,-1,sizeof(dp)); 15 dp[0][0] = 0; 16 for(int i = 0;i<s.length();i++){ 17 for(int j = 0;j<=t1.size();j++){ 18 if(dp[i][j]<0) continue; 19 if(j<t1.size() && s[i] == t1[j]){ 20 dp[i+1][j+1] = max(dp[i+1][j+1],dp[i][j]); 21 } 22 if(dp[i][j]<t2.size() && s[i] == t2[dp[i][j]]){ 23 dp[i+1][j] = max(dp[i+1][j],dp[i][j]+1); 24 } 25 dp[i+1][j] = max(dp[i+1][j],dp[i][j]); 26 } 27 } 28 if(dp[s.length()][t1.length()] == t2.length()) return true; 29 return false; 30 } 31 void solve(){ 32 string s,t; 33 cin>>s>>t; 34 if(check(s,t)){ 35 cout<<"YES"<<endl; 36 return; 37 } 38 for(int i = 0;i<t.length()-1;i++){ 39 string t1 = t.substr(0,i+1); 40 string t2 = t.substr(i+1,t.size()); 41 if(check(s,t1,t2)){ 42 cout<<"YES"<<endl; 43 return; 44 } 45 } 46 cout<<"NO"<<endl; 47 return; 48 } 49 int main(){ 50 int t; 51 cin>>t; 52 while(t--){ 53 solve(); 54 } 55 return 0; 56 }