Bound Found
Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 2897 | Accepted: 888 | Special Judge |
Description
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.
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
尺取法的一般用法就是对于一个连续区间求解某些最值,此题便是如此
/*头文件模板*/ #include <map> #include <set> #include <cmath> #include <ctime> #include <queue> #include <vector> #include <cctype> #include <cstdio> #include <string> #include <cstring> #include <sstream> #include <cstdlib> #include <typeinfo> #include <iostream> #include <algorithm> #include <functional> using namespace std; #define pb push_back #define mp make_pair #define mem(a, x) memset(a, x, sizeof(a)) #define copy(a, b) memcpy(a, b, sizeof(a)) #define lson rt << 1, l, mid #define rson rt << 1|1, mid + 1, r #define FIN freopen("input.txt", "r", stdin) #define FOUT freopen("output.txt", "w", stdout) typedef long long LL; typedef pair<int, int > PII; typedef pair<int,string> PIS; typedef unsigned long long uLL; template<typename T> void print(T* p, T* q, string Gap = " ", bool flag = false) { int d = p < q ? 1 : -1; while(p != q) { if(flag) cout << Gap[0] << *p << Gap[1]; else cout << *p; p += d; if(p != q && !flag) cout << Gap; } cout << endl; } template<typename T> void print(const T &a, string bes = "") { int len = bes.length(); if(len >= 2)cout << bes[0] << a << bes[1] << endl; else cout << a << endl; } void IO_Init() { ios::sync_with_stdio(false); } LL LLabs(LL a) { return a > 0 ? a : -a; } const double PI = 3.1415926535898; const double eps = 1e-10; const int MAXM = 1e4 + 5; const int MAXN = 1e5 + 5; const LL INF = 0x3f3f3f3f; /*头文件模板*/ int n, k; LL c, x; struct o { LL r, w; bool operator < (const o &p) const { return w < p.w; } } A[MAXN]; void cal(LL f) { LL l = 0, r = 1LL, res ,ans = INF, resl = 0LL, resr = A[1].r; while(r <= n && ans) { LL h = A[r].w - A[l].w ; if(ans > LLabs(h - f)) { resl = A[l].r; resr = A[r].r; res = A[r].w - A[l].w; ans = LLabs(h - f); } if(h > f && l < r - 1) { l ++; continue; } r ++; } if(resr < resl) swap(resr, resl); printf("%lld %lld %lld\n", res, resl + 1, resr); } int main() { while(~scanf("%d%d", &n, &k), n || k) { A[0].w = 0; A[0].r = 0; for(int i = 1; i <= n; i ++) { scanf("%lld", &c); A[i].w = A[i - 1].w + c; A[i].r = i; } sort(A, A + n + 1); for(int i = 0; i < k; i ++) { scanf("%lld", &x); cal(x); } } return 0; }
个人想缓缓,太坑爹了,自己做题经验还是不足,对于问题一定要分析清楚,而不能直接下手,不然倒霉的绝对是自己