Codeforces Round 901 (Div. 2)

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.

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

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

cpp 复制代码
#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!

cpp 复制代码
#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;
}
相关推荐
梭七y6 分钟前
【力扣hot100题】(020)搜索二维矩阵Ⅱ
算法·leetcode·职场和发展
v维焓17 分钟前
C++(思维导图更新)
开发语言·c++·算法
ylfhpy31 分钟前
Java面试黄金宝典22
java·开发语言·算法·面试·职场和发展
Phoebe鑫38 分钟前
数据结构每日一题day9(顺序表)★★★★★
数据结构·算法
CYRUS_STUDIO1 小时前
Frida Hook Native:jobjectArray 参数解析
android·c++·逆向
榆榆欸1 小时前
4.Socket类、InetAddr类、Epoll类实现模块化
linux·c++·tcp/ip
..过云雨1 小时前
11. 【C++】模板进阶(函数模板特化、类模板全特化和偏特化、模板的分离编译)
开发语言·c++
烁3471 小时前
每日一题(小白)动态规划篇2
算法·动态规划
南玖yy2 小时前
数据结构C语言练习(栈)
c语言·数据结构·算法
BC橡木2 小时前
C++ IO流
c++