Codeforces Round #578 (Div. 2)

A.模拟。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 6 typedef long long ll;
 7 using namespace std;
 8  
 9 const int N=200010;
10 char s[N];
11 int n,b[N];
12  
13 int main(){
14     scanf("%d%s",&n,s+1);
15     rep(i,1,n){
16         if (s[i]==‘L‘) rep(j,0,9) if (!b[j]){ b[j]=1; break; }
17         if (s[i]==‘R‘) for (int j=9; ~j; j--) if (!b[j]){ b[j]=1; break; }
18         if (s[i]>=‘0‘ && s[i]<=‘9‘) b[s[i]-‘0‘]=0;
19     }
20     rep(i,0,9) if (b[i]) putchar(‘1‘); else putchar(‘0‘);
21     return 0;
22 }

A

B.为了积累积木,一定是走到h[i]-k的高度。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 6 typedef long long ll;
 7 using namespace std;
 8  
 9 const int N=2000010;
10 int T,n,m,k,h[N];
11  
12 int main(){
13     for (scanf("%d",&T); T--; ){
14         scanf("%d%d%d",&n,&m,&k); bool flag=0;
15         rep(i,1,n) scanf("%d",&h[i]);
16         rep(i,1,n-1){
17             if (h[i]<h[i+1]-k){
18                 if (m<h[i+1]-k-h[i]){ flag=1; break; }
19                 m-=h[i+1]-k-h[i]; continue;
20             }
21             m+=h[i]-max(h[i+1]-k,0);
22         }
23         if (flag) puts("NO"); else puts("YES");
24     }
25     return 0;
26 }

B

C.都知道怎么做,关键就是把它变成代码。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 6 typedef long long ll;
 7 using namespace std;
 8  
 9 ll n,m,s1,s2,sx,sy,ex,ey;
10 int Q;
11  
12 ll gcd(ll a,ll b){ return b ? gcd(b,a%b) : a; }
13  
14 int main(){
15     cin>>n>>m>>Q;
16     rep(i,1,Q){
17         cin>>sx>>sy>>ex>>ey; s1=n/gcd(n,m); s2=m/gcd(n,m);
18         if (sx==1 && ex==1) puts((sy-1)/s1==(ey-1)/s1?"YES":"NO");
19         else if (sx==2 && ex==2) puts((sy-1)/s2==(ey-1)/s2?"YES":"NO");
20         else if (sx==1 && ex==2) puts((sy-1)/s1==(ey-1)/s2?"YES":"NO");
21         else if (sx==2 && ex==1) puts((sy-1)/s2==(ey-1)/s1?"YES":"NO");
22     }
23     return 0;
24 }

C

D.若想将一行全部变成白色,则选取矩形的左上角是在一个矩形里的,一列也一样。于是这就是个矩形加问题,二维差分即可。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 6 typedef long long ll;
 7 using namespace std;
 8  
 9 const int N=2010;
10 int n,k;
11 char s[N][N];
12 int a[N][N],b[N][N];
13  
14 int main(){
15     scanf("%d%d",&n,&k);
16     rep(i,1,n) scanf("%s",&s[i][1]);
17     int ans=0;
18     rep(i,1,n){
19         int mn=n+1,mx=0;
20         rep(j,1,n) if (s[i][j]==‘B‘) mn=min(mn,j),mx=max(mx,j);
21         if (mn==n+1) ++ans;
22         else if (mx-mn+1>k) continue;
23         else{
24             int x=max(1,i-k+1),y=max(1,mx-k+1);
25             rep(j,x,i) a[j][y]++,b[j][mn+1]++;
26         }
27     }
28     rep(i,1,n){
29         int mn=n+1,mx=0;
30         rep(j,1,n) if (s[j][i]==‘B‘) mn=min(mn,j),mx=max(mx,j);
31         if (mn==n+1) ++ans;
32         else if (mx-mn+1>k) continue;
33         else{
34             int x=max(1,mx-k+1),y=max(1,i-k+1);
35             rep(j,x,mn) a[j][y]++,b[j][i+1]++;
36         }
37     }
38     int Ans=0;
39     rep(i,1,n){
40         int tot=0;
41         rep(j,1,n){
42             tot+=a[i][j]; tot-=b[i][j];
43             Ans=max(Ans,tot+ans);
44         }
45     }
46     cout<<Ans;
47     return 0;
48 }

D

E.一个做法是依次做,比较前后缀是否相同时用Hash。另一个做法是每次对新加入的串求KMP中的fail数组,然后扫一遍求出前面合成出的串与新加入的串的最长匹配长度。

 1 #include<string>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<iostream>
 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 6 using namespace std;
 7  
 8 const int p1=131,p2=998244353;
 9 int n;
10 string s,res;
11  
12 int main(){
13     for (scanf("%d",&n); n--; ){
14         cin>>s;
15         int l=0,r=0,p=1,c=0,ed=min(res.size(),s.size())-1,len=res.size();
16         rep(i,0,ed){
17             l=(l+1ll*res[len-i-1]*p)%p2,r=(1ll*r*p1+s[i])%p2,p=1ll*p*p1%p2;
18             if (l==r) c=i+1;
19         }
20         res+=s.substr(c);
21     }
22     cout<<res<<endl;
23     return 0;
24 }

