当前位置:网站首页>AcWing 133. 蚯蚓
AcWing 133. 蚯蚓
2022-07-16 07:25:00 【hys__handsome】
AcWing 133. 蚯蚓
做起来很难的一道队列题,容易踩到优先队列的坑里(超时)。需要对其进行优化。
思路
这里数构分为 3 3 3 个队列, q 1 q1 q1 初始队列、 q 2 q2 q2 左半段的队列、 q 3 q3 q3 右半段的队列。
将 q 1 q1 q1 大到小排序。
那么蚯蚓分开后的两段肯定是一大一小或一样大,不管怎么样蚯蚓分开后并插入左右队列 q 2 q2 q2、 q 3 q3 q3 一定是单调递减的(因为初始队列是 q 1 q1 q1 单调递减)。所以接下来,我们要找出最大的蚯蚓来分就容易了,直接枚举每个队列的头即可。
因为分开蚯蚓后其他蚯蚓长度会 + q +q +q ,所以我们可以已相对大小 d e l t a delta delta 来看待。(具体看代码)
至此时间复杂度 O ( n + m ) O(n+m) O(n+m) ,就比优先队列做法 O ( m l o g m ) O(mlogm) O(mlogm) 高效了。
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
const int N = 100010, M = 7000010;
int q1[N],q2[M],q3[M];
int h1,h2,h3,t1,t2,t3,delta;
bool cmp(const int &a,const int &b) {
return a > b;
}
int get_max(){
int res = -0x3f3f3f3f;
if(h1 <= t1) res = max(res,q1[h1]);
if(h2 <= t2) res = max(res,q2[h2]);
if(h3 <= t3) res = max(res,q3[h3]);
if(h1 <= t1 && res == q1[h1]) h1++;
else if(h2 <= t2 && res == q2[h2]) h2++;
else if(h3 <= t3 && res == q3[h3]) h3++;
return res;
}
signed main(){
cin.tie(0)->sync_with_stdio(false);
cout.tie(0);
int n,m,q,u,v,t;
cin>>n>>m>>q>>u>>v>>t;
double p = 1.0*u/v;
for(int i = 0; i < n; i++) cin>>q1[i];
sort(q1,q1 + n, cmp);
t1 = n-1; t2 = t3 = -1; delta = 0;
for(int i = 1; i <= m; i++){
int x = get_max();
x += delta;
if(i % t == 0) cout<<x<<' ';
int l = (int)(p*x), r = x-l;
delta += q;
q2[++t2] = l - delta;
q3[++t3] = r - delta;
}
cout<<'\n';
for(int i = 1; i <= n+m; i++){
int x = get_max();
if(i % t == 0) cout<<x+delta<<' ';
}
return 0;
}
边栏推荐
- MySQL field type selection
- 【批处理DOS-CMD命令-汇总和小结】-符号链接、硬链接、软链接、目录联结(mklink)
- How long can zynq PL interrupt pulses be captured by CPU
- cas(Compare-and-Swap)
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) - B, C, D
- page-break-before\page-break-inside\page-break-after用法
- AAV2/9的2/9如何理解 AAV2/2,AAV2/5,AAV2/8和AAV2/9有什么区别
- Value problem in watch
- cron 表达式周一到周五执行以及只有周六周天执行
- 常用测试用例设计方法之判定表法详解
猜你喜欢

PostgreSQL is now installed

Codeforces Round #583 (Div. 1 + Div. 2) - A, D, E

PostgreSQL 现在安装

金字塔思维学习

Surmonter les défis de la transformation numérique en élaborant une vision

无线通信中LoRa技术特点

Splash integrates with Acronis to provide scalable remote support

Domestic postman tool, easy to use!

Small target detection 2_ OHEM

Airiot low code development platform, 10 minutes to build the Internet of things system
随机推荐
redis 配置,集群安装与扩容
Flutter精品学习路线(知识分类+学习资料)
The bottom logic of learning essence
(manual) [sqli labs40, 41] stack injection, blind injection
Lscale theme emlog background management panel theme source code
【LeetCode】10. Maximum subarray · maximum subarray sum
Flutter boutique learning route (knowledge classification + learning materials)
Detailed explanation of the decision table method of common test case design methods
Func () {} () is a strange way of writing. I don't know what category it belongs to
The difference and use between get request and post request
Code to celebrate the Dragon Boat Festival -- Zong your heart
面试常问的shell基础,你会了吗?
zabbix详细介绍
Cron expressions are executed from Monday to Friday and only on Saturday and Sunday
PostgreSQL 现在安装
Detailed explanation of Flink resource management
Kingbasees v8r6 ksql turn off auto submit
Kingbasees v8r6 cluster backup recovery case - the backup database performs physical backup as a repo host
Overcome the challenges of digital transformation by developing a vision
Airiot low code development platform, 10 minutes to build the Internet of things system