The program isn't pass, and the result is RE-- run time error.
although it can pass for the example data.
I can't find the error. I felt it waste time to dig it out, and I don't plan to find it out, at least currently.
Why there isn't any prompt int the online judge system? It should be easy to do it!!!
Thought:
consider 3 lines, start from one position x - w, x, x + w, and the continuous same data are [x - w, a], [x, b], [x + w, c].
then make n = min ((b - x), (a - (x -w)), (c - (x + w)))
so, there are at most 3 results: the 2 edge value3, and mid value ( same and count is n -2)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int len_in = -1;
int len_out = -1;
int width = 0;
int all = 0;
int in[1024][2] = {{0,0}};
int out[1024 * 20][2] = {{0,0}};
//#define DEBUG
#ifdef DEBUG
#define D(...)
//#define D(...) printf(__VA_ARGS__)
#else
#define D(...)
#endif
static void input(int a, int b);
static void process();
static void output();
int abcd;
int main(int ac, char**av)
{
int a, b;
while (1)
{
scanf("%d", &width);
if (width == 0)
{
printf("0\n");
break;
}
len_in = -1;
len_out = 0;
all = 0;
memset(out, 0, sizeof(out));
while (scanf("%d %d", &a, &b))
{
if (a == 0 && b == 0)
{
process();
output();
break;
}
input(a, b);
}
}
return 0;
}
void input(int a, int b)
{
len_in++;
in[len_in][0] = a;
in[len_in][1] = b;
all += b;
}
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
int cal_3(int b, int a1, int a2, int a3)
{
int max, min;
max = MAX(a1, MAX(a2, a3));
min = MIN(a1, MIN(a2, a3));
max = abs(b - max);
min = abs(b - min);
return MAX(max, min);
}
int cal_5(int b, int a1, int a2, int a3, int a4, int a5)
{
int max, min;
max = MAX(a1, MAX(a2, MAX(a3, MAX(a4, a5))));
min = MIN(a1, MIN(a2, MIN(a3, MIN(a4, a5))));
max = abs(b - max);
min = abs(b - min);
return MAX(max, min);
}
int cal_8(int b, int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8)
{
int max, min;
max = MAX(a1, MAX(a2, MAX(a3, MAX(a4, MAX(a5, MAX(a6, MAX(a7, a8)))))));
min = MIN(a1, MIN(a2, MIN(a3, MIN(a4, MIN(a5, MIN(a6, MIN(a7, a8)))))));
max = abs(b - max);
min = abs(b - min);
return MAX(max, min);
}
void addPair(int v, int n)
{
if (out[len_out][0] == v)
out[len_out][1] += n;
else
{
len_out++;
out[len_out][0] = v;
out[len_out][1] = n;
}
}
#define POS_MOVE(p, n, nc) \
n -= nc; \
if (n == 0) \
{ \
p++; \
n = in[p][1]; \
}
void process()
{
int cur_value = 0;
int cur_num = 0;
int p1 = 0; // the previous line
int p2 = 0; // the current line
int p3 = 0; // the next line
int n1 = in[p1][1]; // next change in previous line
int n2 = 0; // next change in current line
int n3 = 0; // next change in next line
// calculate p1, p2, p3
int nu = 0;
if ((width == 1) || (all < 3 * width))
return;
while (1)
{
nu += in[p2][1];
if (nu <= width) // assume it should more than one line
p2++;
else
{
break;
}
}
n2 = nu - width;
nu = 0;
if (nu <= width * 2)
{
while (1)
{
nu += in[p3][1];
if (nu <= width * 2) // assume it should more than 2 lines
p3++;
else
{
break;
}
}
}
n3 = nu - width * 2;
D("find the p1 (%d, %d), p2 (%d, %d), p3 (%d, %d), \n",
p1, n1, p2, n2, p3, n3);
// do with [0, w)
int t1 = p1, tn1 = n1, t2 = p2, tn2 = n2;
cur_value = cal_3(in[t1][0], tn1 > 1 ? in[t1][0] : in[t1+1][0],
in[t2][0], tn2 > 1 ? in[t2][0] : in[t2+1][0]);
out[0][0] = cur_value;
cur_num = 0;
int i = 0;
while (i < width)
{
int nc = MIN(tn1, tn2);
nc = MIN(nc, width - i);
// begin
int v1 = 0, v2 = 0, v3 = 0;
if (i == 0)
{
v1 = cal_3(in[t1][0], tn1 > 1 ? in[t1][0] : in[t1+1][0],
in[t2][0], tn2 > 1 ? in[t2][0] : in[t2+1][0]);
}
else if (i == width - 1)
{
v1 = cal_3(in[t1][0], in[t1][1] > tn1 ? in[t1][0] : in[t1-1][0],
in[t2][0], in[t2][1] > tn2 ? in[t2][0] : in[t2-1][0]);
}
else
{
v1 = cal_5(in[t1][0],
in[t1][1] > tn1 ? in[t1][0] : in[t1-1][0], tn1 > 1 ? in[t1][0] : in[t1+1][0],
in[t2][0], in[t2][1] > tn2 ? in[t2][0] : in[t2-1][0], tn2 > 1 ? in[t2][0] : in[t2+1][0]);
}
addPair(v1, 1);
// mid
if (nc > 2)
{
v2 = abs(in[t1][0] - in[t2][0]);
addPair(v2, nc - 2);
}
// end
if (nc >= 2)
{
i += (nc - 1);
if (i == width - 1)
{
v3 = abs(in[t1][0] - in[t2][0]);
}
else
{
v3 = cal_5(in[t1][0],
in[t1][0], tn1 > nc ? in[t1][0] : in[t1+1][0],
in[t2][0], in[t2][0], tn2 > nc ? in[t2][0] : in[t2+1][0]);
}
addPair(v3, 1);
}
i++;
POS_MOVE(t1, tn1, nc);
POS_MOVE(t2, tn2, nc);
D("nc %d, next t1 (%d, %d), t2(%d, %d)\n", nc, t1, tn1, t2, tn2);
}
// calculate [w, len-w)
i = 0;
while (p3 <= len_in)
{
int nc = MIN(n1, MIN(n2, n3));
// begin
int v1 = 0, v2 = 0, v3 = 0;
i %= width;
if (i == 0)
{
v1 = cal_5(in[p2][0],
in[p1][0], n1 > 1 ? in[p1][0] : in[p1+1][0],
n2 > 1 ? in[p2][0] : in[p2+1][0],
in[p3][0], n3 > 1 ? in[p3][0] : in[p3+1][0]);
}
else if (i == (width - 1))
{
v1 = cal_5(in[p2][0],
in[p1][0], in[p1][1] > n1 ? in[p1][0] : in[p1-1][0],
in[p2][1] > n2 ? in[p2][0] : in[p2-1][0],
in[p3][0], in[p3][1] > n3 ? in[p3][0] : in[p3-1][0]);
}
else
{
v1 = cal_8(in[p2][0],
in[p1][0], in[p1][1] > n1 ? in[p1][0] : in[p1-1][0], n1 > 1 ? in[p1][0] : in[p1+1][0],
in[p2][1] > n2 ? in[p2][0] : in[p2-1][0], n2 > 1 ? in[p2][0] : in[p2+1][0],
in[p3][0], in[p3][1] > n3 ? in[p3][0] : in[p3-1][0], n3 > 1 ? in[p3][0] : in[p3+1][0]);
}
addPair(v1, 1);
// mid
if (nc > 2)
{
int tmp = abs(in[p1][0] - in[p2][0]);
v2 = abs(in[p3][0] - in[p2][0]);
v2 = MAX(tmp, v2);
addPair(v2, nc - 2);
}
// end
if (nc >= 2)
{
i += (nc - 1);
i %= width;
if (i == 0)
{
v3 = cal_5(in[p2][0],
in[p1][0], n1 > nc ? in[p1][0] : in[p1+1][0],
n2 > nc ? in[p2][0] : in[p2+1][0],
in[p3][0], n3 > nc ? in[p3][0] : in[p3+1][0]);
}
else if (i == (width - 1))
{
int tmp = abs(in[p1][0] - in[p2][0]);
v3 = abs(in[p3][0] - in[p2][0]);
v3 = MAX(tmp, v3);
}
else
{
v3 = cal_5(in[p2][0],
in[p1][0], n1 > nc ? in[p1][0] : in[p1+1][0],
n2 > nc ? in[p2][0] : in[p2+1][0],
in[p3][0], n3 > nc ? in[p3][0] : in[p3+1][0]);
}
addPair(v3, 1);
}
i++;
POS_MOVE(p1, n1, nc);
POS_MOVE(p2, n2, nc);
POS_MOVE(p3, n3, nc);
D("nc %d, next the p1 (%d, %d), p2 (%d, %d), p3 (%d, %d), \n",
nc, p1, n1, p2, n2, p3, n3);
}
// calculate [len - w, len)
i = 0;
while (p2 <= len_in)
{
int nc = MIN(n1, n2);
// begin
int v1 = 0, v2 = 0, v3 = 0;
if (i == 0)
{
v1 = cal_3(in[p2][0],
in[p1][0], n1 > 1 ? in[p1][0] : in[p1+1][0],
n2 > 1 ? in[p2][0] : in[p2+1][0]);
}
else if (i == (width - 1))
{
v1 = cal_3(in[p2][0],
in[p1][0], in[p1][1] > n1 ? in[p1][0] : in[p1-1][0],
in[p2][1] > n2 ? in[p2][0] : in[p2-1][0]);
}
else
{
v1 = cal_5(in[p2][0],
in[p1][0], in[p1][1] > n1 ? in[p1][0] : in[p1-1][0], n1 > 1 ? in[p1][0] : in[p1+1][0],
in[p2][1] > n2 ? in[p2][0] : in[p2-1][0], n2 > 1 ? in[p2][0] : in[p2+1][0]);
}
addPair(v1, 1);
// mid
if (nc > 2)
{
v2 = abs(in[p1][0] - in[p2][0]);
addPair(v2, nc - 2);
}
// end
if (nc >= 2)
{
i += (nc - 1);
if (i == (width - 1))
{
v3 = abs(in[p1][0] - in[p2][0]);
}
else
{
v3 = cal_3(in[p2][0],
in[p1][0], n1 > nc ? in[p1][0] : in[p1+1][0],
n2 > nc ? in[p2][0] : in[p2+1][0]);
}
addPair(v3, 1);
}
i++;
POS_MOVE(p1, n1, nc);
POS_MOVE(p2, n2, nc);
D("nc %d, next the p1 (%d, %d), p2 (%d, %d), p3 (%d, %d), \n",
nc, p1, n1, p2, n2, p3, n3);
}
}
void output()
{
printf("%d\n", width);
int i = 0;
for (i = 0; i <= len_out; i++)
{
printf("%d %d\n", out[i][0], out[i][1]);
}
printf("0 0\n");
}