2013 ACM/ICPC 长沙网络赛J题

#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;

#define MAX_NUM 100005

int array[MAX_NUM], array1[MAX_NUM], array2[MAX_NUM];
int sum[MAX_NUM];
int num;
int query_num;
bool filled;

void cal_left(int pos, int array[])
{
for (int i = pos - 2; i > 0; i--)
array[i] = sum[i + 1] - array[i + 1] - array[i + 2];
}

void cal_right(int pos, int array[])
{
for (int i = pos + 2; i <= num; i++)
array[i] = sum[i - 1] - array[i - 1] - array[i - 2];
}

void input()
{
int pos = -1;
for (int i = 1; i <= num; i++)
{
scanf("%d", &array[i]);
if (i % 3 != 0 && array[i] != -1)
pos = i;
}
for (int i = 1; i <= num; i++)
scanf("%d", &sum[i]);
array[0] = 0;
for (int i = 3; i <= num; i += 3)
array[i] = sum[i - 1] - (sum[i - 2] - array[i - 3]);
if (num % 3 == 2 && pos == -1)
return;
filled = true;
if (array[num] != -1)
array[num - 1] = sum[num] - array[num];
if (array[num - 1] != -1)
array[num] = sum[num] - array[num - 1];
if (array[num] != -1)
cal_left(num, array);
array[num + 1] = 0;
if (pos != -1)
{
if (array[pos - 1] != -1)
pos--;
cal_left(pos + 1, array);
cal_right(pos, array);
}
}

void work()
{
scanf("%d", &query_num);
for (int i = 0; i < query_num; i++)
{
int a;
scanf("%d", &a);
a++;
if (filled)
{
printf("%d\n", array[a]);
continue;
}
if (a % 3 == 1)
printf("%d\n", array1[a]);
else if (a % 3 == 2)
printf("%d\n", array2[a]);
else
printf("%d\n", array[a]);
}
}

void make2()
{
memcpy(array2, array, sizeof(array2));
array2[1] = 0;
array2[2] = sum[1] - array2[1];
cal_right(1, array2);
array2[1] -= *min_element(array2 + 1, array2 + num + 1);
array2[2] = sum[1] - array2[1];
cal_right(1, array2);
}

void make1()
{
memcpy(array1, array, sizeof(array1));
array1[num] = 0;
array1[num - 1] = sum[num] - array1[num];
cal_left(num, array1);
array1[num] -= *min_element(array1 + 1, array1 + num + 1);
array1[num - 1] = sum[num] - array1[num];
cal_left(num, array1);
}

int main()
{
while (scanf("%d", &num) != EOF)
{
filled = false;
input();
if (!filled)
{
make2();
make1();
}
work();
}
return 0;
}
View Code

posted @ 2013-09-23 12:53 金海峰 阅读(...) 评论(...) 编辑 收藏