Codeforces Round #642 (Div. 3)(A B C D)

这一场完成了前三题加了分, 收获在于熟悉了优先队列以及运算符的重载(在D题中有所体现)

A. Most Unstable Array(数学)

#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ms(a,b) memset(a,b,sizeof(a))
#define rush() int T;cin>>T;while(T--)
#define ll long long

using namespace std;
const int N = 10e5;

int main(){
	FAST;
	rush(){
		ll n,m;
		cin>>n>>m;
		if(n == 1)cout<<"0"<<endl;
		else if(n == 2)cout<<m<<endl;
		else cout<<2 * m<<endl;
	}
	return 0;
} 

B. Two Arrays And Swaps(字符串)

#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ms(a,b) memset(a,b,sizeof(a))
#define rush() int T;cin>>T;while(T--)
#define ll long long

using namespace std;
const int N = 10e5;
int a[N],b[N];
int main(){
	FAST;
	rush(){
		int n,k;
		cin>>n>>k;
		for(int i = 0;i < n;i++)cin>>a[i];
		for(int i = 0;i < n;i++)cin>>b[i];
		sort(a,a + n);
		sort(b,b + n);
		int high = n - 1,low = 0;
		while(k-- && low < n){
			if(a[low] < b[high]){
				swap(a[low],b[high]);
				low++;
				high--;
			}
			else break;
		}
		int ans = 0;
		for(int i = 0;i < n;i++)ans += a[i];
		cout<<ans<<endl;
	}
	return 0;
} 

C. Board Moves(数学)

#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ms(a,b) memset(a,b,sizeof(a))
#define rush() int T;cin>>T;while(T--)
#define ll long long
using namespace std;
const int N = 1000;


//小学生找规律都比你牛!!!!!!!!!!!!!!!!!!!!!!!!!! 

int main(){
	FAST;
	rush(){
		ll n;
		ll ans = 0,term;
		cin>>n;
		for(ll i = 1;i <= (n - 1) / 2;i++)ans += i * i * 8;
		cout<<ans<<endl;
	}
	return 0;
} 

(补题)
D. Constructing the Array(优先队列)

#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ms(a,b) memset(a,b,sizeof(a)) 
#define rush() int T;cin>>T;while(T--)
#define ll long long
//80654425	May/19/2020 12:42UTC+8	answer_is_42	D - Constructing the Array	GNU C++14	Accepted	109 ms	5400 KB
//借鉴自: https://www.cnblogs.com/streamazure/p/12897396.html 
using namespace std;
const int N = 2e5 + 1000;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
//题解 每一次操作都会产生两个子序列 将这个子序列的头,尾和长度存入优先队列(按照长度优先重载) 
struct node {
	int l, r, len;
	node(int a, int b, int c) {
		l = a;
		r = b;
		len = c;
	}
	//优先队列从大到小排
	//返回值为真意味着在队列排序中 this<a 成立
	bool operator < (const node& a) const {
		if (a.len == len) return l > a.l;
		//如果长度相同而a的l较小(即靠近左边),则a>this(a更优先)
		return len < a.len;
		//否则如果a更长,则a>this(a更优先)
	}
};

int ans[N];
int main(){
	FAST;
	rush(){
		int n,count = 1;
		cin>>n;
		
		priority_queue<node>tro;
		tro.push(node(1,n,n));
		
		while(count <= n){
			node x = tro.top();
			tro.pop();
			int place = (x.l + x.r) / 2;
			ans[place] = count;
			tro.push(node(x.l,place - 1,place - x.l));
			tro.push(node(place + 1,x.r,x.r - place));
			count++;
		}
		
		for(int i = 1;i <= n;i++)cout<<ans[i]<<" ";
		cout<<endl;
	}
	return 0;
} 

TO BE CONTINUED.....


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值