标签:
Time Limit: 5000MS | Memory Limit: 65536K | |||
Total Submissions: 2277 | Accepted: 703 | Special Judge |
Description
Input
Output
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
Source
#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
pair<int, int > p[100005];
int n, m, k;
void solve(int k)
{
int l = 0, r = 1, al, ar, av, minn = inf;
while (l<=n&&r<=n&&minn!=0)
{
int temp=p[r].first - p[l].first;
if (abs(temp - k) < minn)
{
minn = abs(temp - k);
ar = p[r].second;
al = p[l].second;
av = temp;
}
if (temp> k)
l++;
else if (temp < k)
r++;
else
break;
if (r == l)
r++;
}
if(al>ar)
swap(al,ar);//因为al和ar大小没有必然关系()取绝对值,所以//要交换
printf("%d %d %d\n", av, al+1, ar);
}
int main()
{
while (~scanf("%d %d", &n, &m))
{
if (!n&&!m) return 0;
p[0] = make_pair(0, 0);
for (int i = 1; i <= n; i++)
{
scanf("%d", &p[i].first);
p[i].first += p[i - 1].first;
p[i].second = i;
}
sort(p, p + n + 1);
while (m--)
{
scanf("%d", &k);
solve(k);
}
}
return 0;
}
下面是自己的wa代码
好好找茬
#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
pair<int, int > p[100005];
int n, m, k;
void solve(int k)
{
int l = 0, r = 1, al, ar, av, minn = inf;
while (l<=n&&r<=n)
{
int temp = p[r].first - p[l].first;
if (abs(temp - k) < minn)
{
minn = abs(temp - k);
ar = p[r].second;
al = p[l].second;
av = temp;
}
if (temp > k)
l++;
else if (temp < k)
r++;
else
break;
if (r == l)
r++;
}
printf("%d %d %d\n", av, al+1, ar);
}
int main()
{
while (~scanf("%d %d", &n, &m))
{
if (!n&&!m) return 0;
p[0] = make_pair(0, 0);
for (int i = 1; i <= n; i++)
{
scanf("%d", &p[i].first);
p[i].first += p[i - 1].first;
p[i].second = i;
}
sort(p + 1, p + n + 1);
while (m--)
{
scanf("%d", &k);
solve(k);
}
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/smilesundream/p/5129758.html