Kick Start Round D 2020(前两题)
https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ff08?scoreboard_type=China
A.当前数比前面所有和后面一个都大的时候,算做破纪录一次。遍历一遍。
#include <bits/stdc++.h> using namespace std; vector<int> visiter(1000005,0); int main() { int T,N; int currmax=-1; int res=0; cin>>T; for(int i=0;i<T;i++){ currmax=-1; res=0; cin>>N; for(int j=0;j<N;j++){ cin>>visiter[j]; } cout<<"Case #"<<i+1<<": "; if(N==1){ cout<<1; }else{ for(int j=0;j<N-1;j++){ if(visiter[j]>currmax && visiter[j]>visiter[j+1]){ res++; } currmax=max(currmax,visiter[j]); } if(visiter[N-1]>currmax) res++; cout<<res; } cout<<endl; } return 0; }
B.将多音高的音乐转换成只有4个音高的音乐,问最小破坏规则的次数。动态规划
#include <bits/stdc++.h> using namespace std; int notes[10005]; int dp[10005][4]; int main() { int T,K; dp[0][0]=0; dp[0][1]=0; dp[0][2]=0; dp[0][3]=0; int res=0; cin>>T; for(int i=0;i<T;i++){ cin>>K; for(int j=0;j<K;j++){ cin>>notes[j]; } cout<<"Case #"<<i+1<<": "; for(int j=1;j<K;j++){ if(notes[j]>notes[j-1]){ dp[j][0]=min(min(dp[j-1][0],dp[j-1][1]),min(dp[j-1][2],dp[j-1][3]))+1; dp[j][1]=min(min(dp[j-1][0],dp[j-1][1]+1),min(dp[j-1][2]+1,dp[j-1][3]+1)); dp[j][2]=min(min(dp[j-1][0],dp[j-1][1]),min(dp[j-1][2]+1,dp[j-1][3]+1)); dp[j][3]=min(min(dp[j-1][0],dp[j-1][1]),min(dp[j-1][2],dp[j-1][3]+1)); }else if(notes[j]==notes[j-1]){ dp[j][0]=min(min(dp[j-1][0],dp[j-1][1]+1),min(dp[j-1][2]+1,dp[j-1][3]+1)); dp[j][1]=min(min(dp[j-1][0]+1,dp[j-1][1]),min(dp[j-1][2]+1,dp[j-1][3]+1)); dp[j][2]=min(min(dp[j-1][0]+1,dp[j-1][1]+1),min(dp[j-1][2],dp[j-1][3]+1)); dp[j][3]=min(min(dp[j-1][0]+1,dp[j-1][1]+1),min(dp[j-1][2]+1,dp[j-1][3])); }else if(notes[j]<notes[j-1]){ dp[j][0]=min(min(dp[j-1][0]+1,dp[j-1][1]),min(dp[j-1][2],dp[j-1][3])); dp[j][1]=min(min(dp[j-1][0]+1,dp[j-1][1]+1),min(dp[j-1][2],dp[j-1][3])); dp[j][2]=min(min(dp[j-1][0]+1,dp[j-1][1]+1),min(dp[j-1][2]+1,dp[j-1][3])); dp[j][3]=min(min(dp[j-1][0],dp[j-1][1]),min(dp[j-1][2],dp[j-1][3]))+1; } } cout<<min(min(dp[K-1][0],dp[K-1][1]),min(dp[K-1][2],dp[K-1][3])); cout<<endl; } return 0; }