Codeforces Round 922 (Div. 2) A-E

链接:Dashboard - Codeforces Round 922 (Div. 2) - Codeforces

A. Brick Wall

题意:给定一个n*m的区域,求用1*k(k可以变化)的方块去放时,横着放-竖着放的最大值

思路:显然有尽量用横着去铺的想法,那就是都用1*2的方块,如果m是奇数就需要有一块区域是用1*3的

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
typedef long double ld;
//#define endl '\n'
#define pii pair<int, int>
const int N = 1e6+8;
const int M = 1e2 + 5;
const ll mod = 1e8+1;
const int inf = 0x3f3f3f3f;
const ll inff = 1e18;

void solve(){
	ll x,y;
	cin>>x>>y;
	if(y%2==1){
		cout<<x*(y-3)/2+x<<endl;
	}
	else cout<<x*(y/2)<<endl;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0); 

    int T;
    T = 1;
   	cin>>T;
    for (int i = 1; i <= T; ++i)
    {
        solve();
    }
    //system("pause");
    return 0;
}
// 16 6  3  16

B. Minimize Inversions

题意:给出长度为n的序列a和b,可以任意调整a序列中元素位置,但是调整后b也要做出相应的调整,求a,b总共的逆序对最少是多少

思路:将a调整为升序后,b的逆序对数即为答案;反过来想,如果a此时有序,那么调整b有序一对必然会导致a的一对无序,代价不会小于成效。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
typedef long double ld;
//#define endl '\n'
#define pii pair<int, int>
const int N = 1e6+8;
const int M = 1e2 + 5;
const ll mod = 1e8+1;
const int inf = 0x3f3f3f3f;
const ll inff = 1e18;


int f[N][27];
int book[N];
int cnt;
void solve(){
	int n;cin>>n;
	vector<pair<int,int>> a(n);
	for(int i=0;i<n;i++) cin>>a[i].first;
	for(int j=0;j<n;j++) cin>>a[j].second;
	sort(a.begin(),a.end());
	for(int i=0;i<n;i++){
		cout<<a[i].first<<" ";
	}
	cout<<endl;
	for(int j=0;j<n;j++){
		cout<<a[j].second<<" ";
	}
	cout<<endl;
	
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0); 

    int T;
    T = 1;
   	cin>>T;
    for (int i = 1; i <= T; ++i)
    {
        solve();
    }
    //system("pause");
    return 0;
}
// 16 6  3  16

C. XOR-distance

题意:如题

思路:假设a<b,那么对于要使目标更小的话,要求有一个x异或后能让ab相互接近,枚举a、b每一位,保证b要大于a,所以第一位要跟着b来,让b异或后为1,其他地方尽量让a该位异或后为1,b为0,同时要检查x是否会大于r,当然对于ab该位相同的就不用考虑了,因为都是一样的效果了。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
typedef long double ld;
//#define endl '\n'
#define pii pair<int, int>
const int N = 1e6+8;
const int M = 1e2 + 5;
const ll mod = 1e8+1;
const int inf = 0x3f3f3f3f;
const ll inff = 1e18;


int f[N][27];
int book[N];
int cnt;
void solve(){
	int64_t a,b,r;
	cin>>a>>b>>r;
	int64_t ans=0;
	if(a>b) swap(a,b);
	bool flag=true;
	for(int i=60;i>=0;i--){
		int pa=((a&(1LL<<i))!=0);
		int pb=((b&(1LL<<i))!=0);
		if(pa==pb){
			continue;
		}
		else{
			if(flag){
				flag=false;
			}
			else{
				if(pa==0&&ans+(1LL<<i)<=r){
					ans+=(1LL<<i);
					a^=(1LL<<i);
					b^=(1LL<<i);
				}
			}
		}
	}
	cout<<b-a<<endl;
	
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0); 

    int T;
    T = 1;
   	cin>>T;
    for (int i = 1; i <= T; ++i)
    {
        solve();
    }
    //system("pause");
    return 0;
}

D. Blocking Elements

题意:给出一数组a,可以选定其中其中k(自定)个数字作为边界,然后整个数组的代价就是各个被边界分开的区间的区间和中的最大值和边界数字和的最大值,要使得这个最大值最小。

