做了一夜 再也不想敲键盘了..等等再报告 贴出来先 (其实又没人看….)
没有什么任何优化 直接0msAC 真不好意思 XD
1 | #include <cstdio> |
2 | #include <cstring> |
3 | #include <cmath> |
4 | #define MAXNUM 2000 |
5 |
6 | typedef struct pair { |
7 | int v,l; |
8 | } Pair; |
9 |
10 | typedef struct pointer { |
11 | int p,num; |
12 | } Pointer; |
13 |
14 | Pair a[MAXNUM],r[MAXNUM]; |
15 | int width,prow,rp; |
16 | Pointer p2,p5,p8; |
17 |
18 | void calc(); |
19 | void set(Pointer *target, int p, int num); |
20 | void go( int spit, int steps); |
21 | void gop(Pointer *p, int steps); |
22 | int pre(Pointer p); |
23 | int next(Pointer p); |
24 | int spit();; |
25 |
26 | int main() { |
27 | int i; |
28 | while (1) { |
29 | rp=0; |
30 | scanf ( "%d" ,&width); |
31 | if (width==0) { |
32 | printf ( "0\n" ); |
33 | return 0; |
34 | } |
35 | for (i=1;;i++) { |
36 | scanf ( "%d%d" ,&a[i].v,&a[i].l); |
37 | if (!a[i].v&&!a[i].l) |
38 | break ; |
39 | } |
40 | a[0].v=a[i].v=-1; |
41 | a[0].l=a[i].l=width; |
42 | r[0].v=-1; |
43 | r[1].l=0; |
44 | calc(); |
45 | printf ( "%d\n" ,width); |
46 | for (i=1;;i++) { |
47 | printf ( "%d %d\n" ,r[i].v,r[i].l); |
48 | if (!r[i].v&&!r[i].l) |
49 | break ; |
50 | } |
51 | } |
52 | } |
53 |
54 | void calc() { |
55 | int i,counter; |
56 | int x,y,z,steps; |
57 | prow=1; |
58 | set(&p2,0,1); |
59 | set(&p5,1,1); |
60 | counter=0; |
61 | for (i=1;counter<=width;i++) |
62 | counter+=a[i].l; |
63 | i--; |
64 | set(&p8,i,width-counter+a[i].l+1); |
65 | while (1) { |
66 | if (a[p5.p].v==-1) |
67 | break ; |
68 | x=a[p2.p].l-p2.num; |
69 | y=a[p5.p].l-p5.num; |
70 | z=a[p8.p].l-p8.num; |
71 | steps=x>y?y:x; |
72 | steps=steps>z?z:steps; |
73 | if (steps<3) { |
74 | go(spit(),1); |
75 | continue ; |
76 | } //可在此处插入判断边界程序 |
77 | go(spit(),1); |
78 | go(spit(),steps-1); |
79 | } |
80 | rp++; |
81 | r[rp].v=r[rp].l=0; |
82 | } |
83 |
84 | void set(Pointer *target, int p, int num) { |
85 | target->p=p; |
86 | target->num=num; |
87 | } |
88 |
89 | int spit(){ |
90 | int result=0,i=0,temp,x=a[p5.p].v; |
91 | bool top= false ,bottom= false ; |
92 | if (a[p2.p].v==-1) |
93 | top= true ; |
94 | if (a[p8.p].v==-1) |
95 | bottom= true ; |
96 | if (prow!=1) { |
97 | if (!top) |
98 | result=(temp= fabs (pre(p2)-x))>result?temp:result; |
99 | result=(temp= fabs (pre(p5)-x))>result?temp:result; |
100 | if (!bottom) |
101 | result=(temp= fabs (pre(p8)-x))>result?temp:result; |
102 | } |
103 | if (prow!=width) { |
104 | if (!top) |
105 | result=(temp= fabs (next(p2)-x))>result?temp:result; |
106 | result=(temp= fabs (next(p5)-x))>result?temp:result; |
107 | if (!bottom) |
108 | result=(temp= fabs (next(p8)-x))>result?temp:result; |
109 | } |
110 | if (!top) |
111 | result=(temp= fabs (a[p2.p].v-x))>result?temp:result; |
112 | if (!bottom) |
113 | result=(temp= fabs (a[p8.p].v-x))>result?temp:result; |
114 | return result; |
115 | } |
116 |
117 | int pre(Pointer p) { |
118 | if (p.num==1) |
119 | return (a[p.p-1].v); |
120 | else |
121 | return (a[p.p].v); |
122 | } |
123 |
124 | int next(Pointer p) { |
125 | if (p.num==a[p.p].l) |
126 | return (a[p.p+1].v); |
127 | else |
128 | return (a[p.p].v); |
129 | } |
130 |
131 | void go( int spit, int steps) { |
132 | if (r[rp].v==spit) |
133 | r[rp].l+=steps; |
134 | else { |
135 | rp++; |
136 | r[rp].v=spit; |
137 | r[rp].l=steps; |
138 | } |
139 | gop(&p2,steps); |
140 | gop(&p5,steps); |
141 | gop(&p8,steps); |
142 | prow=(prow+steps-1)%width+1; |
143 | } |
144 |
145 | void gop(Pointer *p, int steps) { |
146 | p->num+=steps; |
147 | while (p->num>a[p->p].l) { |
148 | p->num-=a[p->p].l; |
149 | p->p++; |
150 | } |
151 | } |