Codeforces Round 901 (Div. 2)A~C

D题后面再补啦啦啦啦啦

A题

Problem - A - Codeforces

典型的模拟,每次倒计时到1时加时间就行,加的时间取min(a-1,x[i])

#include<bits/stdc++.h>

using namespace std;
const int N = 100010;
using ll = long long;

void solve() {
    ll a, b, n; cin >> a >> b >> n;
    vector<ll>x(n);
    for (int i = 0; i < n; i++) {
        cin >> x[i];
    }

    ll ans = b;
    sort(x.begin(), x.end());
    for (int i = 0; i < n; i++) {
        if (x[i] < a) {
            ans += x[i];
        }
        else {
            ans += a - 1;
        }
    }
    cout << ans << '\n';
}


signed main() {
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t=1;
    cin >> t;

    while (t--)
        solve();
}

B题

Problem - B - Codeforces

分为两步看:

第一步,就是第一轮时,看A中最小与B中最大交换是否值得,值得就交换,不值得也算第一轮过去,如果k==1,就到此为止,输出就行。

第二步,就是之后所有的轮数了,k的奇偶决定了A,B两个数列里最大最小值的归属,谁最后一个行动,谁就拿到最大值然后把最小值交换给对方。

#include<bits/stdc++.h>

using namespace std;
using ll = long long;

void solve(){
	int n, m, k; cin >> n >> m >> k;
	vector<int>a(n);
	vector<int>b(m);
	ll ans = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < m; i++) {
		cin >> b[i];
	}
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());

	int minn = min(a[0], b[0]);
	int maxx = max(a[n - 1], b[m - 1]);

	if (a[0] < b[m - 1]) {
		swap(a[0], b[m - 1]);
	}

	if (k == 1) {
		for (int i = 0; i < n; i++) {
			ans += 1ll * a[i];
		}
		cout << ans << '\n';
		return;
	}

	sort(a.begin(), a.end());
	if (k & 1) {
		for (int i = 0; i < n - 1; i++) {
			ans += 1ll * a[i];
		}
		ans += 1ll * maxx;
		cout << ans << '\n';
	}
	else {
		for (int i = 0; i < n - 1; i++) {
			ans += 1ll * a[i];
		}
		ans += 1ll * minn;
		cout << ans << '\n';
	}
}
signed main(){

	ios::sync_with_stdio(false);
	cin.tie(0);
	std::cout.tie(0);

	int t = 1;
	cin >> t;
	while (t--)
		solve();
	return 0;
}

C题:

Problem - C - Codeforces

第一步,使n=n%m,也就是先一人拿n/m个果,然后剩下的n就严格小于m。

第二步,剩下n个果子平均分给m个人,给n切为大小相同的片,至少lcm(n,m)(最小公倍数)片才能均分。lcm(n,m)=n*m/gcd(n,m),也就是平均每人n/gcd(n,m)片,也就是每个果子分成m/gcd(n,m)片,因为果子只能对半分,m/gcd(n,m)必须是2的整数次幂。

第三步,每人n/gcd(n,m)片,我们可以先玩个1024,把最小的两片合成中片,两个中片合成大片。。。。。其实就是二进制表示的n/gcd(n,m)中1的个数,假设这个数字是x,每切一刀果子片数+1,那么一共要x*m片,初始有n个,答案就是x*m-n

#include<bits/stdc++.h>

using namespace std;
using ll = long long;

int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}

void solve(){
	int n, m; cin >> n >> m;
	n = n % m;

	if (!n) {
		cout << 0 << '\n';
		return;
	}

	ll lc = 1ll * n * m / (1ll * gcd(n, m));
	ll x = lc / m;
	ll y = lc / n;
	ll t = y & -y;
	if (t != y) {
		cout << -1 << '\n';
		return;
	}

	int cnt = 0;
	while (x) {
		x = x - (x & -x);
		cnt++;
	}
	cout << 1ll * cnt * m - n << '\n';
}
signed main(){

	ios::sync_with_stdio(false);
	cin.tie(0);
	std::cout.tie(0);

	int t = 1;
	cin >> t;
	while (t--)
		solve();
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值