1046. Shortest Distance (20)

考察环模拟操作

#include<iostream>

#define MAX 100000
int g_Dis[MAX];
int g_Sum[MAX+1];
int Min(int a, int b)
{
	if(a < b)
		return a;
	else return b;
}
int main()
{
	int n;
	while( scanf("%d", &n) != EOF )
	{
		int total = 0;
		g_Sum[0] = 0;
		for(int i = 0; i < n; ++i)
		{
			scanf("%d", &g_Dis[i]);
			total += g_Dis[i];
			if(i == 0)
				g_Sum[i+1] = g_Dis[i];
			else
				g_Sum[i+1] = g_Sum[i]+g_Dis[i];
		}
		/*for(int i = 0; i <= n; ++i)
			printf("%d ", g_Sum[i]);
		printf("\n");*/
		//quest
		int m;
		scanf("%d", &m);
		while(m--)
		{
			int a, b;
			scanf("%d%d", &a, &b);
			if(a > b)
			{
				int temp = a;
				a = b;
				b = temp;
			}
			int ans = g_Sum[b-1]-g_Sum[a-1];
			ans = Min(ans, total-ans);
			printf("%d\n", ans);
		}
	}
	return 0;
}


 

你可能感兴趣的:(pat,ZJU)