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;
}