pat 1046 (Dijstra只能过俩个case,这里的方法很巧妙)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 100005;
int dis[MAXN] , A[MAXN];
int main()
{
int sum = 0 , query , n , left , right;
scanf("%d" , &n);
int i;
for(i = 1 ; i <= n ; ++i)
{
scanf("%d" , &A[i]);
sum += A[i];
dis[i] = sum;
}
scanf("%d" , &query);
for(i = 0 ; i < query ; ++i)
{
scanf("%d%d" , &left , &right);
if(left > right)
swap(left , right);
int temp = dis[right - 1] - dis[left - 1];
printf("%d\n" , min(temp , sum - temp));
}
return 0;
}

热评文章