1283C - Friends and Gifts(思维)

传送门

思路:开两个数组,分别保存没送出礼物的人和没收到礼物的人。然后遍历两个数组,发现两个数组元素有相等的地方则与下一个交换,注意边界当最后一个相同则和开始的元素交换。

Code:

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#include<memory.h>
#include<cmath>
#include<queue>
#include<sstream>
#define pii pair<int,int>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
const int Max = 6e6 + 5;
int lst[Max];
int b1[Max],b2[Max];
int ls1[Max], ls2[Max];
int main()
{
   
	FAST;
	int n;cin >> n;
	map<int, int> ma;
	vector<int> vec;
	for (int i = 1;i <= n;i++)
	{
   
		cin >> lst[i];
		if (lst[i] != 0)b1[i]++;
		b2[lst[i]]++;
	}
	int g = 0, f = 0;
	for (int i = 1;i <= n;i++)
	{
   
		if (b1[i] == 0)ls1[++g] = i;
		if (b2[i] == 0)ls2[++f] = i;
	}
	for (int i = 1;i <= g;i++)
	{
   
		if (ls1[i] == ls2[i])
		{
   
			if (i == g)swap(ls2[g], ls2[1]);
			else swap(ls2[i], ls2[(i + 1)]);
		}
	}g = 0;
	for (int i = 1;i <= n;i++)
	{
   
		if (lst[i])cout << lst[i] << " ";
		else cout << ls2[++g] << " ";
	}
}
全部评论

相关推荐

06-26 10:08
门头沟学院 C++
北京Golang实习,一个月4700,吃住都不报,公司位置在海淀。请问友友怎么看呢?如果要租房的话有什么建议吗
码农索隆:租房肯定是合租了,剩下的钱,差不多够正常吃饭了,看看能不能学到东西吧
点赞 评论 收藏
分享
迟缓的斜杠青年巴比Q...:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务