【HihoCoder - 1269】 优化延迟 (优先队列+二分优化)

题干:

小Ho编写了一个处理数据包的程序。程序的输入是一个包含N个数据包的序列。每个数据包根据其重要程度不同,具有不同的"延迟惩罚值"。序列中的第i个数据包的"延迟惩罚值"是Pi。如果N个数据包按照<Pi1, Pi2, ... PiN>的顺序被处理,那么总延迟惩罚

SP=1*Pi1+2*Pi2+3*Pi3+...+N*PiN(其中i1, i2, ... iN是1, 2, 3, ... N的一个排列)。

小Ho的程序会依次处理每一个数据包,这时N个数据包的总延迟惩罚值SP为

1*P1+2*P2+3*P3+...+i*Pi+...+N*PN。  

小Hi希望可以降低总延迟惩罚值。他的做法是在小Ho的程序中增加一个大小为K的缓冲区。N个数据包在被处理前会依次进入缓冲区。当缓冲区满的时候会将当前缓冲区内"延迟惩罚值"最大的数据包移出缓冲区并进行处理。直到没有新的数据包进入缓冲区时,缓冲区内剩余的数据包会按照"延迟惩罚值"从大到小的顺序被依次移出并进行处理。

例如,当数据包的"延迟惩罚值"依次是<5, 3, 1, 2, 4>,缓冲区大小K=2时,数据包被处理的顺序是:<5, 3, 2, 4, 1>。这时SP=1*5+2*3+3*2+4*4+5*1=38。

现在给定输入的数据包序列,以及一个总延迟惩罚阈值Q。小Hi想知道如果要SP<=Q,缓冲区的大小最小是多少?

输入

Line 1: N Q

Line 2: P1 P2 ... PN

对于50%的数据: 1 <= N <= 1000

对于100%的数据: 1 <= N <= 100000, 0 <= Pi <= 1000, 1 <= Q <= 1013

输出

输出最小的正整数K值能满足SP<=Q。如果没有符合条件的K,输出-1。

样例输入

5 38
5 3 1 2 4

样例输出

2

解题报告:

对于缓冲区的描述我们一般就直接用优先队列了 复杂度为O(N*logN)

对于这个题如果我们考虑直接去暴力枚举缓冲区K的大小,然后在优先队列去入队出队算出 SP值得话, 复杂度为O(N^2logN)N为10^5 复杂度还是很高;我们可以观察考虑到我们枚举K的大小时K为单调的,而且我们发现随着K变大 SP的值在单调递减,所以我们可以想到二分K的大小,复杂度降为O(N*logN*logN)。其实这题不用二分也貌似能过。

AC代码:

#include<bits/stdc++.h>using namespace std;
const int MAX = 100000 + 5;
int a[MAX];	
long long n;
long long q;
//缓冲区大小为k 
bool ok(int k) {priority_queue<int > pq;int cnt = 1;long long ans = 0;for(int i = 1; i<=n; i++) {if(pq.size() <k) pq.push(a[i]);else {ans += cnt*pq.top();cnt++;pq.pop();i--;}}while(!pq.empty() ) {ans +=cnt*pq.top();pq.pop();cnt++;}
//	printf("ans = %lld\n",ans);if(ans<=q) return 1;else return 0;
}
int main()
{int flag = 0;cin>>n>>q;for(int i = 1; i<=n; i++) {scanf("%d",&a[i]);}int l = 1,r = n,mid; mid = (l+r)/2;while(l<r) {mid = (l+r)/2;if(ok(mid) ) r = mid,flag = 1;else l = mid + 1; }if(flag == 1 ) printf("%d\n",l) ;else printf("-1\n");return 0 ;} 

 

总结:两个坑。第一l初始化成1,不能初始化成0!不然就RE了,大概因为输入n为1的时候  刚开始进去的mid为0了?然后ok函数的k为0,,,肯定有RE啊。。第二看清数据范围 Q的范围是1e13,所以需要开longlong。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442022.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

带有风的诗词_带有风的诗句

带有风的诗句1、古道西风瘦马&#xff0c;夕阳西下&#xff0c;断肠人在天涯。——马致远《天净沙秋思》2、欲乘风归去&#xff0c;又恐琼楼玉宇。——苏轼《水调歌头明月几时有》3、道通天地有形外&#xff0c;思入风云变态中。——程颢《秋日》4、津亭杨柳碧毵毵&#xff0c;…

【ZOJ - 3210】A Stack or A Queue? (模拟)

题干&#xff1a; Do you know stack and queue? Theyre both important data structures. A stack is a "first in last out" (FILO) data structure and a queue is a "first in first out" (FIFO) one. Here comes the problem: given the order of …

ehchache验证缓存过期的api_Ehcache缓存配置

Cache的配置很灵活&#xff0c;官方提供的Cache配置方式有好几种。你可以通过声明配置、在xml中配置、在程序里配置或者调用构造方法时传入不同的参数。你可以将Cache的配置从代码中剥离出来&#xff0c;也可以在使用运行时配置&#xff0c;所谓的运行时配置无非也就是在代码中…

*【POJ - 3061】 Subsequence (尺取或二分)

题干&#xff1a; A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, and a positive integer S (S < 100 000 000) are given. Write a program to find the minimal length of the subsequence of consecutive eleme…

