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;
}
相关推荐
JohnFF1 分钟前
48. 旋转图像
数据结构·算法·leetcode
bbc1212261 分钟前
AT_abc306_b [ABC306B] Base 2
算法
生锈的键盘9 分钟前
推荐算法实践:movielens数据集
算法
董董灿是个攻城狮10 分钟前
Transformer 通关秘籍9:词向量的数值实际上是特征
算法
林泽毅19 分钟前
SwanLab x EasyR1:多模态LLM强化学习后训练组合拳,让模型进化更高效
算法·llm·强化学习
小林熬夜学编程21 分钟前
【高并发内存池】第八弹---脱离new的定长内存池与多线程malloc测试
c语言·开发语言·数据结构·c++·算法·哈希算法
刚入门的大一新生27 分钟前
归并排序延伸-非递归版本
算法·排序算法
独好紫罗兰32 分钟前
洛谷题单3-P1980 [NOIP 2013 普及组] 计数问题-python-流程图重构
开发语言·python·算法
独好紫罗兰37 分钟前
洛谷题单3-P1009 [NOIP 1998 普及组] 阶乘之和-python-流程图重构
开发语言·python·算法
曦月逸霜1 小时前
蓝桥杯高频考点——高精度(含C++源码)
c++·算法·蓝桥杯