Codeforces Round #546 (Div. 2) ABCD 题解

题目链接

A. Nastya Is Reading a Book

分析

只需要从最后一个章节开始遍历,比较k与r[i]的值,如果k<=r[i],则表明这一章节没读完.

代码

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <string>
 8 #include <utility>
 9 #include <queue>
10 #include <stack>
11 #include <map> 
12 const int INF=0x3f3f3f3f;
13 using namespace std;
14 
15 int l[100],r[100]; 
16 
17 int main()
18 {
19     int n;
20     cin>>n;
21     for(int i=0;i<n;i++)
22     cin>>l[i]>>r[i];
23     int res;
24     cin>>res;
25     int ans=0;
26     for(int i=n-1;i>=0;i--)
27     {
28         if(res<r[i]) ans++;
29         else
30         {
31             if(res==r[i]) ans++;
32             break;
33         }
34     }
35     cout<<ans<<endl;
36     return 0;
37 }
View Code

B. Nastya Is Playing Computer Games

分析

如果刚开始Nastya出现在左边区域,则Nastya需要先往左边收集硬币再往右边走收集硬币,如果刚开始Nastya出现右边,则Nastya需要先往右边走收集硬币再往左边走收集硬币,注意刚开始两步必须执行6个行动,即扔石头,拿硬币,前往下个井,仍石头,扔石头(这里把石头扔回第一个井),拿硬币,往后没走一步只需要3步,即前往下一个井,扔石头(把石头扔回第一个井),拿硬币.

代码

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <string>
 8 #include <utility>
 9 #include <queue>
10 #include <stack>
11 #include <map> 
12 const int INF=0x3f3f3f3f;
13 using namespace std; 
14 
15 int a[5001];
16 
17 int main()
18 {
19     int n,k;
20     cin>>n>>k;
21     int t=n/2;
22     int ans=0;
23     if(k>t)
24     {
25         int s=1;
26         while(k<n)
27         {
28             a[k]=1;
29             k++;
30             if(s) ans+=6,s=0;
31             else  ans+=3;
32         }
33         a[k]=1;
34         while(k>1)
35         {
36             k--;
37             if(s) ans+=6,s=0;
38             else if(!a[k]) ans+=3;
39             else if(a[k]) ans+=1;
40         }
41     }
42     else 
43     {
44         int s=1;
45         while(k>1)
46         {
47             a[k]=1;
48             k--;
49             if(s) ans+=6,s=0;
50             else ans+=3;
51         }
52         a[k]=1;
53         while(k<n)
54         {
55             k++;
56             if(s) ans+=6,s=0;
57             else if(!a[k]) ans+=3;
58             else if(a[k]) ans+=1;
59         }
60     }
61     cout<<ans<<endl;
62     return 0;
63 }
View Code

C. Nastya Is Transposing Matrices

分析

题目给的镜像操作实质上换来换去只会交换对角线上的值,那要知道第二个矩阵能不能由第一个矩阵镜像操作而来,只需要检查两个矩阵间对角线的元素是否相同就行了(元素相同顺序不一定相同)

代码

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <string>
 8 #include <utility>
 9 #include <queue>
10 #include <stack>
11 #include <map> 
12 const int INF=0x3f3f3f3f;
13 using namespace std; 
14 
15 int a[500][500],b[500][500],c[500][500];
16 
17 int main()
18 {
19     int n,m;
20     scanf("%d%d",&n,&m);
21     for(int i=0;i<n;i++)
22     for(int j=0;j<m;j++)
23     scanf("%d",&a[i][j]);
24     for(int i=0;i<n;i++)
25     for(int j=0;j<m;j++)
26     {
27         scanf("%d",&b[i][j]);
28         int p=i,q=j,s=0;
29         while(p>=0&&p<n&&q>=0&&q<m)
30         {
31             if(!c[p][q]&&b[i][j]==a[p][q])
32             {
33                 c[p][q]=1,s=1;
34                 break;
35             }
36             p++,q--;
37         }
38         
39         if(!s)
40         {
41             p=i,q=j;
42             while(p>=0&&p<n&&q>=0&&q<m)
43             {
44                 if(!c[p][q]&&b[i][j]==a[p][q])
45                 {
46                     c[p][q]=1,s=1;
47                     break;
48                 }
49                 p--,q++;
50             }
51         }
52         if(!s)
53         {
54             cout<<"NO\n";
55             return 0;
56         }
57     }
58     cout<<"YES\n";
59     return 0;
60 }
View Code

D. Nastya Is Buying Lunch

分析

如果一个人能让Nastya(最后一个人)前进,那么这个人就得能和他后面的人中的不能和让Nastya前进人换位(当然也必须得能和Nastya换位).就上图例子来说,第二个点能和第三个点换位,则第一个点只需要能和第三个点换位即可,第二个点不能和第三个点换位,则第一点必须能与第三个点换位且能与第二个点换位.再举个一般的例子:

图中1代表能让Nastya前进一步,分析一下就知道了.

 代码

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <string>
 8 #include <utility>
 9 #include <queue>
10 #include <stack>
11 #include <map>
12 #include <set> 
13 const int INF=0x3f3f3f3f;
14 using namespace std;
15 
16 typedef pair<int,int> P;
17 set<P> s;
18 int a[300000];
19 vector<int> v;
20 
21 int main()
22 {
23     int n,m;
24     cin>>n>>m;
25     for(int i=0;i<n;i++) scanf("%d",&a[i]);
26     for(int i=0;i<m;i++)
27     {
28         int x,y;
29         scanf("%d%d",&x,&y);
30         s.insert(P(x,y));
31     }
32     v.push_back(a[n-1]);
33     int ans=0;
34     for(int i=n-2;i>=0;i--)
35     {
36         int flag=1;
37         for(int j=0;j<v.size();j++)
38         {
39             if(!s.count(P(a[i],v[j])))
40             {
41                 flag=0;
42                 break;
43             }
44         }
45         if(flag) ans++;
46         else v.push_back(a[i]); 
47     }
48     cout<<ans<<endl;
49     return 0;
50 }
View Code

猜你喜欢

转载自www.cnblogs.com/VBEL/p/10612546.html