Codeforces-Round#579 Div3

A. Circle of Students

Minimum is found, after about temptation, find a string of consecutive sequence of continuous output forward until it returns to its original position, or drop out

 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

Can be introduced min * max is the size of the rectangle, all stick to row a sequence, you can head to tail shrinking, note stick must appear in pairs.

 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

Constantly seeking the greatest common divisor of all numbers, then you can ask about the number of the greatest common divisor.

 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 

Two reset input string $ s_1, s_2 $, $ record $ S_2 each character in the first start position of $ $ S_1, and latest end position are stored in two arrays $ q_1,, q_2 $ , since the intermediate any two characters to delete the maximum length of $ q_2 [i + 1] -q_1 [i] -1 $, and can take a maximum value, comprising a head and tail.

 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 #define10007 the MOD 
 14  the using  namespace STD;
 15  int main ()
 16  {
 . 17      String S1, S2; S1 >> >> CIN S2;
 18 is      int P = 0 ; int ANS = 0 ;
 . 19      Vector < int > Ql, Q2;
 20 is      for ( int I = 0 I ++;; I <s1.size () )
 21 is      { // left are 
22 is          IF (S1 [I] == S2 [P])
 23 is          { // record the position of each character to the left of the first 
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         {// record the right of the latest position 
39              the P-- ;
 40              q2.push_back (I);
 41 is          }
 42 is          IF (P == - . 1 ) {
 43 is              BREAK ;
 44 is          }
 45      }
 46 is      Sort (q1.begin (), q1.end ()); // left 
47      Sort (q2.begin (), q2.end ()); // the right 
48      ANS = max (ANS, Q2 [ 0 ]);
 49      ANS = max (ANS, ( int ) s1.size () - Ql [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

Greedy, after the first sort, from high to low, can be raised to a plus, if equal, a decrease, and finally to re-count.

 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)

Greedy, for $ val> = 0 $ placed in a lower priority queue threshold for $ val <0 $ threshold + val placed higher priority queue.

 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)

Greedy + DP backpack for $ val> = 0 $ placed in a lower priority queue threshold, the same easy version, val for $ <$ 0 + higher placed val threshold array, for a backpack dp.

State transition is:

$ 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])$

Shows a state $ i, j represents the Backpacks receiving $

 

 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

 

Guess you like

Origin www.cnblogs.com/tldr/p/11355566.html