Codeforces Round #732 (Div. 2) A,C

A. AquaMoon and Two Arrays

题意:
给定2个数组a,b
对a可以进行以下操作
选取i,j,让ai - 1,aj + 1

求最少操作次数和方案

思路:
把一个数变成另一个数,也就是把他们的差补掉,首先求出ab的差,然后每次让负数 +1,正数 - 1,当正负数绝对值和相等的时候才有解

#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

const int N = 110;
int a[N];

int main()
{
    int t;
    cin >> t;
    while(t --)
    {
        int n;
        cin >> n;
        for(int i = 1;i <= n;i ++) cin >> a[i];
        int sum1 = 0,sum2 = 0;
        for(int i = 1;i <= n;i ++)
        {
            int t;
            cin >> t;
            a[i] -= t;
            if(a[i] < 0) sum1 += a[i];
            else sum2 += a[i];
        }
        
        
        if(abs(sum1) != abs(sum2)) cout << -1 << endl;
        else
        {
            vector<int> b,c;
            
            for(int i = 1;i <= n;i ++) 
            {
                while(a[i] < 0) b.push_back(i),a[i] ++;
                while(a[i] > 0) c.push_back(i),a[i] --;
            }
                
            cout << abs(sum1) << endl;
            for(int i = 0;i < b.size();i ++) cout << c[i] << " " << b[i] << endl;
        }
    }
    return 0;
}

C. AquaMoon and Strange Sort

题意:
给定一个数组,让它变成有序的,并且每个元素只能移动偶数次

问能不能做到

思路:
每个元素只能移动,也就代表着,奇数位置只会和奇数位置交换,偶数位置一样,所以只要把奇偶位置分别取出来,最后合并看是不是有序的

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int check(vector<int> c)
{
    for(int i = 1;i < c.size();i ++) if(c[i] < c[i - 1]) return 0;
    return 1;
}

int main()
{
    int t;
    cin >> t;
    while(t --)
    {
        vector<int> a,b,c;
        int n;
        cin >> n;
        for(int i = 1;i <= n;i ++)
        {
            int t;
            cin >> t;
            if(i % 2) a.push_back(t);
            else b.push_back(t);
        }
        
        sort(a.begin(),a.end());
        sort(b.begin(),b.end());
        
      //  for(auto x:a) cout << x << " ";
    //    cout << endl;
    //    for(auto x:b) cout << x << " ";
        //cout << endl;
        
        int j ,k;
        j = 0,k = 0;
        for(int i = 1;i <= n;i ++)
        if(i % 2) c.push_back(a[j ++]);
        else c.push_back(b[k ++]);

        //for(auto x:c) cout << x << " ";
        
        if(check(c)) puts("YES");
        else puts("NO");
        
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值