码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #580 (Div. 2)

时间:2019-08-24 00:46:48      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:++   ret   题意   cstring   pos   clu   奇数   偶数   hid   

A. Choose Two Numbers

题意:让你从A集合里取一个数a,B集合里也取一个数b,使得a+b同时不存在于集合A和集合B中

思路:直接取A和B集合中最大元素,最大元素相加必然不在A集合或者B集合里

技术图片
    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include <cstring> 
    using namespace std;
    int main()
    {
        int n, m;
        scanf("%d", &n);
        int a[n];
        for (int i = 0; i < n; i++)
            scanf("%d", &a[i]);
        scanf("%d", &m);
        int b[m];
        for (int i = 0; i < m; i++)
            scanf("%d", &b[i]);
         printf("%d %d", *max_element(a, a+n), *max_element(b, b+m));
    }
View Code

 B.Make Product Equal One

题意:给你一段序列,让你经过k次操作后使得这个序列积为1。

思路:统计0的个数,统计小于等于-1的个数,之后分三种情况讨论:

1.负数个数为偶数:直接abs(abs(a[i])-1)遍历一遍即可

2.负数个数为奇数,且有0的存在:直接abs(abs(a[i])-1)遍历一遍即可(因为将要将其中一个-1转成1,0必然是要变成1或-1的所以不用考虑费用)

3.负数个数为奇数数,且0不存在:abs(abs(a[i])-1)遍历一遍之后+2即可(因为将要将其中一个-1转成1

技术图片
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    int n;
    scanf("%d", &n);
    int a[n];
    int cnt1 = 0, cnt0 = 0, mminus = 0x3f3f3f3f;
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        if (a[i] < 0)
        {
            cnt1++;
            if (abs(a[i]) < mminus)
                mminus = a[i];
        }
        if (a[i] == 0) cnt0++;
    }
    if (cnt1 % 2 == 0)
    {
        long long ans = 0;
        for (int i = 0; i < n; i++)
            ans += abs(abs(a[i])-1);
        printf("%lld\n", ans);
        return 0;
    }
    else
    {
        if (cnt0)
        {
            long long ans = 0;
            for (int i = 0; i < n; i++)
                ans += abs(abs(a[i])-1);
            printf("%lld\n", ans);
            return 0;
        }
        else
        {
            long long ans = 0;
            for (int i = 0; i < n; i++)
                ans += abs(abs(a[i])-1);
            printf("%lld\n", ans+2);
        return 0;
        }
    }
}
View Code

C. Almost Equal

待补。

Codeforces Round #580 (Div. 2)

标签:++   ret   题意   cstring   pos   clu   奇数   偶数   hid   

原文地址:https://www.cnblogs.com/Vikyanite/p/11402994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!