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 ...
随机推荐
- SHIWEITI
//Wannafly挑战赛19(牛客网) //A 队列Q #include <iostream> #include <cstdio> #include <cstring& ...
- WPF实现QQ群文件列表动画(二)
上篇(WPF实现QQ群文件列表动画(一))介绍了WPF实现QQ群文件列表动画的大致思路,结合我之前讲过的WPF里ItemsControl的分组实现,实现起来问题不大,以下是效果图: 其实就是个List ...
- Linux QA
gitee: https://gitee.com/dhclly/icedog.script.test/blob/master/doc/linux/linux-qa.md 1. linux 中的 ll( ...
- 使用html进行浏览器判断,浏览器条件注释
下面来点今天写东西的时候查资料,收集的关于使用html进行浏览器判断的一些资料: 条件注释的基本格式: <!--[if expression]>注释内容<![endif]--> ...
- 如何使用PowerShell管理Windows服务
[TechTarget中国原创] 作为一名系统管理员,最常见的任务之一就是学会管理Windows服务,这是保证Windows服务器和客户端正常运行的重要内容. 许多操作系统和应用程序都依赖于这些服务. ...
- 35、键盘布局的tableLayout备份
<TableLayout android:layout_width="wrap_content" android:layout_height="wrap_conte ...
- 【Reverse Linked List II】cpp
题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...
- iOS笔记052- Quartz2D-绘图
简介 Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统 Quartz 2D能完成的工作 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘 ...
- C++模板编程-模板基础重点
模板基础 1.模板参数自动推导,如果是已知的参数类型与个数,这调用模板时可以不写类型. Cout<<max<int>(1,3);可以写为Cout<<max(1,3) ...
- JWT实现token认证
演变 1.1 有状态服务 在原来的单体系统中,有状态服务,即服务端需要记录每次会话的客户端信息,从而识别客户端身份,根据用户身份进行请求的处理,session+cookie.在如今的大多数分布式系统中 ...