CodeForces-1701D Permutation Restoration
Permutation Restoration#
贪心
很类似于调度问题,考虑好贪心优先级即可
显然对于每个位置 我们可以根据现有的 和 来推算出可行的 的范围,是一段连续的区间,我们假设为
题目说了答案必有解,因此从贪心的考虑来讲,只用考虑如何将 分配给每一个
从 开始,如果有能够填入的地方,也就是可行区间的 的,我们贪心地按照其 从小到大排序,因为 较小的区间显然要分配的更加早一些,如果晚了,那么说明会有一个 更大的抢走了原本能分配的,例如: 和 ,显然我们要先将 分配给
因此我们可以使用一个优先队列,队头为 较小的,每次想要分配 时,都将 的区间加入优先队列,然后在这些可分配的区间中将 分配给 较小的区间
一开始以为是什么二分图匹配,然后网络流和二分图显然复杂度都不对,后来开始觉得是贪心问题的时候已经乱掉了,赛后才过
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
#include <array>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
const ll maxn = 5e5 + 10;
const ll inf = 1e17 + 10;
int ans[maxn], vis[maxn];
struct node
{
int l, r, id;
node(){}
node(int _l, int _r, int _id){l = _l; r = _r; id = _id;}
bool operator < (const node& a)const
{
return a.r < r;
}
}num[maxn];
bool cmp(const node& a, const node &b)
{
return a.l < b.l;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n;
scanf("%d", &n);
for(int i=1; i<=n; i++)
{
int x;
scanf("%d", &x);
if(x == 0)
num[i] = node(i + 1, n, i);
else
num[i] = node(i / (x + 1) + 1, i / x, i);
}
sort(num + 1, num + n + 1, cmp);
priority_queue<node>q;
int tp = 1;
for(int i=1; i<=n; i++)
{
while(tp <= n && num[tp].l <= i) q.push(num[tp++]);
node now = q.top();
q.pop();
ans[now.id] = i;
}
for(int i=1; i<=n; i++)
{
if(i != 1) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】