Codeforces Round #655 (Div. 2) Problem Solution ABCD

Question A:
Water question, as long as all elements are set to 1.
Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
typedef long long ll;
const int MAXN=2e5+5;
const int mod=1e9+7;
int main()
{
    
    
    int t;
    cin>>t;
    while(t--)
    {
    
    
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
    
    
           cout<<1<<" ";
        }
        cout<<endl;
    }
}

Question B:
Assuming a<=b, we can find that when b is a multiple of a, lcm(a,b) will achieve the minimum value, that is, b=ka. Then a+ka=n, (k+1)a=n, the smaller k is, the closer a and b are, the smaller b is, and the smaller lcm(a, b) is. Then we are going to find the smallest factor of n (except 1), and get rid of it.
Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
typedef long long ll;
const int MAXN=2e5+5;
const int mod=1e9+7;
ll gcd(ll a, ll b)
{
    
    
    if (b == 0)
        return a;
    else if (a < b)
        return gcd(b, a);
    else
        return gcd(b, a % b);
}

ll lcm(ll a, ll b)
{
    
    
    return a / gcd(a, b) * b;
}
int main()
{
    
    
    int t;
    cin>>t;
    while(t--)
    {
    
    
        int n;
        cin>>n;
        if(n==2)
        {
    
    
            cout<<1<<" "<<1<<endl;
            continue;
        }
        if(n==3)
        {
    
    
            cout<<1<<" "<<2<<endl;
            continue;
        }
        int a1,b1;
        int f=-1;
        for(int i=2;i<=sqrt(n);i++)
        {
    
    
            if(n%i==0)
            {
    
    
                f=1;
               a1=n/i;
               b1=n-a1;
               break;
            }
        }
        if(f==1) cout<<min(a1,b1)<<" "<<max(a1,b1)<<endl;
        else cout<<1<<" "<<n-1<<endl;
    }
}

Question C:
When all a[i]=i in the array, the answer is definitely 0. When there is only a continuous interval (l=<i<=r) a[i]!=i, directly operate the interval The number becomes a[i]=i, and the answer is 1. When there are two or more continuous intervals that do not meet the requirements, we can record the start position L of the first paragraph and the end position R of the last paragraph. Use one operation to first change the interval that does not meet the requirements. For a[i]=i, from the beginning -1 position of the last interval to the beginning of the first paragraph, it can first become unsatisfactory, and then use another operation to make this interval legal, then It only takes two times to complete, and the answer is 2.
Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<vector>
using namespace std;
#define iis std::ios::sync_with_stdio(false); cin.tie(0)
typedef long long ll;
const int MAXN=2e5+5;
const int inf=0x3f3f3f3f;

int a[MAXN];
int main()
 {
    
    
 	int t;
 	cin>>t;
 	while(t--)
 	{
    
    
 		int n;
 		cin>>n;
 		for(int i=1;i<=n;i++)
 		{
    
    
 			cin>>a[i];
 		}
 		int ans=0;
 		int f=0;
 		for(int i=1;i<=n;i++)
 		{
    
    
 			if(a[i]!=i)
 			{
    
    
 				if(f==0)
 				{
    
    
 					f=1;
 					ans++;
 				}
 			}
 			else f=0;
 		}
 		if(ans==0) cout<<0<<endl;
 		else if(ans==1) cout<<1<<endl;
 		else cout<<2<<endl;
 	}
 }

Question D:
I didn’t write it out during the game. After reading the idea of ​​the boss, it is probably like this. We assume that the i-th digit needs to be kept to the end, then just look at the left, then the i-1th digit must be deleted and go all the way to the left Push, when it reaches 1, it will be n to the left until there is only one number left. When i is an odd number, then you can find that the even position before i will be deleted, the odd position will be kept, the odd position after i will be deleted, and the even position will be kept, otherwise when i is an even number, the odd position before i will be deleted If it is deleted, the even position after i will be deleted.
For example, 1 2 3 4 5 6 7.
When we choose 4 to keep, then 3 delete —>1 6 5 6 7, delete 1---->5 6 13, delete 6---->18.
When we choose 5 to keep, then 4 delete—>1 2 8 6 7, delete 2---->6 7 9, delete 7---->15.
So we enumerate the positions to be reserved, use the prefix and find the answer.

Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
using namespace std;
typedef long long ll;
const int MAXN=2e5+5;
const int mod=1e9+7;
ll s[MAXN];
ll a[4][MAXN];
int main()
{
    
    
    int n;
    cin>>n;
    a[1][0]=0;
    a[2][0]=0;
    for(int i=1;i<=n;i++)
    {
    
    
    	cin>>s[i];
    	a[1][i]=a[1][i-1];
    	a[2][i]=a[2][i-1];
    	if(i&1) a[1][i]+=s[i];
    	else a[2][i]+=s[i];
    }
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
    
    
    	if(i&1)
    	{
    
    
    		ans=max(a[1][i]+a[2][n]-a[2][i],ans);
    	}
    	else
    	{
    
    
    		ans=max(ans,a[2][i]+a[1][n]-a[1][i]);
    	}
    }
    cout<<ans<<endl;
}

Guess you like

Origin blog.csdn.net/weixin_45755679/article/details/107294954