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;
}
相关推荐
林的快手26 分钟前
209.长度最小的子数组
java·数据结构·数据库·python·算法·leetcode
FeboReigns27 分钟前
C++简明教程(4)(Hello World)
c语言·c++
FeboReigns28 分钟前
C++简明教程(10)(初识类)
c语言·开发语言·c++
千天夜35 分钟前
多源多点路径规划:基于启发式动态生成树算法的实现
算法·机器学习·动态规划
zh路西法38 分钟前
【C++决策和状态管理】从状态模式,有限状态机,行为树到决策树(二):从FSM开始的2D游戏角色操控底层源码编写
c++·游戏·unity·设计模式·状态模式
从以前41 分钟前
准备考试:解决大学入学考试问题
数据结构·python·算法
.Vcoistnt1 小时前
Codeforces Round 994 (Div. 2)(A-D)
数据结构·c++·算法·贪心算法·动态规划
小k_不小1 小时前
C++面试八股文:指针与引用的区别
c++·面试
沐泽Mu1 小时前
嵌入式学习-QT-Day07
c++·qt·学习·命令模式
ALISHENGYA1 小时前
全国青少年信息学奥林匹克竞赛(信奥赛)备考实战之分支结构(实战训练三)
数据结构·c++·算法·图论