Codeforces Round #778 (Div. 1 + Div. 2, based on Technocup 2022 Final Round)(匿名函数的自我调用)

Problem - A - Codeforces

数不改变,只需要最大值和次大值相加就好了

AC代码:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

void solve() {
	int n;
	cin >> n;
	vector<int> a(n);
	for (int &x : a) {
		cin >> x;
	}
	sort(a.begin(), a.end());
	cout << 1ll * a[n - 1] + a[n - 2] << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);cout.tie(nullptr);
#ifdef ONLINE_JUDGE
#else
	freopen("in.txt", "r", stdin);
#endif
	int _;
	cin >> _;
	while (_--) {
		solve();
	}

	return 0;
}

Problem - B - Codeforces

如果后面有开头的元素,那么就删去,如果没有直接输出剩下的字符串

AC代码:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

void solve() {
	string s;
	cin >> s;
	int len = s.size();
	map<char, int> mp;
	int now = 0;
	for (int i = 0; i < len; i++) {
		mp[s[i]]++;
	}
	for (int i = 0; i < len; i++) {
		if (mp[s[i]] > 1) {
			now++;
			mp[s[i]]--;
		}
		else {
			break;
		}
	}
	for (int i = now; i < len; i++) {
		cout << s[i];
	}
	cout << '\n';
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);cout.tie(nullptr);
#ifdef ONLINE_JUDGE
#else
	freopen("in.txt", "r", stdin);
#endif
	int _;
	cin >> _;
	while (_--) {
		solve();
	}

	return 0;
}

Problem - C - Codeforces

逆向思维,对整个蛋糕开始切分,如果有这么大的一块蛋糕就剪枝,否则继续dfs下去,最后判断最后还有没有某种大小的蛋糕没有切到

AC代码:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

void solve() {
	int n;
	cin >> n;
	vector<int> a(n);
	i64 sum = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		sum += a[i];
	}
	multiset<i64> r(a.begin(), a.end());
	int cnt = 1;
	auto dfs = [&](auto self, auto s) -> void {
		if (cnt > n) {
			return;
		}
		if (r.find(s) != r.end()) {
			r.erase(r.find(s));
			return;
		}
		cnt++;
		self(self, s >> 1);
		self(self, s - (s >> 1));
	};
	dfs(dfs, sum);
	if (cnt == n && r.empty()) {
		cout << "YES" << '\n';
	}
	else {
		cout << "NO" << "\n";
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);cout.tie(nullptr);
#ifdef ONLINE_JUDGE
#else
	freopen("in.txt", "r", stdin);
#endif
	int _;
	cin >> _;
	while (_--) {
		solve();
	}

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值