A. Circle of Students 找到最小值,左右试探后,找到连续的一串序列持续 输出前进 ,直到回到原来位置,或中途退出 1 #includebits/stdc++.h 2 #define FOR(i,a,b) for(int i=a;ib;i++) 3 #define FOR2(i,a,b) fo
A. Circle of Students
找到最小值,左右试探后,找到连续的一串序列持续输出前进,直到回到原来位置,或中途退出
1 #include<bits/stdc++.h> 2 #define FOR(i,a,b) for(int i=a;i<b;i++) 3 #define FOR2(i,a,b) for(int i=a;i<=b;i++) 4 #define ll long long 5 #define INF 0x3f3f3f3f; 6 #define freopenin(a) freopen(a,"r",stdin); 7 #define freopenout(a) freopen(a,"w",stdout); 8 #define sf(a) scanf("%d",&a) 9 #define sf2(a,b) scanf("%d%d",&a,&b) 10 #define MAXN 600 11 #define MOD 10007 12 using namespace std; 13 int n,m,f[MAXN]; 14 int main() 15 { 16 cin>>n; 17 while(n--) 18 { 19 cin>>m;int p; 20 for(int i=0;i<m;i++) 21 { 22 cin>>f[i]; 23 if(f[i]==1)p=i; 24 } 25 // cout<<p<<endl; 26 if(f[p%m]-f[(p+1)%m]==-1) 27 { 28 int pos=p; 29 while(f[(p+1+m)%m]-f[(p+m)%m]==1)p++; 30 if((p-pos+1+m)%m==0)cout<<"YES"<<endl; 31 else cout<<"NO"<<endl; 32 } 33 else { 34 int pos=p; 35 while(f[(p-1+m)%m]-f[(m+p)%m]==1)p--; 36 if((p-pos-1+m)%m==0)cout<<"YES"<<endl; 37 else cout<<"NO"<<endl; 38 } 39 40 } 41 return 0; 42 }View Code
B. Equal Rectangles
可以推出min*max即为矩形大小,把所有棒子排个序,头尾不断收缩即可,注意棒子必须成对出现。
1 #include<bits/stdc++.h> 2 #define FOR(i,a,b) for(int i=a;i<b;i++) 3 #define FOR2(i,a,b) for(int i=a;i<=b;i++) 4 #define ll long long 5 #define INF 0x3f3f3f3f; 6 #define freopenin(a) freopen(a,"r",stdin); 7 #define freopenout(a) freopen(a,"w",stdout); 8 #define sf(a) scanf("%d",&a) 9 #define sf2(a,b) scanf("%d%d",&a,&b) 10 #define MAXN 60000 11 #define MOD 10007 12 using namespace std; 13 ll n,m,f[MAXN]; 14 ll arr[MAXN]; 15 vector<ll>q; 16 int main() 17 { 18 cin>>n; 19 memset(arr,0,sizeof(arr)); 20 while(n--) 21 { 22 q.clear(); 23 cin>>m; 24 for(int i=0;i<4*m;i++) 25 { 26 cin>>f[i];arr[f[i]]++; 27 q.push_back(f[i]); 28 } 29 bool flag=true; 30 31 if(!flag)continue; 32 sort(q.begin(),q.end()); 33 ll c=q[0]*q[q.size()-1]; 34 // cout<<"c="<<c<<endl; 35 for(int i=0,j=q.size()-1;i<j;i+=2,j-=2) 36 { 37 if(1ll*q[i]*q[j]!=c||q[i]!=q[i+1]||q[j]!=q[j-1]) 38 { 39 cout<<"NO"<<endl; 40 flag=false; 41 break; 42 } 43 } 44 if(flag)cout<<"YES"<<endl; 45 } 46 return 0; 47 }View Code
C. Common Divisors
不断对所有数求最大公约数,然后求该最大公约数的约数即可。
1 #include<bits/stdc++.h> 2 #define ll long long 3 #define FOR(a,b,c) for(int a=(b);a<(c);a++) 4 #define MAXN 50000 5 #define close ios::sync_with_stdio(false); 6 using namespace std; 7 ll gcd(ll a,ll b) 8 { 9 if(!b)return a; 10 else return gcd(b,a%b); 11 } 12 int main() 13 { 14 close; 15 ll n;cin>>n; 16 ll num1;ll num2;cin>>num1; 17 FOR(i,1,n) 18 { 19 cin>>num2; 20 num1=gcd(num1,num2); 21 } 22 ll ans=0;ll i; 23 for(i=1;i*i<=num1;i++) 24 if(num1%i==0)ans+=2; 25 i--; 26 if(i*i==num1)ans--; 27 cout<<ans<<endl; 28 return 0; 29 }View Code
D. Remove the Substring
设输入两个字符串$s_1,s_2$,记录下$s_2$中每个字符在$s_1$中的最早开始位置,和最晚结束位置,分别存储在两个数组中$q_1,,q_2$,因为任意两个字符中间最大可删除的长度为$q_2[i+1]-q_1[i]-1$,然后取最大值即可,包括头尾。
1 #include<iostream> 2 #include<vector> 3 #include<bits/stdc++.h> 4 #define FOR(i,a,b) for(int i=a;i<b;i++) 5 #define FOR2(i,a,b) for(int i=a;i<=b;i++) 6 #define ll long long 7 #define INF 0x3f3f3f3f; 8 #define freopenin(a) freopen(a,"r",stdin); 9 #define freopenout(a) freopen(a,"w",stdout); 10 #define sf(a) scanf("%d",&a) 11 #define sf2(a,b) scanf("%d%d",&a,&b) 12 #define MAXN 6000 13 #define MOD 10007 14 using namespace std; 15 int main() 16 { 17 string s1,s2;cin>>s1>>s2; 18 int p=0;int ans=0; 19 vector<int>q1,q2; 20 for(int i=0;i<s1.size();i++) 21 {//左边开始 22 if(s1[i]==s2[p]) 23 {//记录下每个字符左边的最早位置 24 q1.push_back(i); 25 p++; 26 } 27 if(p==s2.size()) 28 { 29 break; 30 } 31 32 } 33 // cout<<ans<<endl; 34 p=s2.size()-1; 35 for(int i=s1.size()-1;i>=0;i--) 36 {//右边开始 37 if(s1[i]==s2[p]) 38 {//记录下右边的最晚位置 39 p--; 40 q2.push_back(i); 41 } 42 if(p==-1) { 43 break; 44 } 45 } 46 sort(q1.begin(),q1.end());//左边 47 sort(q2.begin(),q2.end());//右边 48 ans=max(ans,q2[0]); 49 ans=max(ans,(int)s1.size()-q1[q1.size()-1]-1); 50 for(int i=0;i<q2.size()-1;i++) 51 { 52 ans=max(ans,q2[i+1]-q1[i]-1); 53 } 54 cout<<ans<<endl; 55 return 0; 56 }View Code
E. Boxers
贪心,先排序后,由高到低,能升高就加一,若相等就减一,最后去重计数。
1 #include<bits/stdc++.h> 2 #define ll long long 3 #define FOR(a,b,c) for(int a=(b);a<(c);a++) 4 #define MAXN 50000 5 #define close ios::sync_with_stdio(false); 6 using namespace std; 7 int main() 8 { 9 close; 10 vector<int>q; 11 int n;cin>>n; 12 while(n--) 13 { 14 int num;cin>>num;q.push_back(num); 15 } 16 sort(q.begin(),q.end()); 17 q[q.size()-1]++; 18 for(int i=q.size()-2;i>=0;i--) 19 { 20 if(q[i]>=q[i+1]&&q[i]-1!=0)q[i]--; 21 22 else if(q[i]+1<q[i+1])q[i]++; 23 } 24 sort(q.begin(),q.end()); 25 int pos=unique(q.begin(),q.end())-q.begin(); 26 cout<<pos<<endl; 27 return 0; 28 }View Code
F1. Complete the Projects (easy version)
贪心,对于$val>=0$的放入门槛较低的优先队列,对于$val<0$的放入门槛+val较高的优先队列。
1 #include<bits/stdc++.h> 2 #define FOR(i,a,b) for(int i=a;i<b;i++) 3 #define FOR2(i,a,b) for(int i=a;i<=b;i++) 4 #define ll long long 5 #define INF 0x3f3f3f3f; 6 #define freopenin(a) freopen(a,"r",stdin); 7 #define freopenout(a) freopen(a,"w",stdout); 8 #define sf(a) scanf("%d",&a) 9 #define sf2(a,b) scanf("%d%d",&a,&b) 10 #define MAXN 600 11 #define MOD 10007 12 using namespace std; 13 typedef struct{ 14 int the,val; 15 }NODE; 16 struct cmp{ 17 bool operator()(NODE n1,NODE n2) 18 { 19 return n1.the>n2.the; 20 } 21 }; 22 struct cmp2{ 23 bool operator()(NODE n1,NODE n2) 24 { 25 return n1.the+n1.val<n2.the+n2.val; 26 } 27 }; 28 priority_queue<NODE,vector<NODE>,cmp>q;//正 29 priority_queue<NODE,vector<NODE>,cmp2>p;//负 30 vector<NODE>pp; 31 int main() 32 { 33 int n,m;cin>>n>>m; 34 for(int i=0;i<n;i++) 35 { 36 int num1,num2;cin>>num1>>num2; 37 if(num2<0)p.push((NODE){num1,num2}); 38 else q.push((NODE){num1,num2}); 39 } 40 while(!q.empty()) 41 { 42 if(m<q.top().the){ 43 cout<<"NO"<<endl; 44 return 0; 45 } 46 else { 47 m=m+q.top().val; 48 q.pop(); 49 } 50 } 51 while(!p.empty()) 52 { 53 // cout<<p.top().the<<endl; 54 if(m<p.top().the) 55 { 56 cout<<"NO"<<endl; 57 return 0; 58 } 59 else 60 { 61 m=m+p.top().val; 62 p.pop(); 63 } 64 } 65 if(m<0)cout<<"NO"<<endl; 66 else cout<<"YES"<<endl; 67 return 0; 68 }View Code
F2. Complete the Projects (hard version)
贪心+背包dp,对于$val>=0$的放入门槛较低的优先队列,与easy version相同,对于$val<0$的放入门槛+val较高的数组,进行背包dp。
状态转移为:
$ if(j+i.val>=0\&\&j>=i.threshold) dp[i+1][j+i.val]=max(dp[i+1][j+i.val],dp[i][j]+1) $
$dp[i+1][j]=max(dp[i+1][j],dp[i][j])$
$i表示状态,j表示背包容纳$
1 #include<bits/stdc++.h> 2 #define FOR(i,a,b) for(int i=a;i<b;i++) 3 #define FOR2(i,a,b) for(int i=a;i<=b;i++) 4 #define ll long long 5 #define INF 0x3f3f3f3f; 6 #define freopenin(a) freopen(a,"r",stdin); 7 #define freopenout(a) freopen(a,"w",stdout); 8 #define sf(a) scanf("%d",&a) 9 #define sf2(a,b) scanf("%d%d",&a,&b) 10 #define MAXN 600 11 #define MOD 10007 12 using namespace std; 13 typedef struct{ 14 int the,val; 15 }NODE; 16 struct cmp{ 17 bool operator()(NODE n1,NODE n2) 18 { 19 return n1.the>n2.the;//xiao 20 } 21 }; 22 bool cmp2(NODE n1,NODE n2) 23 { 24 return n1.the+n1.val>n2.the+n2.val; 25 } 26 priority_queue<NODE,vector<NODE>,cmp>q;//正 27 vector<NODE>p; 28 int dp[200][40000]; 29 int main() 30 { 31 int n,m;cin>>n>>m; 32 for(int i=0;i<n;i++) 33 { 34 int num1,num2;cin>>num1>>num2; 35 if(num2<0)p.push_back((NODE){num1,num2}); 36 else q.push((NODE){num1,num2}); 37 } 38 int cnt=0; 39 while(!q.empty()) 40 { 41 if(m<q.top().the) 42 { 43 break; 44 } 45 else { 46 cnt++; 47 m+=q.top().val; 48 q.pop(); 49 } 50 } 51 memset(dp,0,sizeof(dp)); 52 sort(p.begin(),p.end(),cmp2); 53 for(int i=0;i<p.size();i++) 54 { 55 for(int j=0;j<=m;j++) 56 { 57 if(j>=p[i].the&&j+p[i].val>=0) 58 {//合法状态 59 dp[i+1][j+p[i].val]=max(dp[i+1][j+p[i].val],dp[i][j]+1); 60 } 61 //不合法状态 62 dp[i+1][j]=max(dp[i+1][j],dp[i][j]); 63 } 64 } 65 int ans=0; 66 for(int i=0;i<=m;i++)ans=max(ans,dp[p.size()][i]); 67 cout<<cnt+ans<<endl; 68 return 0; 69 }View Code