Codeforces Round #620 (Div. 2) A-F代码 (暂无记录题解)

A. Two Rabbits (手速题)

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

 

B. Longest Palindrome (模拟 枚举)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 int main(){
 5     int n,m;
 6     cin>>n>>m;
 7     vector<string> v;
 8     bool vis[n];
 9     memset(vis,0,sizeof(vis));
10     for(int i = 0;i<n;i++){
11         string s;
12         cin>>s;
13         v.push_back(s);
14     }
15     bool flag = false;
16     string mid = "",ans = "";
17     for(int i = 0;i<n;i++){
18         string t = v[i];
19         reverse(t.begin(),t.end());
20         if(!flag && t == v[i]) {
21             flag = true;
22             mid = v[i];
23             continue;
24         }
25         if(flag && t == v[i]) continue;
26         for(int j = 0;j<n;j++){
27             if(v[j] == t && vis[j] == 0){
28                 ans+=v[i];
29                 vis[i] = vis[j] = 1;
30                 break;
31             }
32         }        
33     }
34     string t = ans;
35     reverse(t.begin(),t.end());
36     ans+=mid,ans+=t;
37     if(ans.length() == 0){
38         cout<<0;return 0;
39     }
40     cout<<ans.length()<<endl;
41     cout<<ans;
42     return 0;
43 }
View Code

 

C. Air Conditioner (模拟 区间覆盖)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 105;
 5 struct node{
 6     ll t,l,h;
 7     ll l1,r;
 8     bool operator < (node &x)const {
 9         return t<x.t;
10     }
11 }c[maxn];
12 int main(){
13     int q;
14     cin>>q;
15     while(q--){
16         ll n,m;
17         cin>>n>>m;
18         for(int i = 1;i<=n;i++){
19             cin>>c[i].t>>c[i].l>>c[i].h ; 
20         } 
21         sort(c+1,c+1+n);
22         c[0].r = c[0].l1 = m; 
23         bool flag = true;
24         for(int i = 1;i<=n;i++){
25             ll d = c[i].t - c[i-1].t;
26             ll R = c[i-1].r + d;
27             ll L = c[i-1].l1 - d;
28             if(L>c[i].h || R< c[i].l){
29                 flag = false;
30     //            cout<<"De"<<i<<endl;
31                 break;
32             }
33             c[i].r = min(R,c[i].h);
34             c[i].l1 = max(L,c[i].l);
35         }        
36         if(flag) cout<<"YES"<<endl;
37         else cout<<"NO"<<endl;
38     }
39     return 0;
40 }
View Code

 

D. Shortest and Longest LIS(贪心 构造)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 void solve(){
 4     int n;
 5     string s;
 6     cin>>n;
 7     cin>>s;
 8     int Min[n];
 9     int t = n;
10     for(int i = 0;i<s.length();i++){
11         int len = 0,indx = i;
12         if(s[i] == '<'){
13             while(s[i]=='<'){
14                 len++;
15                 i++;
16             }
17         }
18         for(int j = i;j>=i-len;j--){
19             Min[j] = t,t--;
20         }
21     } 
22     if(t) Min[n-1] = t;
23     t = 1;
24     int Max[n];
25     for(int i = 0;i<s.length();i++){
26         int len = 0,indx = i;
27         if(s[i] == '>'){
28             while(s[i]=='>'){
29                 len++;
30                 i++;
31             }
32         }
33         for(int j = i;j>=i-len;j--){
34             Max[j] = t,t++;
35         }
36     }
37     if(t == n) Max[n-1] = t;
38     for(int i = 0;i<n;i++) cout<<Min[i]<<" ";
39     cout<<endl;
40     for(int i = 0;i<n;i++) cout<<Max[i]<<" ";
41     cout<<endl; 
42 }
43 int main(){
44     int t;
45     cin>>t;
46     while(t--){
47         solve();
48     } 
49     return 0;
50 }
View Code

 

E. 1-Trees and Queries (最近公共祖先)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxbit = 20;
 5 const int maxn = 1e5+5;
 6 vector<int> G[maxn];
 7 int depth[maxn];
 8 int fa[maxn][maxbit];
 9 int Log[maxn];
