utopia ioi报告(转)

http://acm.pku.edu.cn/JudgeOnline/problem?id=1148

以下转自北大报告

Definition – alternating sequence

A sequence of non-zero integers X=(xa, xa+1,…, xb,), a≤b is an alternating sequence if

  1) |xa|< |xa+1|< …<|xb|, and

  2) for all i, a<i≤b, the sign of xi is different from the sign of xi-1.

Here, |xa| is the absolute value of xa.

Lemma 1.

Let X=(xa, xa+1,…, xb) be an alternating sequence. The sign of xb is equal to the sign of ∑a≤i≤bxi , the total sum of elements in X

Theorem 1.

Let X=(xa, xa+1,…, xb), a≤b be an alternating sequence, and let S=(sa, sa+1,…, sb) , a≤b be a sequence of signs. If the sign of xb is equal to sb , then there exists a sequence X’=(xia, xia+1,…, xib) such that

xa, xa+1,…, xb} ={xia, xia+1,…, xib}, and

X’ is valid with respect to S.

The proof is by induction on the number of k of elements in X. When k=1, it is easy to see that X’=X is a valid sequence with respect to S. Now we assume that k≥2. We let S1=S-sb, that is, S1=(sa,sa+1,…,sb-1).

Case 1. The sign of sb-1 is equal to xb ,

  Let X1=X-xa, that is, X1=(xa+1,xa+2,…,xb).

Case 2. The sign of sb-1 is equal to xb-1 ,

  Let X1=X-xb, that is, X1=(xa,xa+1,…,xb-1)

1 Function Name: Utopia
2 Step 1. //read input
3   1.1 read N;
4 1.2 read 2N code numbers and partition them into A and B such that |A|=|B|;
5 1.3 read a sequence of regions R=(r1, r2, …,rN );
6
7 Step 2. //find x-coordinates of code pairs
8   2.1 find a sequence of signs S=(s1, s2, …,sN ) such that for all j , 1≤j≤N, sj=+if rj=1,4; otherwise sj=-’.
9 2.2 find an alternating sequence X=(x1, x2, …,xN ) from A such that the sign of xN is equal to sN .
10 2.3 given X and S , find a valid sequence X’=(xi1, xi2, …,xiN ) w.r.t S according to the proof of Theorem 1.
11
12 Step 3. //find y-coordinates of code pairs
13   3.1 find a sequence of signs S=(s1, s2, …,sN ) such that for all j , 1≤j≤N, sj=+if rj=1,2; otherwise sj=-’.
14 3.2 find an alternating sequence Y=(y1, y2, …,yN ) from B such that the sign of yN is equal to sN .
15 3.3 given Y and S , find a valid sequence Y’=(yi1, yi2, …,yiN ) w.r.t S according to the proof of Theorem 1.
16
17 Step 4. //write output
18   print (xi1,yi1),(xi2,yi2),…,(xiN,yiN).

 

Algorithm Utopia is correct, and its running time is O(NlogN).

The correctness of the algorithm is mainly due to Theorem 1. The complexity of each step except Step2.2 and 3.2 is O(N), where Step 2.2 and 3.2 require O(NlogN) time for sorting.

 

 

1 #include <stdio.h>
2 #include <stdlib.h>
3  #define MAXSIZE 10004
4
5  int cmp ( const void *a , const void *b )
6 {
7 return *(int *)a - *(int *)b;
8 }
9
10  void utopia(int *a, int *s, int len, int *out)
11 {
12 int i=0,p_al=0,p_ah=0,p_s=0,sign=0;
13
14 qsort( (void *)a, (size_t)len, sizeof(a[0]), cmp );
15
16 sign = s[len-1];
17 for (i=len-1; i>=0; i--)
18 {
19 a[i] *= sign;
20 sign *= -1;
21 }
22
23 p_al = 0;
24 p_ah = len-1;
25 p_s = len-2;
26 for (i = p_s; i>=0; i--)
27 {
28 if (s[i]*a[p_ah]>0)
29 out[i+1] = a[p_al++];
30 else
31 out[i+1] = a[p_ah--];
32 }
33 out[0] = a[p_ah];
34 }
35
36  int main ()
37 {
38 int i=0,len=0,sign=0;
39 int x[MAXSIZE],y[MAXSIZE],a[MAXSIZE],b[MAXSIZE],ao[MAXSIZE],bo[MAXSIZE];
40
41 scanf ("%d",&len);
42 for (i=0; i<len; i++)
43 scanf ("%d%d", &a[i],&b[i]);
44
45 for (i=0; i<len; i++)
46 {
47 scanf ("%d",&sign);
48 if (sign == 1)
49 x[i] = y[i] = 1;
50 else if (sign == 2)
51 x[i] = -1, y[i] = 1;
52 else if (sign == 3)
53 x[i] = y[i] = -1;
54 else
55 x[i] = 1, y[i] = -1;
56 }
57
58 utopia(a,x,len,ao);
59 utopia(b,y,len,bo);
60
61 for (i=0; i<len; i++)
62 {
63 if (ao[i]>0)
64 printf("+");
65 printf ("%d ",ao[i]);
66 if (bo[i]>0)
67 printf ("+");
68 printf ("%d\n",bo[i]);
69 }
70 return 0;
71 }

§Definition – alternating sequence

 

A sequence of non-zero integers X=(xa, xa+1,…, xb,), a≤b is an alternating sequence if

  1) |xa|< |xa+1|< …<|xb|, and

  2) for all i, a<i≤b, the sign of xi is different from the sign of xi-1.

Here, |xa| is the absolute value of xa.

原文地址:https://www.cnblogs.com/eavn/p/1752251.html