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 ...
随机推荐
- DOS中断及程序调用
http://www.cnblogs.com/ynwlgh/archive/2011/12/12/2285017.html
- python资源大全2
原文链接 网络 Scapy, Scapy3k: 发送,嗅探,分析和伪造网络数据包.可用作交互式包处理程序或单独作为一个库. pypcap, Pcapy, pylibpcap: 几个不同 libpcap ...
- IQueryable与IEnumerable区别
前者可以延迟加载,即执行完后不马上执行数据库语句,用到再加载.
- cf975d Ghosts
ref #include <algorithm> #include <iostream> #include <cstdio> #include <map> ...
- 手机APP设计网
http://hao.xueui.cn/ http://www.25xt.com/
- 如何将Linux rm命令删除的文件放入垃圾箱
因为rm命令删除的文件是不会放入垃圾箱的,所以无法恢复,下面小编就给大家介绍一种方法,通过替换Linux rm命令的方法,从而将rm命令删除的文件放入垃圾箱. 方法: 1. 在/home/userna ...
- 【转】Unity 游戏存档 PlayerPrefs类的用法
http://www.cnblogs.com/qiaogaojian/p/5969855.html unity3d提供了一个用于本地持久化保存与读取的类——PlayerPrefs.工作原理非常简单,以 ...
- “取出数据表中第10条到第20条记录”的sql语句+selecttop用法
1.首先,select top用法: 参考问题 select top n * from和select * from的区别 select * from table -- 取所有数据,返回无序集合 sel ...
- AGC 26 F Manju Game
$\DeclareMathOperator{\sw}{sw}$ $\DeclareMathOperator{\sb}{sb}$ $\DeclareMathOperator{\dp}{dp}$ 用 $\ ...
- git filter-branch应用
1.修改author和committer git filter-branch --commit-filter ' export GIT_AUTHOR_EMAIL=me@example.com; exp ...