Codeforces Round #521 (Div. 3) ABCD

A. Frog Jumping

题解:水题。

代码

#include<bits/stdc++.h>
typedef long long LL;
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
#endif
	int t;
	cin>>t;
	while(t--) {
		LL a,b,k,ans = 0,x=0;
		cin>>a>>b>>k;
		if(k & 1) {
			ans = x + (k/2+1) *a - k/2*b;
		}else{
			ans = x + (k/2)*a - k / 2 * b;
		}
		cout<<ans<<endl;
	}

    return 0;
}

B. Disturbed People

题解:枚举一遍有多少个 101 101 101即可。

代码

#include<bits/stdc++.h>

using namespace std;
int a[111];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
#endif
	int n;
	cin>>n;
	for(int i = 0; i < n; ++i) {
		cin>>a[i];
	}
	int cnt = 0;
	for(int i = 1; i < n; ) {
		if(a[i - 1] == 1 && a[i + 1] == 1 && a[i] == 0) {
			cnt++;
			i += 3;
		}else{
			i++;
		}
	}
	cout<<cnt<<endl;
    return 0;
}

C. Good Array

题解:现根据值的大小排个序,最后枚举一下有多少个满足的即可。

代码

#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
//int a[100100];
struct node{
	int v,id;
	bool operator < (const node & a) const{
		return v < a.v;
	}
}a[200010];


vector<int> v;
//set<int> s;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
#endif
	int n;
	LL sum = 0;
	cin>>n;
	for(int i = 0; i < n; ++i) {
		scanf("%d",&a[i].v);
		sum += a[i].v;
		a[i].id = i;
	}
	sort(a,a+n);
//	cout<<*s.end()<<endl;
	for(int i = 0; i < n; ++i) {
	//	s.erase(a[i]);
		//int t = *s.end();
		//cout<<a[i]<<' '<<t<<endl;
		if(i == n - 1) {
			if(sum - a[i].v - a[n - 2].v == a[n - 2].v) {
				v.push_back(a[i].id);
				break;
			}
			break;
		}
		if(sum - a[i].v - a[n - 1].v == a[n - 1].v) {
			v.push_back(a[i].id);
		}
	//	s.insert(a[i]);
	}
	cout<<v.size()<<endl;
	for(int i = 0; i < v.size(); ++i) {
		printf("%d%c",v[i]+1,i==v.size()-1?'\n':' ');
	}
    return 0;
}

D. Cutting Out

题解:优先队列维护一下当前出现次数最多的那个数即可。然后每次加入答案队列后,减去相应的出现次数。

代码

#include<bits/stdc++.h>
#define P pair<int,int> 
using namespace std;
map<int,int> m,cnt;
int a[200100];
/*bool cmp(const pair<int,int> &p1,const pair<int,int> &p2)//要用常数,不然编译错误
{
	return p1.first>p2.first;
}*/
int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
#endif
	int n,k;
	cin>>n>>k;
	for(int i = 0; i < n; ++i) {
		scanf("%d",a + i);
		m[a[i]]++;
	}
	vector<int> v;
	priority_queue<P,vector<P> > pq;
	for(auto t : m) {
		pq.push({t.second,t.first});
	}
	while(v.size() < k) {
		P t = pq.top();
		pq.pop();
		v.push_back(t.second);
		cnt[t.second]++;
		t.first = m[t.second] / (cnt[t.second] + 1);
		pq.push(t);
	}
	for(int i = 0; i < v.size(); ++i) {
		printf("%d%c",v[i],i==v.size()-1?'\n':' ');
	}
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值