alert 回调_JavaScript中到底什么时候回调函数Callback

什么是回调函数Callback简单的理解&#xff1a;回调函数是在另一个函数执行完毕后执行的函数 - 因此名称为call back。复杂的理解&#xff1a;在JavaScript中&#xff0c;函数是对象。因此&#xff0c;函数可以将函数作为参数&#xff0c;并且可以由其他函数返回。执行此操作的…

【CF#148B】Escape(模拟)

题干&#xff1a; The princess is going to escape the dragons cave, and she needs to plan it carefully. The princess runs at vp miles per hour, and the dragon flies at vd miles per hour. The dragon will discover the escape after t hours and will chase the…

mysql sql语句分页查询_如何用sql语句 实现分页查询?

我制作了一个数据库其中一张表createtablenews(news_idintprimarykeyidentity(1,1),news_titlevarchar(50)notnull,news_authorvarchar(20),news_summaryvarchar(50),news_contenttext...我制作了一个数据库其中一张表create table news(news_id int primary key identity(1,1)…

*【HDU - 1042 】 N! (大数乘法)

题干&#xff1a; Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one line, process to the end of file. Output For each N, output N! in one line. Sample Input 1 2 3 Sample Output 1 2 6 解题报告&#xff1a; 大数运…

【bzoj 1754】【POJ - 2389 】Bull Math (高精度运算)

题干&#xff1a; Bulls are so much better at math than the cows. They can multiply huge integers together and get perfectly precise answers ... or so they say. Farmer John wonders if their answers are correct. Help him check the bulls answers. Read in two…

mysql居左查询abcd_数据库--查询语句

查询语句mysql中要学习的知识&#xff1a;多表关系&#xff0c;查询语句&#xff0c;索引添加数据补充将一个查询结果插入到另一张表中create table student(name char(10), gender int);insert into student values(nalituo, 1);insert into student values(sasigi, 0);create…

druid mysql配置详解_druid 参数配置详解

spring.datasource.typecom.alibaba.druid.pool.DruidDataSource#驱动配置信息spring.datasource.driver-class-namecom.mysql.jdbc.Driver#基本连接信息spring.datasource.username rootspring.datasource.password rootspring.datasource.urljdbc:mysql://192.168.153.23:3…

【POJ - 2376】Cleaning Shifts (贪心)

题干&#xff1a; Farmer John is assigning some of his N (1 < N < 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 < T < 1,000,000), …

mysql sql 片段_MySQL代码片段

1.[代码][SQL]代码--导出为xml文件mysql -X -uroot -proot -e "use testa;select * from test_tb;" > /opt/test.xml--导出为csv文件--fields terminated 分割记录中每个字段的字符--optionally enclosed 包围每个字段的字符--lines terminated 每行结束的字符--P…

【HDU - 4990】 Reading comprehension (构造+矩阵快速幂)

题干&#xff1a; Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include <cstring> #include <cmath> #include &…

finereport文本框如何实现多值查询_如何实现参数级联查询

参数级联查询是查询控件之间的一种互动方式&#xff0c;比如在某个下拉框选定选项后&#xff0c;另一个下拉框里的选项范围会随之变化。润乾报表提供了多种编辑风格&#xff0c;每种编辑风格都有丰富的属性&#xff0c;以此为基础实现参数级联查询也很简单。下面就通过一个例子…

【HDU - 5015 】233 Matrix (矩阵快速幂)

题干&#xff1a; In our daily life we often use 233 to express our feelings. Actually, we may say 2333, 23333, or 233333 ... in the same meaning. And here is the question: Suppose we have a matrix called 233 matrix. In the first line, it would be 233, 233…

【HDU - 2899】 Strange fuction(二分或三分,求导)

题干&#xff1a; Now, here is a fuction: F(x) 6 * x^78*x^67*x^35*x^2-y*x (0 < x <100) Can you find the minimum value when x is between 0 and 100. Input The first line of the input contains an integer T(1<T<100) which means the number of…

php mysql html标签_HTML标签格式化PHP和MySQL

我有这个MySQL语句Select type.type, color.color, ShotName, Item.name, Item.Item_idFrom typeInner Join ItemOn type.type_id Item.type_idInner Join colorOn color.color_id Item.color_idWhere Item.state0 And Item.offline 0Group By color.color, Item.name, type.o…

【CF#706B】 Interesting drink (二分)

题干&#xff1a; 瓦西里喜欢在努力工作后休息&#xff0c;所以你可能经常在附近的一些酒吧见到他。他喜欢 "Beecola"&#xff0c;可以从 n 个不同的商店买到。在第 i 个商店的价格为 xi 元。 瓦西里计划购买他最喜欢的饮料 q 次。在第 i 天他能花 mi 元。他想知道每…

mysql datetime month不走索引_like百分号加前面一定不走索引吗?一不小心就翻车,关于mysql索引那些容易错的点...

like百分号加前面一定不走索引吗&#xff1f;正常来讲&#xff0c;我们都知道在mysql的like查询中&#xff0c;百分号加在关键词后面是走索引的&#xff0c;比如 select * like "张三%"&#xff0c;而百分号在前面是不走索引的&#xff0c;比如 select * like "…