Codeforces Round #618 (Div.2)

30 篇文章 0 订阅

A题:http://codeforces.com/contest/1300/problem/A

题意:给你一串数字,你可以对任意一个数字+1,然后问你最少需要加几个1才能使这一串数字的和和积都为0。
思路:这道题的话,首先起码要满足每个数都不等于0,遍历时遇到为0的数先对其+1,同时ans++(进行了一次操作),同时sum(数列和)加上更新后的数(如果没更新就加上原数)。遍历完成后如果sum等于0的话,可以随便找一个数+1,同时ans++,这样能满足操作最少且sum不为0。最后输出ans即可。

AC代码:

#include <bits/stdc++.h>
typedef long long ll;
const int maxx=1010;
const int inf=0x3f3f3f3f;
using namespace std;
int main()
{
    int t,n,a;
    cin>>t;
    while(t--)
    {
        cin>>n;
        ll ans=0,sum=0;
        for(int i=1; i<=n; i++)
        {
            cin>>a;
            if(a==0)
            {
                a++;
                ans++;
            }
            sum+=a;
        }
        if(sum==0)
            ans++;
        cout<<ans<<endl;
    }
    return 0;
}

 

B题:http://codeforces.com/contest/1300/problem/B

题意:给2*n个数,分成两组,每组个数是奇数个,让你求两组中位数之差最小。
思路:这道题的话,对原序列排序,取中间两个做差输出即可。
AC代码:

#include <bits/stdc++.h>
typedef long long ll;
const int maxx=200010;
const int inf=0x3f3f3f3f;
using namespace std;
int a[maxx];
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=1; i<=2*n; i++)
            cin>>a[i];
        sort(a+1,a+2*n+1);
        cout<<a[n+1]-a[n]<<endl;
    }
    return 0;
}

 

C题:http://codeforces.com/contest/1300/problem/C

题意:给定函数:f(x,y)=(x|y)−y。给定一个序列,可以任意调换序列内顺序,使得f(f(…f(f(a1,a2),a3),…an−1),an)值最小。
输出调换顺序后的序列。
思路:这道题的话,我们可以发现,对于函数f(f(…f(f(a1,a2),a3),…an−1),an)值即为,将a数组所有的数表示为二进制后,
当a1的第pos位为1且a2…a3的第pos位均不为1时,所有这样的第pos位的权重之和。

AC代码:

#include <bits/stdc++.h>
typedef long long ll;
const int maxx=100010;
const int inf=0x3f3f3f3f;
using namespace std;
int a[maxx];
int b[maxx][35];
int c[maxx];
int main()
{
    int n;
    cin>>n;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
        int ans=a[i];
        int cnt=0;
        while(ans)
        {
            b[i][++cnt]=ans%2;
            c[cnt]+=ans%2;
            ans/=2;
        }
    }
    int maxn=-1;
    int pos=0;
    for(int i=1; i<=n; i++)
    {
        int tot=1;
        int sum=0;
        for(int j=1; j<=32; j++)
        {
            if(b[i][j]==1 && c[j]==1)
            {
                sum+=tot;
            }
            tot*=2;
        }
        if(sum>maxn)
        {
            pos=i;
            maxn=sum;
        }
    }
    cout<<a[pos]<<" ";
    for(int i=1; i<=n; i++)
    {
        if(i==pos)
            continue;
        else
            cout<<a[i]<<" ";
    }
    cout<<endl;
    return 0;
}

 

D题:http://codeforces.com/contest/1300/problem/D

题意:给定一个图形(凸多边形),平移这个图形,使得原点在这个图形内(包括边和点),求这两个图形是否相似。
思路:这道题的话,判断这个图形是不是中心对称即可。

AC代码:

#include <bits/stdc++.h>
typedef long long ll;
const int maxx=100010;
const int inf=0x3f3f3f3f;
using namespace std;
struct node
{
    int x,y;
} edge[maxx];
int main()
{
    int n;
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>edge[i].x>>edge[i].y;
    edge[0].x=edge[n].x;
    edge[0].y=edge[n].y;
    if(n%2==1)
        cout<<"NO"<<endl;
    else
    {
        int flag=0;
        for(int i=0; i<n/2-1; i++)
        {
            if(edge[i+1].x-edge[i].x!=edge[i+n/2].x-edge[i+1+n/2].x || edge[i+1].y-edge[i].y!=edge[i+n/2].y-edge[i+1+n/2].y)
            {
                flag=1;
                break;
            }
        }
        if(flag)
            cout<<"NO"<<endl;
        else
            cout<<"YES"<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值