http://poj.org/problem?id=2566
2.Idea
Two Pointers
3.Source
struct Item {
int sum;
int end;
bool operator < (const Item &a)const {
return sum < a.sum;
}
} items[100005];
int N, K, T;
void solve()
{
int x, y, s = 0, t = 0, ans = 1000000007, maxn = 1000000007;
while (s <= N && t <= N && maxn) {
if (s == t) {
t++;
}
else {
int sum = items[t].sum - items[s].sum;
if (maxn > abs(sum - T)) {
ans = sum;
maxn = abs(sum - T);
x = min(items[s].end, items[t].end) + 1;
y = max(items[s].end, items[t].end);
}
if (sum > T) {
s++;
}
if (sum < T) {
t++;
}
}
}
printf("%d %d %d\n", ans, x, y);
}
int main()
{
while (scanf("%d%d", &N, &K) > 0, N + K) {
items[0].end = 0;
items[0].sum = 0;
for (int i = 1; i <= N; i++) {
int x;
scanf("%d", &x);
items[i].end = i;
items[i].sum = items[i - 1].sum + x;
}
sort(items, items + N + 1);
for (int i = 0; i < K; i++) {
scanf("%d", &T);
solve();
}
}
return 0;
}
No comments:
Post a Comment