Day8 - G - Bound Found ZOJ - 1964
Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronautic and Space Administration (that must be going through a defiant phase: "But I want to use feet, not meters!"). Each signal seems to come in two parts: a sequence of n integer values and a non-negative integer t. We'll not go into details, but researchers found out that a signal encodes two integer values. These can be found as the lower and upper bound of a subrange of the sequence whose absolute value of its sum is closest to t.
You are given the sequence of n integers and the non-negative target t. You are to find a non-empty range of the sequence (i.e. a continuous subsequence) and output its lower index l and its upper index u. The absolute value of the sum of the values of the sequence from the l-th to the u-th element (inclusive) must be at least as close to t as the absolute value of the sum of any other non-empty range.
Input
The input file contains several test cases. Each test case starts with two numbers n and k. Input is terminated by n = k = 0. Otherwise, 1 <= n <= 100000 and there follow n integers with absolute values <= 10000 which constitute the sequence. Then follow k queries for this sequence. Each query is a target t with 0 <= t <= 1000000000.
Output
For each query output 3 numbers on a line: some closest absolute sum and the lower and upper indices of some range where this absolute sum is achieved. Possible indices start with 1 and go up to n.
Sample Input
5 1
-10 -5 0 5 10
3
10 2
-9 8 -7 6 -5 4 -3 2 -1 0
5 11
15 2
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
15 100
0 0
Sample Output
5 4 4
5 2 8
9 1 1
15 1 15
15 1 15
思路:维护前缀和,排序后维护双指针取值,细节直接看代码
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = 1e5+; int n, k; LL C[maxm]; void add(int x, int val) {
for(; x <= n; x += lowbit(x))
C[x] += val;
} LL getsum(int x) {
LL ret = ;
for(; x; x -= lowbit(x))
ret += C[x];
return ret;
} struct Node {
LL val;
int id;
bool operator<(const Node &a) const {
return val < a.val;
}
} Nodes[maxm]; int main() {
ios::sync_with_stdio(false), cin.tie();
int t, val;
while(cin >> n >> k && n+k) {
memset(C, , sizeof(C));
for(int i = ; i <= n; ++i) {
cin >> val;
add(i, val);
Nodes[i] = {getsum(i), i};
}
Nodes[].id = , Nodes[].val = ;
sort(Nodes,Nodes++n);
while(k--) {
int l = , r = , ansL, ansR;
LL ans, t, diff = 0x3f3f3f3f;
cin >> t;
while(l <= n && r <= n) {
LL val = Nodes[r].val - Nodes[l].val;
if(abs(val - t) < diff) {
ans = val;
diff = abs(val-t);
ansL = min(Nodes[l].id+,Nodes[r].id+);
ansR = max(Nodes[l].id, Nodes[r].id);
}
if(val > t)
l++;
else if(val < t)
r++;
else break;
if(l == r) r++;
}
cout << ans << " " << ansL << " " << ansR << "\n";
} } return ;
}
Day8 - G - Bound Found ZOJ - 1964的更多相关文章
- hdu4507
数位dp,终于守得云开见月明了.建议初学者先试试两道比较简单的hdu2089,hdu3555. 鸣谢:http://blog.csdn.net/acm_cxlove/article/details/8 ...
- HZNU Training 1 for Zhejiang Provincial Collegiate Programming Contest
赛后总结: TJ:今天我先到实验室,开始看题,一眼就看了一道防AK的题目,还居然觉得自己能做wwww.然后金姐和彭彭来了以后,我和他们讲了点题目.然后金姐开始搞dfs,我和彭彭看榜研究F题.想了很久脑 ...
- Solution -「多校联训」数学考试
\(\mathcal{Description}\) Link. 给定 \(n\) 个函数,第 \(i\) 个有 \(f_i(x)=a_ix^3+b_ix^2+cx_i+d~(x\in[l_i, ...
- Storyboards Tutorial 03
这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...
- 文件图标SVG
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...
- ZOJ 4010 Neighboring Characters(ZOJ Monthly, March 2018 Problem G,字符串匹配)
题目链接 ZOJ Monthly, March 2018 Problem G 题意 给定一个字符串.现在求一个下标范围$[0, n - 1]$的$01$序列$f$.$f[x] = 1$表示存在一种 ...
- POJ 2240 && ZOJ 1082 Arbitrage 最短路,c++ stl pass g++ tle 难度:0
http://poj.org/problem?id=2240 用log化乘法为加法找正圈 c++ 110ms,g++tle #include <string> #include <m ...
- zoj 4020 The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light(广搜)
题目链接:The 18th Zhejiang University Programming Contest Sponsored by TuSimple - G Traffic Light 题解: 题意 ...
- 2017CCPC秦皇岛G ZOJ 3987Numbers(大数+贪心)
Numbers Time Limit: 2 Seconds Memory Limit: 65536 KB DreamGrid has a nonnegative integer n . He ...
随机推荐
- 【协作式原创】查漏补缺之Golang中mutex源码实现
概览最简单版的mutex(go1.3版本) 预备知识 主要结构体 type Mutex struct { state int32 // 指代mutex锁当前的状态 sema uint32 // 信号量 ...
- OA:办公自动化———笔记一
oa:办公自动化 1.对公司结构的管理 基础数据管理 部门进行管理 角色进行管理 权限进行管理 员工进行管理 2.流程管理 利用工作流技术对比较 ...
- 十三 Struts2复杂类型的数据封装,List封装和Map封装
在实际开发当中,有可能遇到批量向数据库中插入记录,需要在页面中将数据封装到集合中.类似页面表达式方法 List封装: 前端JSP: <%@ page language="java&qu ...
- ArcMap中对失量数据将具有相同的字段的元素进行合并
ArcMap=>工具栏=>Geoprocessing=>Dissolve,由于是将多个元素进行合并,所以还涉及到合并后的元素的字段保留以及字段取值的问题,在该工具中还可以自定义保存的 ...
- 【原】python异步学习
https://www.liaoxuefeng.com/wiki/1016959663602400/1017959540289152 https://www.jianshu.com/p/b5e347b ...
- c++中的运算符重载operator1(翁恺c++公开课[30]学习笔记)
运算符重载规则: 只有已经存在的运算符才能被重载,不能自己制造一个c++中没有的运算符进行重载 重载可以在类或枚举类型内进行,也可以是全局函数,但int.float这种已有的类型内是不被允许的 不能二 ...
- 企业面试问题收集-ssh框架
SSH框架阶段 SSH的优缺点,使用场景? Hibernate优缺点 Hibernate优点:(1) 对象/关系数据库映射(ORM)它使用时只需要操纵对象,使开发更对象化,抛弃了数据库中心的思想,完全 ...
- JavaScript数组用法
本文介绍一些js数组的用法: 上图的要点为: 1.unshift增加数组头部的元素,shift删除数组头部的元素. 2.delete除可删除对象的属性外,还可以删除数组的元素,使其占位变为undefi ...
- php后门拿下当前目录
Weevely是一个模拟类似telnet的连接的隐形PHP网页shell.它是Web应用程序后期开发的必备工具,可以作为隐形后门程序,也可以作为一个web外壳来管理合法的Web帐户,甚至可以免费托管. ...
- 概率图模型(PGM,Probabilistic Graphical Model)
PGM是现代信号处理(尤其是机器学习)的重要内容. PGM通过图的方式,将多个随机变量之前的关系通过简洁的方式表现出来.因此PGM包括图论和概率论的相关内容. PGM理论研究并解决三个问题: 1)表示 ...