题目链接 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 (前缀和预处理应用)的更多相关文章

  1. BZOJ-1587|前缀和 预处理 dp||叶子合并leaves

    叶子合并leaves Description 在一个美丽的秋天,丽丽每天都经过的花园小巷落满了树叶,她决定把树叶堆成K堆,小巷是笔直的 共有N片树叶(树叶排列也是笔直的),每片树叶都有一个重量值,并且 ...

  2. HDU 5550 - Game Rooms(DP + 前缀和预处理)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=5550 题意: 一个大楼有n(2≤n≤4000)层,每层可以建一个乒乓球房或者一个游泳房,且每种房间在大楼 ...

  3. LightOJ-1007-Mathematically Hard-欧拉函数打表+前缀和+预处理

    Mathematically some problems look hard. But with the help of the computer, some problems can be easi ...

  4. Codeforces Round #552 (Div. 3) F. Shovels Shop (前缀和预处理+贪心+dp)

    题目:http://codeforces.com/contest/1154/problem/F 题意:给你n个商品,然后还有m个特价活动,你买满x件就把你当前的x件中最便宜的y件价格免费,问你买k件花 ...

  5. 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 ...

  6. Educational Codeforces Round 12 E. Beautiful Subarrays 预处理+二叉树优化

    链接:http://codeforces.com/contest/665/problem/E 题意:求规模为1e6数组中,连续子串xor值大于等于k值的子串数: 思路:xor为和模2的性质,所以先预处 ...

  7. Codeforces 1082C Multi-Subject Competition 前缀和 A

    Codeforces 1082C Multi-Subject Competition https://vjudge.net/problem/CodeForces-1082C 题目: A multi-s ...

  8. Codeforces Round #540 Tanya and Candies 预处理

    http://codeforces.com/contest/1118/problem/B 题目大意,给你一个序列,删去一个数值之后,要求剩下序列奇数和偶数的和相同,问有多少种删法. 思路:预处理奇数和 ...

  9. 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 ...

随机推荐

  1. Android广告页循环播放

    摘要:项目要求做一个广告页,实现几秒更换一次广告页,下方还有指示第几张广告页,同样也支持手动左滑或右滑. 1.准备好粘贴5个有关广告页的类. ①BaseViewPager==>自定义高度的Vie ...

  2. 【Keepalived+MySQL】MySQL双主互备+高可用

    一.基本信息说明 [DB1] IP: 192.168.102.144 hostname: LVS-Real1 [DB2] IP: 192.168.102.145 hostname: LVS-Real2 ...

  3. Android学习笔记之-----讯飞语音识别实例化RecognizerDialog参数出现错误的解决方法

    本人也是个小菜鸟,在做语音识别时也遇到了这个问题,空指针一直报错,app程序停止运行. 在网上搜了半天在这个帖子里找到了解决方法:http://bbs.xfyun.cn/forum.php?mo .. ...

  4. 报错: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: An attempt by a client to checkout a Connection has timed out. 数据库连接超时

    解决方法一: [oracle@data ~]$ sqlplus / as sysdba——连接到数据库 SQL*Plus: Release 11.2.0.4.0 Production on Mon M ...

  5. python - web自动化测试 - 元素操作 - 等待

    # -*- coding:utf-8 -*- ''' @project: web学习 @author: Jimmy @file: wait.py @ide: PyCharm Community Edi ...

  6. Chrome autocomplete="off"无效

    如果不希望输入框自动填充,可以设置 input 或 textarea 标签的属性 autocomplete="off". 但是有时 Chrome 会忽视 autocomplete ...

  7. tinyipa make

    参考:http://tinycorelinux.net/ Ironic Python Agent repo还提供了一组脚本,用于在imagebuild / tinyipa文件夹下构建一个基于Linux ...

  8. TOJ5272: 逆矩阵

    5272: 逆矩阵  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 11         ...

  9. 【转】UGUI之用脚本动态的改变Button的背景图片 和 颜色

    http://blog.csdn.net/u014771617/article/details/45102701 public Button button;void Start(){ColorBloc ...

  10. 【bzoj3217】ALOEXT 替罪羊树套Trie树

    题目描述 taorunz平时最喜欢的东西就是可移动存储器了……只要看到别人的可移动存储器,他总是用尽一切办法把它里面的东西弄到手. 突然有一天,taorunz来到了一个密室,里面放着一排可移动存储器, ...