首页 > 代码库 > May Challenge 2017

May Challenge 2017

Chef and his daily routine

分析:水题,设置优先级,判断如果后面小于前面就输出no

技术分享
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "string"
 5 using namespace std;
 6 const int maxn=10000+10;
 7 string s;
 8 int T;
 9 int judge(char s){
10     if(s==C)
11         return 1;
12     else if(s==E)
13         return 2;
14     else
15         return 3;
16 }
17 int main()
18 {
19     cin>>T;
20     while(T--){
21         cin>>s;
22         int flag=0;
23         int len=s.length();
24         for(int i=0;i<len-1;i++){
25             if(judge(s[i])>judge(s[i+1])){
26                 flag=1; break;
27             }
28         }
29         if(flag)  cout<<"no"<<endl;
30         else  cout<<"yes"<<endl;
31     }
32 }
View Code

Courses in an university

分析:简单DP,看上一个状态和当前状态的关系,如果先修课程数相同,则加1,否则取上一门课程加1和当前课程所需先修的最小值

技术分享
 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "string"
 5 using namespace std;
 6 const int maxn=100000+10;
 7 int T;
 8 int a[maxn];
 9 int dp[maxn];
10 int main()
11 {
12     cin>>T;
13     while(T--)
14     {
15         int n;
16         cin>>n;
17         a[0]=0;
18         for(int i=1;i<=n;i++)
19             scanf("%d",&a[i]);
20         memset(dp,0,sizeof(dp));
21         dp[1]=1;
22         for(int i=2;i<=n;i++){
23             if(a[i]==a[i-1])
24                 dp[i]=dp[i-1]+1;
25             else{
26                 dp[i]=min((i-a[i]),dp[i-1]+1);
27             }
28         }
29         cout<<dp[n]<<endl;
30     }
31     return 0;
32 }
View Code

 

May Challenge 2017