A:Candy Cookie Law
签到题。
1 a,b,c,d = map(int,input().split()) 2 if c>=a: 3 if d>=b: 4 print("No") 5 else: 6 print("Yes") 7 else: 8 print("No")
B:Count Subgrid
数据范围很小,直接枚举左上角,然后做加法,写起来快。
要想跑得快,甚至可以加上矩阵前缀和,但是没必要。
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <stack> 6 #include <unordered_set> 7 using namespace std; 8 const int N = 14; 9 string g[N]; 10 unordered_set<string> S; 11 int main() { 12 int n,m; 13 cin>>n>>m; 14 int ans=0; 15 // cout<<n<<m<<endl; 16 for(int i=0;i<n;i++) cin>>g[i]; 17 for(int i=0;i<n-m+1;i++){ 18 for(int j=0;j<n-m+1;j++){ 19 string t=""; 20 for(int x=i;x<i+m;x++){ 21 for(int y=j;y<j+m;y++){ 22 t+=g[x][y]; 23 } 24 } 25 // cout<<t<<endl; 26 if(S.count(t)==0){ 27 S.insert(t); 28 ans++; 29 } 30 } 31 } 32 33 cout << ans << "\n"; 34 return 0; 35 }
C:Truck Driver
将a和b的位置分组,枚举l,判断r是否合法。
1 n,a,b = map(int,input().split()) 2 s=input() 3 posa,posb=[],[] 4 for i,c in enumerate(s): 5 posa.append(i+1) if c=='a' else posb.append(i+1) 6 ia,ib=0,0 7 ans=0 8 for l in range(1,n+1): 9 while ia<len(posa) and posa[ia]<l: 10 ia+=1 11 while ib<len(posb) and posb[ib]<l: 12 ib+=1 13 rA=posa[ia+a-1] if ia+a-1<len(posa) else n+1 14 rB=posb[ib+b-1] if ib+b-1<len(posb) else n+1 15 if rA<=n: 16 ans+=max(0,rB-rA) 17 print(ans)
D:Neighbor Distance
每次删除只会影响有限的几个值,考虑清楚,遍历即可。学到了set上使用prev和next函数。
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 int main() { 5 int n; 6 cin >> n; 7 set<LL> S; 8 S.insert(0); 9 LL t; 10 cin >> t; 11 S.insert(t); 12 LL ans = 2LL * t; 13 cout << ans << endl; 14 const LL INF = (1LL<<60); 15 for (int i = 0; i < n - 1; ++i){ 16 cin >> t; 17 auto it = S.lower_bound(t); 18 19 if (it == S.begin()){ 20 LL R = *it; 21 LL nextR = (next(it) == S.end() ? INF : *next(it)); 22 LL oldR = (nextR == INF ? INF : nextR - R); 23 LL newR = min((nextR == INF ? INF : nextR - R), R - t); 24 LL dt = R - t; 25 ans += (newR + dt) - oldR; 26 }else if (it == S.end()){ 27 auto itL = prev(it); 28 LL L = *itL; 29 LL prevL = *prev(itL); 30 LL oldL = L - prevL; 31 LL newL = min(L - prevL, t - L); 32 LL dt = t - L; 33 34 ans += (newL + dt) - oldL; 35 }else { 36 LL R = *it; 37 auto itL = prev(it); 38 LL L = *itL; 39 LL prevL = (itL == S.begin() ? INF : *prev(itL)); 40 LL nextR = (next(it) == S.end() ? INF : *next(it)); 41 LL oldL = min((prevL == INF ? INF : L - prevL), R - L); 42 LL oldR = min((nextR == INF ? INF : nextR - R), R - L); 43 LL newL = min((prevL == INF ? INF : L - prevL), t - L); 44 LL newR = min((nextR == INF ? INF : nextR - R), R - t); 45 LL dt = min(t - L, R - t); 46 ans += (newL + newR + dt) - (oldL + oldR); 47 } 48 S.insert(t); 49 cout << ans << endl; 50 } 51 return 0; 52 }
E:Shift String
KMP板子题。
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int N = 2e6+10; 6 char p[N],s[N]; 7 int ne[N]; 8 int main() 9 { 10 int t; 11 cin>>t; 12 while(t--){ 13 // memset(ne,0,sizeof ne); 14 scanf("%s %s",s+1,p+1); 15 int n=strlen(s+1); 16 for(int i=2,j=0;i<=n;i++){ 17 while(j&&p[i]!=p[j+1]) j=ne[j]; 18 if(p[i]==p[j+1]) j++; 19 ne[i]=j; 20 } 21 for(int i=n+1;i<=2*n;i++){ 22 s[i]=s[i-n]; 23 } 24 s[2*n+1]='\0'; 25 // printf("%s \n%s\n",p+1,s+1); 26 int flag=false; 27 for(int i=1,j=0;i<=2*n;i++){ 28 while(j&&s[i]!=p[j+1]) j=ne[j]; 29 if(s[i]==p[j+1]) j++; 30 if(j==n){ 31 printf("%d\n",i-j); 32 flag=true; 33 break; 34 } 35 } 36 if(!flag) 37 printf("-1\n"); 38 } 39 return 0; 40 }