codeforces div2 525 部分题解

A. Ehab and another construction problem

题目:

传送门A

按照题目的要求,只需要将输入的x打印两遍就可以了。

注意还有x=1的情况,这样只需要打印-1即可。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
    int x;
    scanf("%d",&x);
    if(x==1)
    {
        printf("-1\n");
    }
    else
    {
        printf("%d %d\n",x,x);
    }
    return 0;
}

 B. Ehab and subtraction

题目:

传送门B

此题模拟即可。

可以发现,一个数只需要减去前一个数就是要处理的结果。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int n,k;
int main()
{
    scanf("%d%d",&n,&k);
    for (int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    sort(a,a+n);
    int j=0,rest=0;
    for (int i=0;i<k;)
    {
        if(j<n&&a[j]-rest)
        {
            i++;
            printf("%d\n",a[j]-rest);
            rest=a[j++];
        }
        else if(j<n&&a[j]-rest==0)
        {
            j++;
        }
        else if(j>=n)
        {
            i++;
            printf("0\n");
        }
    }
    return 0;
}

 C. Ehab and a 2-operation task

题目:

传送门C

此题模拟即可,从后往前调到想要的数即可。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int maxn=2005;
typedef long long ll;
int n;
ll a[maxn];
ll sum=0;
vector<int>ans,ans2;
int main()
{
    scanf("%d",&n);
    for (int i=0;i<n;i++)
    {
        scanf("%lld",&a[i]);
    }
    ll stand;
    if(a[n-1]<n+1)
    {
        stand=n+1;
    }
    else
    {
        stand=a[n-1]+1;
    }
    for (int i=n-1,j=stand-1;i>=0;i--,j--)
    {
        if((a[i]+sum)%stand!=j)
        {
            ll t=(a[i]+sum)%stand;
            ll yu=(t/stand+1)*stand+j-t;
            ans.push_back(yu);
            ans2.push_back(i+1);
            sum+=yu;
        }
    }
    printf("%d\n",ans.size()+1);
    for (int i=0;i<ans.size();i++)
        {
            printf("1 %d %d\n",ans2[i],ans[i]);
        }
    printf("2 %d %d\n",n,stand);
    return 0;
}


 

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/84826640
今日推荐