POJ 2808 校门外的树 解题报告
POJ 2808 校门外的树 解题报告
编号:2808
考查点:简单计算题
思路:自己是按照维护一个区域集合,保证这些区域彼此不相交,到最后再减去这个空间实现的,后来看了书才发现,用穷举法特别简单,我太嫩了..
提交情况:比较走运,很快就AC了,我还以为特别难呢,估计是昨天思考的比较清楚的原因,不过代码还是远远比不上穷举..
Source Code:
1 //POJ Grids 2808
2 #include <iostream>
3 using namespace std;
4
5 int main()
6 {
7 int len;
8 cin>>len;
9 len++;
10 int count;
11 cin>>count;
12 int low[100]={0},high[100]={0};
13 for (int i=0;i<count;i++)
14 {
15 int n,m;
16 cin>>n>>m;
17 for (int j=0;j<i;j++)
18 {
19 if (low[j]||high[j])
20 {
21 if (n>=low[j]&&m<=high[j])
22 {
23 n = m = 0;
24 break;
25 }
26 if (n>=high[j])
27 {
28 if (n==high[j])
29 {
30 n++;
31 }
32 continue;
33 }
34 if (m<=low[j])
35 {
36 if (m==low[j])
37 {
38 m--;
39 }
40 continue;
41 }
42 if (n<=low[j]&&m>=high[j])
43 {
44 low[j] = 0;
45 high[j] = 0;
46 continue;
47 }
48 if (n<low[j]&&m<=high[j])
49 {
50 m = high[j];
51 low[j]=high[j]=0;
52 continue;
53 }
54 if (n>=low[j]&&m>high[j])
55 {
56 n = low[j];
57 low[j] = high[j] = 0;
58 continue;
59 }
60 }
61 }
62 low[i] = n;
63 high[i] = m;
64 }
65 for (int i=0;i<count;i++)
66 {
67 if (low[i]||high[i])
68 {
69 len -= high[i]-low[i]+1;
70 }
71 }
72 cout<<len<<endl;
73 return 0;
74 }
75
76
2 #include <iostream>
3 using namespace std;
4
5 int main()
6 {
7 int len;
8 cin>>len;
9 len++;
10 int count;
11 cin>>count;
12 int low[100]={0},high[100]={0};
13 for (int i=0;i<count;i++)
14 {
15 int n,m;
16 cin>>n>>m;
17 for (int j=0;j<i;j++)
18 {
19 if (low[j]||high[j])
20 {
21 if (n>=low[j]&&m<=high[j])
22 {
23 n = m = 0;
24 break;
25 }
26 if (n>=high[j])
27 {
28 if (n==high[j])
29 {
30 n++;
31 }
32 continue;
33 }
34 if (m<=low[j])
35 {
36 if (m==low[j])
37 {
38 m--;
39 }
40 continue;
41 }
42 if (n<=low[j]&&m>=high[j])
43 {
44 low[j] = 0;
45 high[j] = 0;
46 continue;
47 }
48 if (n<low[j]&&m<=high[j])
49 {
50 m = high[j];
51 low[j]=high[j]=0;
52 continue;
53 }
54 if (n>=low[j]&&m>high[j])
55 {
56 n = low[j];
57 low[j] = high[j] = 0;
58 continue;
59 }
60 }
61 }
62 low[i] = n;
63 high[i] = m;
64 }
65 for (int i=0;i<count;i++)
66 {
67 if (low[i]||high[i])
68 {
69 len -= high[i]-low[i]+1;
70 }
71 }
72 cout<<len<<endl;
73 return 0;
74 }
75
76
总结:贴的是我的代码,穷举的代码忘了扔哪了,那个要好些,发现有时候还是要考虑用空间换时间..。
By Ns517