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 ...
随机推荐
- 50道SQL练习题及答案与详细分析(MySQL)
50道SQL练习题及答案与详细分析(MySQL) 网上的经典50到SQL题,经过一阵子的半抄带做,基于个人理解使用MySQL重新完成一遍,感觉个人比较喜欢用join,联合查询较少 希望与大家一起学习研 ...
- CSS选择器整理
基本选择器 标签选择器:直接写标签名 id选择器:#id名 class选择器:.class名 通配选择器:* 组合选择器 交集:ABCDEFG...... 并集:E, F, G, ...... 关系选 ...
- win7系统中开启wifi热点
1.进入cmd下 2.输入命令创建一个热点,名称为testwifi,密码为12345678 netsh wlan 3.进入网络和共享中心->更改适配器设置,看到多出一个“无线网络连接2”,选中本 ...
- Django:cookie和session相关问题
http://www.cnblogs.com/fnng/p/3750596.html http://www.cnblogs.com/chenchao1990/p/5283725.html
- warning:Pointer is missing a nullability type specifier (__nonnull or __nullable)
当我们定义某个属性的时候 如果当前使用的编译器版本比较高(6.3+)的话经常会遇到这样一个警告:warning:Pointer is missing a nullability type speci ...
- 提高unigui开发效率的两个方法(02)
1.编译时自己退出运行的程序. 在做unigui开发时,每次编译运行时,unigui的应用都会在后台运行,每次重新编译时都必须手工在任务栏里将应用退出才行,非常麻烦,可以在项目编译的参数里加上杀进程的 ...
- Java 理论与实践: 哈希
有效和正确定义hashCode()和equals() 每个Java对象都有 hashCode() 和 equals() 方法.许多类 Override 这些方法的缺省实施,以在对象实例之间提供更深层次 ...
- 1 Oracle概述&与MySQL的差别&SQL语句分类复习
一. 知识点目录 Oracle的概念和安装 基本查询 条件查询 Oracle中的函数 多表查询 子查询 表空间的状态 用户 视图 索引 序列 同义词 PLSQL编程 游标 存储过程 存储函数 触发器 ...
- Linux系统资深运维工程师的进阶秘籍
2010年毕业,从事IT行业已经接近7个年头,一路走来有很多不足,不论是技术上的还是工作当中的待人接事等,但正是这些不足让我有了现在的进步,技术上从最初的做水晶头,综合布线到服务器上架,网络设备调试, ...
- Codeforces 598D:Igor In the Museum
D. Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standar ...