codeforces 912(a,b,c)

补写一下题解

A

#include<iostream>
#include<vector>

using namespace std;

void solve() {
	int n, k; cin >> n >> k;
	vector<int>a(n + 3);
	for (int i = 1; i <= n; i++)cin >> a[i];
	bool flag = 1;
	for (int i = 1; i < n; i++) {
		if (a[i] > a[i + 1])flag = 0;
	}
	if (flag) { cout << "YES" << '\n'; return; }
	if (k == 1) { cout << "NO" << '\n'; return; }
	cout << "YES" << '\n';
}

int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

B

思路

难点就在位运算上,位运算
题目中是进行了异或运算,我们做一个与运算逆过来就行了,然后再比较是否相等,注意一开始数组元素要把每一个二进制位开开

#include<iostream>
#include<vector>

using namespace std;

const int M = 1e3 + 9;
int b[M][M];

void solve() {
	int n; cin >> n;
	vector<int>a(n + 3);
	for (int i = 0; i < n + 3; i++)a[i] = (1 << 30) - 1;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++) {
			cin >> b[i][j];
			if (i != j) {
				a[i] &= b[i][j];
				a[j] &= b[i][j];
			}
		}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (i != j && (a[i] | a[j])!= b[i][j]) {
				cout<<"NO"<<'\n';
				return;
			}
		}
	}
	cout << "YES" << '\n';
	for (int i = 1; i <= n; i++)cout << a[i] << ' ';
	cout << '\n';
}

int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}

C

思路

就是分段很麻烦,贪心我也贪不出来,想后缀和也不会优化,猛然发现了大佬的化乘法为加法的骚操作,确实牛逼

#include<iostream>
#include<vector>

using namespace std;

using ll=long long;

void solve() {
	int n; cin >> n;
	vector<int>a(n + 3);
	for (int i = 1; i <= n; i++)cin >> a[i];
	ll ans = 0;
	ll sum = 0;
	for (int i = n; i > 0; i--) {
		ans += a[i];
		if (ans > 0) {
			sum += ans;
		}
	}
	if (ans < 0)sum += ans;
	cout << sum << '\n';
}

int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值