10 int N;
11 void pre(){
12     Log[0] = -1;
13     Log[1] = 0,Log[2] = 1;
14     for(int i = 3;i<maxn;i++) Log[i] = Log[i/2] + 1;
15 } 
16 void dfs(int cur,int father){//dfs预处理 
17     depth[cur] = depth[father] + 1;//当前结点的深度为父亲结点+1 
18     fa[cur][0] = father;//更新当前结点的父亲结点 
19     for(int j = 1;(1<<j)<=N;j++){//倍增更新当前结点的祖先 
20         fa[cur][j] = fa[fa[cur][j-1]][j-1];
21     }
22     for(int i = 0;i<G[cur].size() ;i++){
23         if(G[cur][i] != father) {//dfs遍历 
24             dfs(G[cur][i],cur);
25         }
26     }
27 }
28 int LCA(int u,int v){
29     if(depth[u]<depth[v]) swap(u,v);
30     int dist = depth[u] - depth[v];//深度差 
31     while(depth[u]!=depth[v]){//把较深的结点u倍增到与v高度相等 
32         u = fa[u][Log[depth[u]-depth[v]]];
33     }
34     if(u == v) return u;//如果u倍增到v,说明v是u的LCA 
35     for(int i = Log[depth[u]];i>=0;i--){//否则两者同时向上倍增 
36         if(fa[u][i]!=fa[v][i]){//如果向上倍增的祖先不同,说明是可以继续倍增 
37             u = fa[u][i];//替换两个结点 
38             v = fa[v][i];
39         }
40     }
41     return fa[u][0];//最终结果为u v向上一层就是LCA 
42 } 
43 int main()
44 {
45     pre();
46     cin>>N;
47     for(int i = 1;i<N;i++){
48         int u,v;
49         cin>>u>>v;
50         G[u].push_back(v),G[v].push_back(u);  
51     }
52     dfs(1,0);
53     int q;cin>>q;
54     while(q--){
55         int x,y,a,b,k;
56         cin>>x>>y>>a>>b>>k;
57         int t1 = LCA(a,b),t2 = LCA(a,x),t3 = LCA(a,y),t4 = LCA(b,x),t5  = LCA(b,y);
58         int dis = abs(depth[t1]-depth[a])+abs(depth[t1]-depth[b]);
59         if(dis%2 == k%2 && dis <=k ){
60 //            cout<<"De1"<<endl;
61             cout<<"YES"<<endl;
62             continue;
63         }
64         int dis1 = abs(depth[t2]-depth[a])+abs(depth[t2]-depth[x]);
65         int dis2 = abs(depth[t5]-depth[b])+abs(depth[t5]-depth[y]); 
66         if( (dis1+dis2+1)%2 == k%2  && dis1+dis2+1 <= k){
67 //            cout<<"de2"<<endl;
68             cout<<"YES"<<endl;
69             continue;
70         }
71         dis1 = abs(depth[t3]-depth[a])+abs(depth[t3]-depth[y]);
72         dis2 = abs(depth[t4]-depth[b])+abs(depth[t4]-depth[x]);
73         if((dis1+dis2+1)%2 == k%2 && dis1+dis2+1 <= k){
74 //            cout<<"de3"<<endl;
75             cout<<"YES"<<endl;
76             continue;
77         } 
78         cout<<"NO"<<endl;
79     }
80     return 0;
81 }
View Code

 

F2. Animal Observation (hard version) (dp 滑动窗口 线段树区间更新最大值查询)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 4e4+5;
 4 int dp[55][maxn];
 5 int val[55][maxn];
 6 int sum[55][maxn];
 7 int n,m,k;
 8 struct node{
 9     int l,r;
10     int Max,lz;
11 }seg_t[maxn*4];
12 void build(int l,int r,int p){
13     seg_t[p].l = l,seg_t[p].r = r;
14     if(l == r) {
15         seg_t[p].Max = 0;return ;
16     }
17     int mid = (l+r)>>1;
18     build(l,mid,p*2);
19     build(mid+1,r,p*2+1);
20     seg_t[p].Max = max(seg_t[p*2].Max ,seg_t[p*2+1].Max );
21 }
22 void pushdown(int k){
23     seg_t[k*2].lz +=seg_t[k].lz ;
24     seg_t[k*2+1].lz +=seg_t[k].lz ;
25     seg_t[k*2].Max +=seg_t[k].lz ;
26     seg_t[k*2+1].Max +=seg_t[k].lz ;
27     seg_t[k].lz = 0; 
28 }
29 void upd(int L,int R,int p,int v){
30     if(seg_t[p].l == L && seg_t[p].r == R){
31         seg_t[p].lz +=v;
32         seg_t[p].Max +=v;
33         return;
34     }
35     if(seg_t[p].lz ) pushdown(p);
36     int mid = (seg_t[p].l + seg_t[p].r )>>1;
37     if(R<=mid) upd(L,R,p*2,v);
38     else if(L>mid) upd(L,R,p*2+1,v);
39     else{
40         upd(L,mid,p*2,v);
41         upd(mid+1,R,p*2+1,v);
42     }
43     seg_t[p].Max = max(seg_t[p*2].Max ,seg_t[p*2+1].Max );
44 }
45 int query(int p,int L,int R){
46     if(seg_t[p].l == L && seg_t[p].r == R) return seg_t[p].Max ;
47     if(seg_t[p].lz ) pushdown(p);
48     int mid = (seg_t[p].l +seg_t[p].r)>>1;
49     if(R<=mid) return query(p*2,L,R);
50     else if(L>mid) return query(p*2+1,L,R);
51     else return max(query(p*2,L,mid),query(p*2+1,mid+1,R)); 
52 }
53 int main(){
54     cin>>n>>m>>k;
55     for(int i = 1;i<=n;i++){
56         for(int j = 1;j<=m;j++) {
57             cin>>val[i][j];
58             sum[i][j] = sum[i][j-1] + val[i][j];
59         }
60     }
61     for(int i = 1;i<=m-k+1;i++){
62         dp[1][i] = sum[1][i+k-1] - sum[1][i-1]+sum[2][i+k-1]-sum[2][i-1];
63     }
64     for(int i = 2;i<=n;i++){
65         memset(seg_t,0,sizeof(seg_t));
66         build(1,2*m,1);
67         for(int j = 1;j<=m;j++) upd(j,j,1,dp[i-1][j]);
68         for(int j = 1;j<=k;j++) upd(1,j,1,-val[i][j]);
69         for(int j = 1;j<=m-k+1;j++){
70             dp[i][j] = max(dp[i][j],query(1,1,m)+sum[i][j+k-1]-sum[i][j-1]+sum[i+1][j+k-1]-sum[i+1][j-1]);
71             upd(max(1,j-k+1),j,1,val[i][j]);
72             upd(j+1,j+k,1,-val[i][j+k]);
73         }
74     } 
75     int ans = 0;
76     for(int i = 1;i<=m;i++) ans = max(ans,dp[n][i]);
77     cout<<ans;
78     return 0;
79 }
View Code

 

posted @ 2020-02-24 17:24  AaronChang  阅读(117)  评论(0编辑  收藏  举报