mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create 2566_wiking__acm.cpp
From http://blog.csdn.net/wiking__acm/article/details/9910415
This commit is contained in:
parent
4c09336c24
commit
267e0c69bd
55
POJ/2566_wiking__acm.cpp
Normal file
55
POJ/2566_wiking__acm.cpp
Normal file
|
@ -0,0 +1,55 @@
|
|||
#include<cstdio>
|
||||
#include<iostream>
|
||||
#include<algorithm>
|
||||
using namespace std;
|
||||
const int maxn = 100000 + 5;
|
||||
const int INF = 2000000000;
|
||||
typedef pair<int, int> P;
|
||||
typedef long long LL;
|
||||
|
||||
P p[maxn];
|
||||
|
||||
int Abs(int x){return x<0?-x:x;}
|
||||
int n;
|
||||
|
||||
void query(int tar){
|
||||
int s = 0,e = 1,Min = INF;
|
||||
int ansl,ansr,ansx;
|
||||
while(s <= n && e <= n){
|
||||
int tem = p[e].first-p[s].first;
|
||||
if(Abs(tem-tar) < Min){
|
||||
Min = Abs(tem-tar);
|
||||
ansx = tem;
|
||||
ansl = p[s].second;
|
||||
ansr = p[e].second;
|
||||
}
|
||||
if(tem > tar) s++;
|
||||
else if(tem < tar) e++;
|
||||
else break;
|
||||
if(s == e) e++;
|
||||
}
|
||||
if(ansl > ansr) swap(ansl,ansr);
|
||||
printf("%d %d %d\n",ansx,ansl+1,ansr);
|
||||
}
|
||||
|
||||
int main(){
|
||||
int m;
|
||||
while(scanf("%d%d",&n,&m)){
|
||||
if(n == 0 && m == 0) break;
|
||||
p[0] = P(0,0);
|
||||
int sum = 0;
|
||||
for(int i = 1;i <= n;i++){
|
||||
int tem;
|
||||
scanf("%d",&tem);
|
||||
sum += tem;
|
||||
p[i] = P(sum,i);
|
||||
}
|
||||
sort(p,p+n+1);
|
||||
while(m--){
|
||||
int x;
|
||||
scanf("%d",&x);
|
||||
query(x);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user