当前位置:网站首页>2022/7/17 每日一题(构造+数学+贪心+指针)
2022/7/17 每日一题(构造+数学+贪心+指针)
2022-07-19 05:58:00 【钟钟终】
D. Permutation Restoration
题意:数组a(由1~n组成)通过计算格式i/a[i]
转化为数组b,现在给定数组b,要求还原出数组a,输出其中一种可能即可。
思路:
1.i/a[i]=b[i]
可通过数学思维转化为 a[i]*b[i]=<i<a[i]*(b[i]+1)
进一步转化为 i/(b[i]+1)<a[i]<=i/b[i]
,最终为 i/(b[i]+1)+1=<a[i]<=i/b[i]
2.由此确定了每一个a[i]的取值范围,下一步确定1~n中每一个值的归属。
3.对每个a[i]的范围进行排序,根据左范围l进行从小到大排序;若相等,则再根据右范围升序排列。
4.创建一个pair类型的优先队列,第一关键字为右范围,第二关键字为b[i]中编号。
(pair默认是先对第一个关键字从小到大排序,如果第一关键字相同,在对第二关键字从小到大排序,都是升序)
5.非常关键的特判,如果b[i]为0,那么a[i]范围应该是[i+1,n]
.要特别得区别于数学公式推出来的范围。
代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define pii pair<int,int>
using namespace std;
const int N=1e6+6;
struct node
{
int l,r,id;
}e[N];
int n,a[N],b[N];
bool cmp(node n1,node n2)
{
if(n1.l==n2.l)
return n1.r<n2.r;
return n1.l<n2.l;
}
signed main()
{
int t;cin>>t;
while(t--)
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>b[i];
e[i].id=i;
e[i].l=i/(b[i]+1)+1;
if(b[i]==0)
e[i].r=n;
else
e[i].r=i/b[i];
}
sort(e+1,e+n+1,cmp);
priority_queue<pii,vector<pii>,greater<pii> >q;
int k=1;
for(int i=1;i<=n;i++)
{
while(k<=n&&e[k].l<=i)
{
q.push({
e[k].r,e[k].id});
k++;
}
int g=q.top().second;
q.pop();
a[g]=i;
}
for(int i=1;i<=n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
return 0;
}
边栏推荐
- 微信小程序应用开发赛作品综合开发记录——晋鹿文旅(云开发——概览)
- FPGA buzzer plays the music "sea of flowers"
- 20day
- 预计iPhone14获中国消费者热捧,苹果和富士康给中国员工发钱,最高近万元
- Verilog grammar basics HDL bits training 02
- Location information of ORC index
- MySQL InnoDB engine (3)
- 5day
- Dynamically adjust impala log level
- FPGA implementation of UART serial asynchronous communication - one byte data reception
猜你喜欢
解决报错:Do not access Object.prototype method ‘hasOwnProperty‘ from target object no
flex布局简介
JMeter project practice: BeanShell processes the obtained results 64base processing
随机生成10(范围1~100)个整数,保存到数组,并倒序打印该数组。以及求平均值、最大值和最大值的下标、并查找里面含有某一数字的个数。
FPGA buzzer plays the music "sea of flowers"
ikbc键盘win键失效的解决方法
C4 学习资料(未完待续)
亲测 运营版 在线考试 online_testck163 2.7.18功能强大的在线考试模块
6day
【电子器件笔记2】电阻的使用技巧
随机推荐
New features and community progress of impala 3.4
20day
Kubernetes technology and Architecture (II)
揭开空块攻击的谎言与真相
3day
How to print thread stack for impala in CDH cluster
Understanding of blocking assignment and non blocking assignment in FPGA
SOFA Weekly | 开源人—牛学蔚、本周 QA、本周 Contributor
FPGA uses MATLAB to generate MIF files of four waveforms
18day
什么是「中华田园敏捷开发」,人才
13day
MySQL InnoDB engine (3)
14day
3. Editors (vim)
如何对CDH集群中的Impala打印线程堆栈
8day
15day
预计iPhone14获中国消费者热捧,苹果和富士康给中国员工发钱,最高近万元
19day