Codeforces 371E Subway Innovation (前缀和预处理应用)
题目链接 Subway Innovation
首先不难想到所求的k个点一定是连续的,那么假设先选最前面的k个点,然后在O(1)内判断第2个点到第k+1个点这k个点哪个更优。
判断的时候用detla[i]来记录信息。令delta[k+1]+delta[k+2]+......+delta[k+x] = sum[x],则sum[x]最大时,x即为排完序后的要选的k个点的最大的编号。
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for(int i(a); i <= (b); ++i)
#define LL long long const int N = ; struct node{
LL x, y;
friend bool operator < (const node &a, const node &b){
return a.x < b.x;
}
} a[N]; LL x[N], s[N], delta[N];
LL n, k, p, ss, ans, cnt; int main(){ scanf("%lld", &n);
s[] = ;
rep(i, , n){
scanf("%lld", &a[i].x);
a[i].y = i;
} scanf("%lld", &k);
sort(a + , a + n + );
rep(i, , n) x[i] = a[i].x;
rep(i, , n) s[i] = s[i - ] + x[i];
int j = ;
rep(i, k + , n){
++j;
delta[i] = (k - ) * (x[i] + x[j]) - * (s[i - ] - s[j]);
}
cnt = ans = , p = k;
rep(i, k + , n){
cnt += delta[i];
if (ans > cnt){
ans = cnt;
p = i;
}
}
rep(i, p - k + , p) printf("%lld\n", a[i].y);
return ; }
Codeforces 371E Subway Innovation (前缀和预处理应用)的更多相关文章
- BZOJ-1587|前缀和 预处理 dp||叶子合并leaves
叶子合并leaves Description 在一个美丽的秋天,丽丽每天都经过的花园小巷落满了树叶,她决定把树叶堆成K堆,小巷是笔直的 共有N片树叶(树叶排列也是笔直的),每片树叶都有一个重量值,并且 ...
- HDU 5550 - Game Rooms(DP + 前缀和预处理)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5550 题意: 一个大楼有n(2≤n≤4000)层,每层可以建一个乒乓球房或者一个游泳房,且每种房间在大楼 ...
- LightOJ-1007-Mathematically Hard-欧拉函数打表+前缀和+预处理
Mathematically some problems look hard. But with the help of the computer, some problems can be easi ...
- Codeforces Round #552 (Div. 3) F. Shovels Shop (前缀和预处理+贪心+dp)
题目:http://codeforces.com/contest/1154/problem/F 题意:给你n个商品,然后还有m个特价活动,你买满x件就把你当前的x件中最便宜的y件价格免费,问你买k件花 ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D 数学+(前缀 后缀 预处理)
D. "Or" Game time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化
链接:http://codeforces.com/contest/665/problem/E 题意:求规模为1e6数组中,连续子串xor值大于等于k值的子串数: 思路:xor为和模2的性质,所以先预处 ...
- Codeforces 1082C Multi-Subject Competition 前缀和 A
Codeforces 1082C Multi-Subject Competition https://vjudge.net/problem/CodeForces-1082C 题目: A multi-s ...
- Codeforces Round #540 Tanya and Candies 预处理
http://codeforces.com/contest/1118/problem/B 题目大意,给你一个序列,删去一个数值之后,要求剩下序列奇数和偶数的和相同,问有多少种删法. 思路:预处理奇数和 ...
- Codeforces Round #355 (Div. 2) C 预处理
C. Vanya and Label time limit per test 1 second memory limit per test 256 megabytes input standard i ...
随机推荐
- java并发面试题-基础
多线程 java中有几种方法可以实现一个线程? 1.直接继承thread类:2.实现runnable接口: 如何停止一个正在运行的线程?可以使用正在运行的线程,支持线程中断,通常是定义一个volati ...
- 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式
1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1469 Solved: ...
- setTimeout相关整理
setTimeout里面函数有无双引号的区别 双引号中的作用域不捕捉局部变量,不用双引号包着的是捕捉局部作用域 var a = function(){ alert(1111) } function a ...
- python2.7运行报警告:UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal解决办法
1. 程序源代码报错部分: #选择年级if grade == '幼升小': outline.nianji().pop(0).click()elif grade == "一年级": ...
- mvc-自定义Route
public class CustomerRoute : RouteBase { //从路径中解析出controller.action以及其他参数,创建RouteData(其中包括HttpHandle ...
- jquery的html、text、val的用法
.html()用为读取和修改元素的HTML标签 .text()用来读取或修改元素的纯文本内容 .val()用来读取或修改表单元素的value值. 这三个方法功能上的对比 .html(),.text() ...
- java 获取请求的完整url地址
String url=request.getRequestURL()+"?"+request.getQueryString();
- HLG1125 循环小数2
循环小数 II Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 155(55 users) Total Accepted: 92(51 ...
- Spring aop报错:com.sun.proxy.$Proxyxxx cannot be cast to yyy
在使用Spring AOP时,遇到如下的错误: Exception in thread "main" java.lang.ClassCastException: com.sun.p ...
- 【bzoj3944/bzoj4805】Sum/欧拉函数求和 杜教筛
bzoj3944 题目描述 输入 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 输出 一共T行,每行两个用空格分隔的数ans1,ans2 样例输 ...