Codeforces Round 901 (Div. 2)

文章讲述了Jellyfish和另一个玩家如何在分配苹果时通过交换确保每个人获得公平份额,涉及找到最大值和最小值的位置,以及在特定条件下进行分割的操作。
摘要由CSDN通过智能技术生成

B. Jellyfish and Game

Example

input

Copy

 

4

2 2 1

1 2

3 4

1 1 10000

1

2

4 5 11037

1 1 4 5

1 9 1 9 8

1 1 1

2

1

output

Copy

6
1
19
2

Note

In the first test case, Jellyfish will swap the apple of value 11 and 44.

In the second test case, both players will swap the two apples 10,00010,000 times.

In the fourth test case, Jellyfish will do nothing.

无论有多少个回合,中间的过程无非就是用最小的换对方最大的。而两个为了最优,最小的和最大的一定在不同的人手上(因为谁都想把最大的留在自己这边,最小的留在对方那边)

那么就只需要关注:最后一次交换权在谁的手上

#include<set>
#include<list>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<string.h>
#include <stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<stack>
#include <utility>
#include<map>
#include <vector>

#define inf 0x3f3f3f3f
#define int long long
const int N =1e6 + 10;
//#include <bits/stdc++.h>
typedef long long ll;
#include<iostream>
using namespace std;

//long long MAX(long long a, long long b) { return a < b ? b : a; }

int a[60], b[60];
signed main() {
    int T;
    cin >> T;
    while (T--) {
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));

        //找出最大和最小在哪边
        int n, m, k;
        cin >> n >> m >> k;
        int ans = 0;

        for (int i = 1; i <= n; i++) {
            cin >> a[i];            
        }
        for (int i = 1; i <= m; i++) {
            cin >> b[i];
            
        }
        sort(a + 1, a + n + 1);
        sort(b + 1, b + m + 1);

        if (a[1] < b[m]) {
            swap(a[1], b[m]);
            sort(a + 1, a + n + 1);
            sort(b + 1, b + m + 1);
        }
        
        if (k % 2 == 0) {
            swap(a[n], b[1]);
        }
        for (int i = 1; i <= n; i++) {
            ans += a[i];
        }
        cout << ans << endl;
    }

    return 0;
}

C. Jellyfish and Green Apple

input

Copy

 

4

10 5

1 5

10 4

3 4

output

Copy

0
-1
2
5

If there are 7 apples for 4 persons,you should give 1 apple for each person,so there are 3 apples(3 pieces) now.And we know no one will get a whole apple(a whole piece),so we should devide every piece into two,now we have 6 pieces.After giving every person a piece,there are 2 pieces now.As what I just said,no one will get a whole piece,so we should devide every piece into two,now there are 4 pieces for 4 person,perfect!

#include<set>
#include<list>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<string.h>
#include <stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<sstream>
#include<stack>
#include <utility>
#include<map>
#include <vector>

#define inf 0x3f3f3f3f
#define int long long
const int N =1e6 + 10;
//#include <bits/stdc++.h>
typedef long long ll;
#include<iostream>
using namespace std;

//long long MAX(long long a, long long b) { return a < b ? b : a; }

signed main() {
    int T;
    cin >> T;
    while (T--) {
        int n, m; cin >> n >> m;
        int t = 0;
        int ans = 0;
        while (t < 2000) {
            t++;
            if (n % m == 0) break;
            n %= m;
            ans += n;//要切n刀
            n *= 2;

        }
        if (t >= 2000) {
            cout << -1 << endl;
            continue;
        }
        cout << ans << endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值