Codeforces Round #620 (Div. 2)【A、B、C、D】题解(持续更新)

涵盖知识点:主要考查思维和代码实现能力,前四题没什么算法。

比赛链接:

https://codeforces.com/contest/1304

A:Two Rabbits

题意:两只兔子分别在x和y位置,左边的兔子每次往右跳a,右边的兔子每次往左跳b,问是否会在某一点相遇。

题解:算间隔距离是否为a+b的倍数即可。

AC代码:

 1 //A
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 
 5 int main(){
 6     int t;
 7     cin>>t;
 8     while(t--){
 9         int x,y,a,b;
10         cin>>x>>y>>a>>b;
11         int len=y-x;
12         if(len%(a+b)==0){
13             cout<<len/(a+b)<<"\n";
14         }else{
15             cout<<"-1\n";
16         }
17     }
18     return 0;
19 }

B:Longest Palindrome

题意:给长度为m的n个串,问最多选几个构成回文串。

题解:暴力寻找配对回文串分别加在答案的头和尾,最后再遍历一遍没有配对的是否含有自身就是回文的串,有则放正中间

AC代码:

 1 //B
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 string s[110];
 5 bool vis[110];
 6 int main(){
 7     int n,m;
 8     cin>>n>>m;
 9     for(int i=0;i<n;i++){
10         cin>>s[i];
11     }
12     string ans="";
13     for(int i=0;i<n;i++){
14         if(vis[i])continue;
15         string tmp=string(s[i].rbegin(),s[i].rend());
16         for(int j=i+1;j<n;j++){
17             if(vis[j])continue;
18             if(s[j]==tmp){
19                 vis[i]=vis[j]=true;
20                 ans=s[i]+ans;
21                 ans=ans+s[j];
22             }
23         }
24     }
25     for(int i=0;i<n;i++){
26         if(!vis[i]){
27             string tmp=string(s[i].rbegin(),s[i].rend());
28             if(s[i]==tmp){
29                 cout<<ans.length()+m<<"\n";
30                 for(int j=0;j<ans.length()/2;j++){
31                     cout<<ans[j];
32                 }
33                 cout<<s[i];
34                 for(int j=ans.length()/2;j<ans.length();j++){
35                     cout<<ans[j];
36                 }
37                 cout<<"\n";
38                 return 0;
39             }
40         }
41     }
42     cout<<ans.length()<<"\n";
43     if(ans.length())cout<<ans<<"\n";
44     return 0;
45 }

C:Air Conditioner

题意:每秒可以控制室内温度+1,-1或者不变。每个客人来的时候需要满足室内温度在自己的舒适区间内,问是否可以完成任务。

题解:算出每个人来的时候的可以控制的温度区间,观察是否存在温度区间内的温度在自己的舒适区间内,并更新当前的温度区间为[两者左区间的较大值,两者右区间的较小值]。遍历一遍即可。

AC代码:

 1 //C
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 
 5 struct Node{
 6     int t,l,r;
 7 }p[110];
 8 void solve(){
 9     int n,m;
10     cin>>n>>m;
11     Node timer={0,m,m};
12     for(int i=0;i<n;i++){
13         cin>>p[i].t>>p[i].l>>p[i].r;
14     }
15     int lst=0;
16     for(int i=0;i<n;i++){
17         timer={p[i].t,max(p[i].l,timer.l-p[i].t+timer.t),min(p[i].r,timer.r+p[i].t-timer.t)};
18         if(timer.l>p[i].r||timer.r<p[i].l){
19             cout<<"NO\n";
20             return;
21         }
22     }
23     cout<<"YES\n";
24 }
25 int main(){
26     int t;
27     cin>>t;
28     while(t--){
29         solve();
30     }
31     return 0;
32 }

D:Shortest and Longest LIS

题意:给n个数(1-n)之间组成串的相邻两数的大小关系,求使得LIS最小和最大的两种序列。

题解:使LIS最小即小于号往大取,使LIS最大即大于号往小取即可。主要考察代码实现的能力。

AC代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 2e5 + 5;
 4 char s[N];
 5 int n, a[N];
 6 void getmax() {
 7     int L = 0, pre = 0;
 8     for(int i = 1; i <= n; i++) {
 9         if(s[i] == '>') continue;
10         else {
11             for(int j = i; j > pre; j--) a[j] = ++L;
12             pre = i;
13         }
14     }
15     for(int i = 1; i <= n; i++) printf("%d ", a[i]);
16     printf("\n");
17 }
18 void getmin() {
19     int L = n + 1, pre = 0;
20     for(int i = 1; i <= n; i++) {
21         if(s[i] == '<') continue;
22         else {
23             for(int j = i; j > pre; j--) a[j] = --L;
24             pre = i;
25         }
26     }
27     for(int i = 1; i <= n; i++) printf("%d ", a[i]);
28     printf("\n");
29 }
30 
31 int main() {
32     int T; scanf("%d", &T);
33     while(T--) {
34         scanf("%d", &n);
35         scanf(" %s", s + 1);
36         getmin();
37         getmax();
38     }
39 }

E:1-Trees and Queries

F1:Animal Observation (easy version)

F2:Animal Observation (hard version)

猜你喜欢

转载自www.cnblogs.com/charles1999/p/12316528.html