Codeforces Round 922 (Div. 2)

文章介绍了三道Codeforces竞赛中的问题:A.贪心算法解决BrickWall问题,B.通过比较排序最小化Inversions,C.利用位运算和贪心策略求解XOR-distance。展示了在编程竞赛中处理这些问题的方法和思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A.贪心

// Problem: A. Brick Wall
// Contest: Codeforces - Codeforces Round 922 (Div. 2)
// URL: https://codeforces.com/contest/1918/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 2e6 + 9;
int a[N];
void Lan(){
	int n,m;
	cin>>n>>m;
	if(m<2){
		cout<<"-"<<n/2<<'\n';
	}else{
		if(m%2){
			cout<<(m/2)*n<<'\n';//其中一个变成3
		}else{		
			cout<<(m/2)*n<<'\n';//都是2
		}
	}
		
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int q;
	cin>>q;
	while (q--) {
		Lan();
	}
	return 0;
}

B.排序

// Problem: B. Minimize Inversions
// Contest: Codeforces - Codeforces Round 922 (Div. 2)
// URL: https://codeforces.com/contest/1918/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 2e6 + 9;
struct node{
	int x,y;
}a[N];
bool cmp(node a,node b){
		return a.x<b.x;
}
void Lan(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i].x;
	}
	for(int i=1;i<=n;i++){
		cin>>a[i].y;
	}
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;i++){
		cout<<a[i].x<<" ";
	}
	cout<<'\n';
	for(int i=1;i<=n;i++){
		cout<<a[i].y<<" ";
	}
	cout<<'\n';
	
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int q;
	cin>>q;
	while (q--) {
		Lan();
	}
	return 0;
}

C.位运算,贪心(!)

分析每一个位

10100

01010

如果a,b这个位置是一样的,可以发现r无论是什么都没有办法变化值

因此相同的位置跳过

应该是16-8-4(这块就应该用^1反转)-2

贪心:(正-负-负-负)这样绝对值最小,以及(负-正-正-正)

因此判断ans*(na-nb)的符号

同号改,异号不变

// Problem: C. XOR-distance
// Contest: Codeforces - Codeforces Round 922 (Div. 2)
// URL: https://codeforces.com/contest/1918/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define eps 1e-5
#define INF 4e18
using namespace std;
typedef long long ll;
void Lan(){
	ll a,b,r;
	cin>>a>>b>>r;
	bool high=true;//最高位
	ll ans=0;
	for(ll i=60;i>=0;i--){//枚举每一位
		bool na=(a&(1ll<<i));
		bool nb=(b&(1ll<<i));
		if(na==nb){//都是11,00跳过
			continue;
		}
		if(high){//最高位且位置上数字不一样
			ans=(1ll<<i)*(na-nb);//第一个大的
			high=false;
		}else{
			if(r>=(1ll<<i) && (na-nb)*ans>0){//r要在这个范围才可以操作,位置上的数字相减与得到的数字是同号说明要贪心做,用^1转变
				r-=(1ll<<i);//用过^1
				ans-=(na-nb)*(1ll<<i);//变成减去
			}else{//异号相加不用变这个位置上是0
				ans+=(na-nb)*(1ll<<i);//r不需要在这个位置上使用过直接+
			}
		}
	}
	ans=abs(ans);//绝对值
	cout<<ans<<'\n';//得到结果
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int q;
	cin>>q;
	while (q--) {
		Lan();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值