思路:首先是二分来确定这个最大值mid,然后是检查这个最大值mid是否正确;对于一个区间连续和如果大于mid,那么就要把之前进来的划分到另外一个区间去,这里可以利用小跟堆来让边界和最小,该点如果要作为边界的时候,从哪个点推上来,就是小跟堆的堆顶。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
typedef long double ld;
//#define endl '\n'
#define pii pair<int, int>
const int N = 1e5+8;
const int M = 1e2 + 5;
const ll mod = 1e8+1;
const int inf = 0x3f3f3f3f;
const ll inff = 1e18;

ll a[N];
int n;
bool check(ll maxn){
	set<pair<ll,int>> q;
	vector<ll> dp(n+1);
	int pos=0;
	dp[0]=0;
	q.insert({0,0});
	ll sum=0;
	for(int i=1;i<=n;i++){
		while(sum>maxn){
			sum-=a[pos+1];
			q.erase({dp[pos],pos});
			pos++;
		}
		dp[i]=q.begin()->first+a[i];
		q.insert({dp[i],i});
		sum+=a[i];
	}
	sum=0;
	bool flag=false;
	for(int i=n;i>=1;i--){
		if(sum>maxn){
			break;
		}
		if(sum<=maxn&&dp[i]<=maxn){
			flag=true;
			break;
		}
		sum+=a[i];
	}
	//cout<<flag<<endl;
	return flag;
}

void solve(){
	
	cin>>n;
	ll sum=0;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		sum+=a[i];
	}
	
	ll l=0,r=sum+1;
	ll mid;
	while(l<r){
		mid=(l+r)>>1;
		if(check(mid)){
			//cout<<"mid: "<<mid<<endl;
			r=mid; 
		}
		else l=mid+1;
	}
	cout<<l<<endl;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0); 

    int T;
    T = 1;
   	cin>>T;
    for (int i = 1; i <= T; ++i)
    {
        solve();
    }
    //system("pause");
    return 0;
}

E. ace5 and Task Order

题意:交互题,给出一个n代表有n个未知顺序的序列,每次提出是“? i”,代表提问ai的数字和初定的x(未知)关系,小于就回答“<”,然后x-1,以此类推;求最后可以得到序列是什么样的。

思路:随机抽取序列中的一个数字,询问他直到回答等于,说明此时x==该数字的位置,然后将回答小于的数字放到左边,大于的放到右边,各个分区间操作后再合并。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//typedef __int128 lll;
typedef long double ld;
//#define endl '\n'
#define pii pair<int, int>
const int N = 1e5+8;
const int M = 1e2 + 5;
const ll mod = 1e8+1;
const int inf = 0x3f3f3f3f;
const ll inff = 1e18;
mt19937 rnd(593);

inline char query(int i){
	cout<<"? "<<i+1<<endl;
	char c;
	cin>>c;
	return c;
}

void quicksort(vector<int> &a,vector<int> &ord){
	if(a.size()==0) return;
	int mid=rnd()%a.size();
	while(query(a[mid])!='=');
	vector<int> l,r;
	for(int i=0;i<a.size();i++){
		if(i==mid) continue;
		if(query(a[i])=='<'){
			l.push_back(a[i]);
		}
		else{
			r.push_back(a[i]);
		}
		query(a[mid]);
	}
	vector<int> l_ord,r_ord;
	quicksort(l,l_ord);
	quicksort(r,r_ord);
	for(int i=0;i<l_ord.size();i++){
		ord.push_back(l_ord[i]);
	}
	ord.push_back(a[mid]);
	for(int i=0;i<r_ord.size();i++){
		ord.push_back(r_ord[i]);
	}
	return;
}

void solve(){
	
	int n;
	cin>>n;
	vector<int> a,ord;
	for(int i=0;i<n;i++) a.push_back(i);
	quicksort(a,ord);
	cout<<"! ";
	vector<int> ans(n);
	for(int i=0;i<n;i++){
		ans[ord[i]]=i;
	} 
	for(int i=0;i<n;i++){
		cout<<ans[i]+1<<" ";
	}
	cout<<endl;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0); 

    int T;
    T = 1;
   	cin>>T;
    for (int i = 1; i <= T; ++i)
    {
        solve();
    }
    //system("pause");
    return 0;
}
// 16 6  3  16
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃芒果的蘑菇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值