当前位置:网站首页>D. Rating compression (thinking + double pointer)
D. Rating compression (thinking + double pointer)
2022-07-26 01:47:00 【to cling】
Problem
Give a length of n Array of a, among 1 ≤ a [ i ] ≤ n 1 \leq a[i] \leq n 1≤a[i]≤n.
b [ j ] = m i n j ≤ i ≤ j + k − 1 a [ i ] b[j] = min_{j \leq i \leq j + k - 1}~~~a[i] b[j]=minj≤i≤j+k−1 a[i]
Judge k = 1 ~ n when , Got b Whether the array is an arrangement .
Solution
- 1 Is the smallest number , And can only be located in a The beginning or end of an array , And only once .
otherwise , a n s k = n = 1 , a n s 1 ≤ k ≤ n − 1 = 0 ans_{k = n} = 1, ans_{1 \leq k \leq n - 1} = 0 ansk=n=1,ans1≤k≤n−1=0. - When a When the array itself is an arrangement , a n s 1 = 1 ans_1 = 1 ans1=1
- Maintain a range [ l , r ] [l, r] [l,r], loop j = 1, 2, 3,···.
When a l or person a r a_l perhaps a_r al or person ar be equal to i When , And m i n ( a [ l , r ] ) = i + 1 min(a[l,r]) = i + 1 min(a[l,r])=i+1,
be a n s n − i = 1 ans_{n - i} = 1 ansn−i=1, Otherwise, the program will be terminated .
Finally, when 2 ≤ k < n − i 2 \leq k < n - i 2≤k<n−i when ,ans = 0; When n − i ≤ k ≤ n n - i \leq k \leq n n−i≤k≤n when , ans = 1.
This topic is not so much thinking , It's more a conclusion question . In fact, one of the characteristics of the arrangement itself . This kind of algorithm needs more problem summaries .
Code
Time complexity O ( n ) O(n) O(n)
const int N = 3e5 + 5, M = 1e6 + 7;
int a[N], ans[N], cnt[N];
int main()
{
IOS;
int T; cin >> T;
while (T--)
{
int n; cin >> n;
for (int i = 1; i <= n; i++)
cnt[i] = ans[i] = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
cnt[a[i]]++;
}
//k = 1
int f = 1;
for (int i = 1; i <= n; i++)
if (cnt[i] == 0) f = 0;
if (f) ans[1] = 1;
//k = n
if (cnt[1]) ans[n] = 1;
//1 < k < n
int l = 1, r = n;
int j = 1;
while (l < r)
{
//--cnt[j] Ensure that only one of the left and right endpoints is equal to j
if (--cnt[j] == 0 && (a[l] == j || a[r] == j) && cnt[j + 1])
{
ans[n - j] = 1;
l += a[l] == j;
r -= a[r] == j;
j++;
}
else break;
}
for (int i = 1; i <= n; i++)
cout << ans[i];
cout << "\n";
}
return 0;
}
边栏推荐
- Travel (split points and layers)
- How does Flink SQL configure to print the insert parameter log
- 我mysql to mysql数据表同步,代码上只有写在第一个顺序上的生效 其余的不生效,这个可能是
- The work of robot engineering and the puzzle of postgraduate entrance examination "volume" supplement
- 【Verilog数字系统设计(夏宇闻)4-----Verilog语法的基本概念2】
- Niuke - bm39 serialized binary tree [hard]
- I want to know how much the Commission is for opening an account. Is it safe to open an account on your mobile phone
- Implementation of recommendation system collaborative filtering in spark
- Image batch processing Gaussian filter noise reduction + peak signal-to-noise ratio calculation
- Prime Ring Problem
猜你喜欢

How to display numbers / English time in Excel

大咖观点+500强案例,软件团队应该这样提升研发效能

怎么使用宝塔面板把node全栈项目部署到服务器上

Handler message mechanism - FWK layer

Understand Linglong platform unified access service from simple to deep Monet

PtGui pro12 vertical line correction

MDK compilation process and arm compilation tool chain

After reading this article, you should thoroughly understand how to do interface testing

Recommend a super good UI automation tool: uiautomator2!

Silicon Valley classroom - official account cloud on demand Silicon Valley classroom microservice project practical notes
随机推荐
"Weilai Cup" 2022 Niuke summer multi school training camp 2 h.[take the elevator] maintenance section
Pt onnx ncnn conversion problem record (followed by yolov5 training)
软件加群验证
Guys, the flinksql datahub source table has a field timestamp 16 bits, which is written to ora
Three modes of CPU
【深入浅出玩转FPGA学习11----Testbench书写技巧2】
“蔚来杯“2022牛客暑期多校训练营2 D.[Link with Game Glitch] 二分答案+SPFA判环
How does Flink SQL configure to print the insert parameter log
给RestTemplate添加拦截器记录请求响应,还需解决流只读一次的问题
Big view +500 cases, software teams should improve R & D efficiency in this way
After reading this article, you should thoroughly understand how to do interface testing
Niuke - bm39 serialized binary tree [hard]
Zombie's treasure test (enumeration)
Nodejs builds cloud native microservice applications based on dapr, a quick start guide from 0 to 1
Oracle is nested at multiple levels, and the alias problem of the table cannot be found
3059. Sculpture (jzoj)
Is it safe to buy funds in stock accounts? Professional answers
Easyrecovery15 data recovery software with high recovery rate and high download volume
Zombie‘s Treasure Chest(枚举)
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2 d.[link with game glitch] two point answer +spfa ring