USACO 3.1 Shaping Regions

货真价实的难题,必须权衡有限的的空间和时间

TASK: rect1
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3084 KB]
   Test 2: TEST OK [0.000 secs, 3084 KB]
   Test 3: TEST OK [0.000 secs, 3084 KB]
   Test 4: TEST OK [0.000 secs, 3084 KB]
   Test 5: TEST OK [0.000 secs, 3084 KB]
   Test 6: TEST OK [0.000 secs, 3084 KB]
   Test 7: TEST OK [0.000 secs, 3084 KB]
   Test 8: TEST OK [0.000 secs, 3084 KB]
   Test 9: TEST OK [0.000 secs, 3084 KB]
   Test 10: TEST OK [0.000 secs, 3084 KB]
   Test 11: TEST OK [0.000 secs, 3084 KB]

1 /*
2 PROG: rect1
3 ID: jiafeim1
4 LANG: C++
5  */
6 #include <algorithm>
7 #include <iostream>
8 #include <fstream>
9
10 using namespace std;
11 int a,b,n;
12
13 #define maxN(x,y) ((x)>(y)?(x):(y))
14 #define minN(x,y) ((x)<(y)?(x):(y))
15
16 struct res
17 {
18 int xl;
19 int yl;
20 int xr;
21 int yr;
22 int color;
23 };
24 res recs[2505];
25
26 int ca[2505]={0};
27 int cur_color;
28 void work(int xl,int yl,int xr,int yr,int num)
29 {
30 if(num >= n)
31 {
32 ca[cur_color]+=(xr-xl)*(yr-yl);
33 return;
34 }
35 if(xl>=recs[num].xr || xr<=recs[num].xl || yl>=recs[num].yr || yr<=recs[num].yl)
36
37 {work(xl,yl,xr,yr,num+1);return;}
38 if(yr>recs[num].yr)
39 {
40 work(xl,recs[num].yr,xr,yr,num+1);
41 }
42 if(yl<recs[num].yl)
43 {
44 work(xl,yl,xr,recs[num].yl,num+1);
45 }
46 if(xl<recs[num].xl)
47 {
48 work(xl,maxN(yl,recs[num].yl),recs[num].xl,minN(yr,recs[num].yr),num+1);
49 }
50 if(xr>recs[num].xr)
51 {
52 work(recs[num].xr,maxN(yl,recs[num].yl),xr,minN(yr,recs[num].yr),num+1);
53 }
54 }
55 int main()
56 {
57 std::ifstream fin("rect1.in");
58 std::ofstream fout("rect1.out");
59
60 fin>>a>>b>>n;
61
62 ++n;
63
64 recs[0].xl=0;
65 recs[0].yl=0;
66 recs[0].xr=a;
67 recs[0].yr=b;
68 recs[0].color=1;
69 int max_color = 1;
70 for(int i=1;i!=n;++i)
71 {
72 fin>>recs[i].xl>>recs[i].yl>>recs[i].xr>>recs[i].yr>>recs[i].color;
73 max_color = maxN(recs[i].color,max_color);
74 }
75
76 for(int i=0;i!=n;++i)
77 {
78 cur_color = recs[i].color;
79 work(recs[i].xl,recs[i].yl,recs[i].xr,recs[i].yr,i+1);
80 }
81
82 for(int i=1;i<=max_color;++i)
83 {
84 if(ca[i]!=0)
85 fout<<i<<" "<<ca[i]<<endl;
86 }
87
88
89
90 fin.close();
91 fout.close();
92 return 0;
93 }


All tests OK.

posted @ 2011-05-12 20:34  幻魇  阅读(277)  评论(0编辑  收藏  举报