Educational Codeforces Round 131 (Rated for Div. 2) D题(区间贪心)
题意:给定一个数组b,定义 ,还原这个a数组。
分析:
由于是下取整,所以对于每个 我们都能得到的范围
对于 ,
对于
所以问题可以转化为有n个活动,每个活动都有起始时间和结束时间,每个活动只需要一个单位时间完成,问如何安排这些活动才能在n个单位时间内完成所有任务,输出每个任务在那个时间完成
贪心策略:
枚举时间,对于时间i,优先安排开始时间 i的所有活动中结束时间最小的一个活动
ac代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <map>
#include <vector>
#include <stack>
#include <set>
#include <sstream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define x first
#define y second
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl '\n'
#define pb push_back
#define all(x) x.begin(),x.end()
#define all1(x) x.begin()+1,x.end()
#define pi 3.14159265358979323846
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const int N = 1000010,M = 200010,INF = 0x3f3f3f3f,mod = 998244353;
const double INFF = 0x7f7f7f7f7f7f7f7f;
int n,m,k,t;
int main()
{
ios;
cin >> t;
while(t --)
{
cin >> n;
vector<int> b(n + 1),ans(n + 1);
vector<vector<PII>> a(n + 1) ;
for(int i = 1;i <= n;i ++)
{
cin >> b[i];
int l = i / (b[i] + 1) + 1;
int r = b[i] ? i / b[i] : n;
a[l].pb({r,i});
}
priority_queue<PII,vector<PII>,greater<PII>> heap;
for(int i = 1;i <= n;i ++)
{
for(PII x : a[i]) heap.push(x);
auto x = heap.top();
heap.pop();
ans[x.y] = i;
}
for(int i = 1;i <= n;i ++) cout << ans[i] << ' ';
cout << endl;
}
return 0;
}
本文作者:notyour_young
本文链接:https://www.cnblogs.com/notyour-young/p/16460955.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步