Codeforces Round 901 (Div. 2)

B. Jellyfish and Game

Codeforces Round 901 (Div. 2)_第1张图片

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
#include
#include
#include
#include
#include
#include
#include 
#include
#include
#include
#include
#include
#include 
#include
#include 

#define inf 0x3f3f3f3f
#define int long long
const int N =1e6 + 10;
//#include 
typedef long long ll;
#include
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

Codeforces Round 901 (Div. 2)_第2张图片

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
#include
#include
#include
#include
#include
#include
#include 
#include
#include
#include
#include
#include
#include 
#include
#include 

#define inf 0x3f3f3f3f
#define int long long
const int N =1e6 + 10;
//#include 
typedef long long ll;
#include
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;
}

你可能感兴趣的:(算法,c++,图论)