E(Hash)

 1 #include<string>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<iostream>
 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 6 using namespace std;
 7  
 8 const int N=1000010;
 9 string res,s;
10 int n,fail[N];
11  
12 int main(){
13     ios::sync_with_stdio(0); cin.tie(0);
14     cin>>n>>res;
15     while (--n){
16         cin>>s; fail[0]=0;
17         for (int i=1,j=0; i<(int)s.length(); i++){
18             fail[i]=0;
19             while (j>0 && s[i]!=s[j]) j=fail[j-1];
20             if (s[i]==s[j]) fail[i]=++j;
21         }
22         int j=0;
23         for (int i=max(0,(int)res.length()-(int)s.length()); i<(int)res.length(); i++){
24             while (j>0 && res[i]!=s[j]) j=fail[j-1];
25             if (res[i]==s[j]) j++;
26         }
27         for (; j<(int)s.length(); j++) res+=s[j];
28     }
29     cout<<res<<endl;
30     return 0;
31 }

E(KMP)

F.每个点拆成2520份然后直接跑记忆化搜索即可。

 1 #include<vector>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<iostream>
 5 #define rep(i,l,r) for (int i=(l); i<=(r); i++)
 6 using namespace std;
 7  
 8 const int N=1010,mod=2520;
 9 int n,Q,x,y,dep,f[N][mod],tim[N],k[N];
10 vector<int>to[N];
11  
12 int dfs(int x,int y){
13     if (f[x][y]>0) return f[x][y];
14     if (f[x][y]<0){
15         int sz=0;
16         rep(i,1,n) sz+=tim[i]<=f[x][y];
17         return f[x][y]=sz;
18     }
19     f[x][y]=tim[x]=--dep;
20     return f[x][y]=dfs(to[x][(y+k[x])%to[x].size()],(y+k[x])%mod);
21 }
22  
23 int main(){
24     ios::sync_with_stdio(0); cin.tie(0);
25     cin>>n;
26     rep(i,1,n) cin>>k[i],k[i]=(k[i]%mod+mod)%mod;
27     rep(i,1,n) for (cin>>x; x--; ) cin>>y,to[i].push_back(y);
28     for (cin>>Q; Q--; ) cin>>x>>y,y=(y%mod+mod)%mod,cout<<dfs(x,y)<<endl;
29     return 0;
30 }

F

原文地址:https://www.cnblogs.com/HocRiser/p/11372275.html

时间: 2024-07-31 15:29:32

Codeforces Round #578 (Div. 2)的相关文章

Codeforces Round #578 (Div. 2) 二维差分 可做模板

题意: 在n*n的矩阵中,你可以选择一个k*k的子矩阵,然后将这个子矩阵中的所有B全部变为W,问你怎么选择这个子矩阵使得最终的矩阵中某一行全是W或者某一列全是W的个数最多 题解:考虑每一行和每一列,对于特定的一行来说,要想让其全变为W,那么子矩阵的左上角端点是在一个范围中的,因此我们可以把范围中的每一个值加1 为了速度选择用二维差分来做,最终矩阵中的最大值就是答案 此题可以作为二维差分模板 #include<bits/stdc++.h> #define forn(i, n) for (int

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我

[Codeforces] Round #352 (Div. 2)

人生不止眼前的狗血,还有远方的狗带 A题B题一如既往的丝帛题 A题题意:询问按照12345678910111213...的顺序排列下去第n(n<=10^3)个数是多少 题解:打表,输出 1 #include<bits/stdc++.h> 2 using namespace std; 3 int dig[10],A[1005]; 4 int main(){ 5 int aa=0; 6 for(int i=1;;i++){ 7 int x=i,dd=0; 8 while(x)dig[++dd

Codeforces Round #273 (Div. 2)

Codeforces Round #273 (Div. 2) 题目链接 A:签到,仅仅要推断总和是不是5的倍数就可以,注意推断0的情况 B:最大值的情况是每一个集合先放1个,剩下都丢到一个集合去,最小值是尽量平均去分 C:假如3种球从小到大是a, b, c,那么假设(a + b) 2 <= c这个比較明显答案就是a + b了.由于c肯定要剩余了,假设(a + b)2 > c的话,就肯定能构造出最优的(a + b + c) / 3,由于肯定能够先拿a和b去消除c,而且控制a和b成2倍关系或者消除

Codeforces Round #339 (Div. 2) B. Gena&#39;s Code

B. Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, f

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un

DP Codeforces Round #303 (Div. 2) C. Woodcutters

题目传送门 1 /* 2 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 3 问最多能砍到多少棵树 4 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 5 分情况讨论,若符合就取最大值更新,线性dp,自己做出来了:) 6 */ 7 #include <cstdio> 8 #include <algorithm> 9 #include <cstring> 10 #include <cmath> 11 #include &