当前位置:网站首页>D. Permutation restoration (greedy / double pointer /set)
D. Permutation restoration (greedy / double pointer /set)
2022-07-19 03:10:00 【Luo GKV】
The question
Given length is n Permutation a[i], Make b[i]=i/a[i]( Rounding down ), Now given b[i], Seek to restore the original a[i].
If there are multiple solutions , Output any set of , The problem ensures that there is at least one solution to the data .
Ideas
because b [ i ] = i / a [ i ] b[i]=i/a[i] b[i]=i/a[i]( Rounding down ), therefore
a [ i ] ∗ b [ i ] < = i < a [ i ] ∗ ( b [ i ] + 1 ) a[i]*b[i]<= i < a[i]*(b[i]+1) a[i]∗b[i]<=i<a[i]∗(b[i]+1), therefore
i / ( b [ i ] + 1 ) < a [ i ] < = i / b [ i ] i/(b[i]+1)< a[i]<=i/b[i] i/(b[i]+1)<a[i]<=i/b[i]
Again because a[i] It's arrangement , We can enumerate every number from small to large , At the same time maintain Include the current number The range of ;
Every time you select an interval , choice The earliest expiration date Of , namely Right interval minimum the .
Combined with code understanding
Code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 500010;
int n, m;
int a[maxn], b[maxn];
void solve() {
scanf("%d", &n);
vector<pair<int, int> > ve;
for (int i = 1; i <= n; ++i) {
scanf("%d", &b[i]);
ve.push_back(make_pair(i/(b[i]+1)+1, i));
}
// Left subscript of interval , Sort from small to large
sort(ve.begin(), ve.end());
// for (auto v: ve) {
// printf("{%d %d}\n", v.first, v.second);
// }
set<pair<int, int> > st;
for (int i = 1, j = 0; i <= n; ++i) {
// Will contain number i The interval of is crammed into the set
while (j < n && ve[j].first <= i) {
int pos = ve[j].second;
if(b[pos])
st.insert(make_pair(pos/b[pos], pos));
else
st.insert(make_pair(n, pos));
++j;
}
// debug
if (st.empty()) {
printf("%d corrupt\n", i);
break;
}
// Select the one with the smallest right boundary from the set
a[st.begin()->second] = i;
st.erase(st.begin());
}
for (int i = 1; i <= n; ++i) {
printf("%d ", a[i]);
}
printf("\n");
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
solve();
}
}
边栏推荐
- 【单片机仿真】(五)寻址方式 — 立即寻址与寄存器间接寻址
- [single chip microcomputer simulation] (10) instruction system - multiplication instruction and division of arithmetic operation instruction
- yolov5 ncnn 推理
- 重写equals为什么要重写hashcode
- 【单片机仿真】(十六)控制转移类指令 — 无条件转移指令、条件转移指令
- Examine your investment path
- SysTick定时器的基础学习以及手撕代码
- 【回归预测】基于粒子滤波实现锂离子电池寿命预测附matlab代码
- 快照:数据快照(数据兜底方式)
- After 4 years of developing two-sided meituan, we finally lost: the interview question of volatile keyword function and principle
猜你喜欢
多项式插值拟合(三)
Detailed explanation of case when usage of SQL
1. Introduction, analysis and implementation of asynctool framework
[MCU simulation] (XX) org - set start address
BiSeNetV2-面部分割
一个优酷VIP会员帐号可以几个人用的设备同时登录如何共享多人使用优酷会员账号?
工具及方法 - Excel插件XLTools
2. Actual use of asynctool framework
[PHP] tp6 multi table connection query
RESNET learning notes
随机推荐
[MCU simulation] (VI) addressing mode - index addressing and relative addressing
快照:数据快照(数据兜底方式)
ncnn DataReader&Extractor&blob
【单片机仿真】(八)指令系统 — 数据传送指令
关于XML文件(六)-与JSON的区别
The place where the dream begins ---- first knowing C language
ncnn Allocator内存分配器
[MCU simulation] (XXI) dB (define byte) - define byte
【单片机仿真】(二十一)DB(Define Byte)— 定义字节
BiSeNetV2-面部分割
RTX3090安装pytorch3D
【单片机仿真】(六)寻址方式 — 变址寻址与相对寻址
通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
Pytorch best practices and code templates
yolov6 学习初篇
人脸检测几种方法
要开源节流
无法访问此网站无法找到DNS地址DNS_PROBE_STARTED怎么办?
樂視還有400多比特員工?過著沒有老板的神仙日子 官方出來回應了...
ncnn Mat矩阵类