D. Productive Meeting(贪心)

Codeforces Round #744 (Div. 3)

题意

给一个长度为n的数组a,求最多有几个组合 ( i d x i , i d x j ) , 满 足 i ≠ j 且 1 ≤ i , j ≤ n (idx_i , idx_j),满足 i \neq j且1\leq i, j \leq n (idxi,idxj),i=j1i,jn。每个下标最多被使用 a i a_i ai次。

思路

将所有非零的数值存入优先队列中,每次选则数量最大与次大的两个值进行交谈,每次交谈过后要及时进行出队入队操作。
(我开始是这样想的:每次对最小的和最大的进行操作,可是。。我实现不了)

代码

思路简单,代码也简单。简简单单40行。

#include<cstdio>
#include<deque>
#include<queue>
#include<set>
#include<cstdlib>
#include<string.h>
#include<string>
#include<iostream>
#include<cmath>
#include<unordered_map>
#include<map>
#include<algorithm>
#define endl "\n"
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
#define ft first
#define sd second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ll long long int
#define ull unsigned long long int
#define mt(a,b) memset(a, b, sizeof a)
//#define int long long
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
using namespace std;
const int N = 2e5 + 7, M = 1e6 + 10;
priority_queue<pii>q;
pii s[N];
pii ans[M];
int main()
{
	IOS;
	int t; cin >> t;
	while (t--)
	{
		int n; cin >> n;
		while (!q.empty()) q.pop();//注意及时清空队列
		for (int i = 1; i <= n; i++)
		{
			cin >> s[i].ft, s[i].sd = i;
			if (s[i].ft) q.push(s[i]);//可以直接进行make_pair,而不必要开s数组
		}

		int k = 0;
		
		while (q.size() >= 2)
		{
			pii a = q.top(); q.pop();//最大
			pii b = q.top(); q.pop();//次大
			ans[++k].ft = a.sd;
			ans[k].sd = b.sd;
			if (--a.ft)q.push(make_pair(a.ft, a.sd));//非零
			if (--b.ft)q.push(make_pair(b.ft, b.sd));//非零
		}
		

		cout << k << endl;
		for (int i = 1; i <= k; i++)
		{
			if (ans[i].ft > ans[i].sd) swap(ans[i].ft, ans[i].sd);
			cout << ans[i].ft << " " << ans[i].sd << endl;
		}
	}


	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Sloppy People vs. Neat People Sloppy people and neat people are two different types of individuals with distinct personality traits. Sloppy people tend to be more relaxed and carefree, while neat people are often more organized and detail-oriented. Both types of people have their own unique strengths and weaknesses. Sloppy people are often creative and spontaneous. They tend to prioritize fun and enjoyment over order and structure. They are comfortable with chaos and can adapt easily to changing situations. They are also great at improvising and finding new solutions to problems. However, their lack of organization can sometimes lead to disarray and confusion. They may struggle to meet deadlines or remember important tasks. On the other hand, neat people prefer structure and order. They like to have a plan and stick to it. They are detail-oriented and take pride in their work. They are often reliable and responsible. They are also great at managing their time and resources efficiently. However, their attention to detail can sometimes lead to perfectionism and rigidity. They may struggle to be flexible or adapt when things don't go according to plan. In terms of living spaces, sloppy people tend to have cluttered and messy environments. They may have clothes strewn about or dishes piled up in the sink. Neat people, on the other hand, prefer clean and organized spaces. They may have a place for everything and everything in its place. They like to keep their living spaces tidy and free of clutter. In conclusion, both sloppy people and neat people have their own unique strengths and weaknesses. Sloppy people tend to be creative and adaptable, while neat people are organized and responsible. It's important to recognize and appreciate these differences, as they can add diversity and balance to our lives. Whether you're a sloppy person or a neat person, it's important to embrace your strengths and work on improving your weaknesses. By doing so, you can create a more fulfilling and productive life.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

to cling

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

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

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

打赏作者

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

抵扣说明:

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

余额充值