申請SAE

如果您發現本博客的外觀很難看,那是因為部分外觀文件被中國.國家.防火.牆屏.蔽所致!
請翻~牆!

我的Wordpress博客的地址: http://zhuyf.tk/

2012年3月3日 星期六

USACO Feb12 Bronze Rope Folding 折繩子 題解


USACO 2012 February Contest, Bronze Division

Problem 1. Rope Folding




Problem 1: Rope Folding [Brian Dean, 2012] 
Farmer John has a long rope of length L (1 <= L <= 10,000) that he uses for various tasks around his farm. The rope has N knots tied into it at various distinct locations (1 <= N <= 100), including one knot at each of its two endpoints. 

FJ notices that there are certain locations at which he can fold the rope back on itself such that the knots on opposite strands all line up exactly with each-other:
  
Please help FJ count the number of folding points having this property. Folding exactly at a knot is allowed, except folding at one of the endpoints is not allowed, and extra knots on the longer side of a fold are not a problem (that is, knots only need to line up in the areas where there are two strands opposite each-other). FJ only considers making a single fold at a time; he fortunately never makes multiple folds. 

PROBLEM NAME: folding
INPUT FORMAT:
 * Line 1: Two space-separated integers, N and L.
 * Lines 2..1+N: Each line contains an integer in the range 0...L specifying the location of a single knot. Two of these lines will always be 0 and L. 



SAMPLE INPUT (file folding.in): 
5
10 

10 



INPUT DETAILS: 
The rope has length L=10, and there are 5 knots at positions 0, 2, 4, 6, and 10. 

OUTPUT FORMAT: 
* Line 1: The number of valid folding positions. 

SAMPLE OUTPUT (file folding.out):
 4
 OUTPUT DETAILS: 
The valid folding positions are 1, 2, 3, and 8.

【分析】
由題意可知,滿足要求的點只能在一個節上或者在兩個節的正中間,所以依次枚舉即可。

【我的代碼】
 裸代碼RawCode


C++语言: Codee#25718
01 /*
02 *PROB: folding
03 *LANG: C++
04 *ID: yeefan
05 */
06 #include <cstdio>
07 #include <cstdlib>
08 #include <cstring>
09 #include <algorithm>
10 using namespace std;
11
12 int N,L;
13 int Rope[101];
14
15 int Abs(int x)
16 {
17     return x>=0?x:-x;
18 }
19
20 void init()
21 {
22     scanf("%d %d\n",&N,&L);
23     for(int i=1;i<=N;i++)
24         scanf("%d\n",&Rope[i]);
25     sort(Rope+1,Rope+N+1);
26 }
27
28 void solve()
29 {
30     int Ans=0;
31     for(int i=2;i<=N-1;i++)
32     {
33         int head=i-1,tail=i+1;
34         bool flag=true;
35         while(head>=1 && tail<=N)
36         {
37             if(Rope[head]+Rope[tail]!=2*Rope[i])
38             {
39                 flag=false;
40                 break;
41             }
42             head--;
43             tail++;
44         }
45         if(flag)
46             Ans++;
47     }
48    
49     for(int i=1;i<=N-1;i++)
50     {
51         int ca=Rope[i],cb=Rope[i+1];
52        
53         int head=i,tail=i+1;
54         bool flag=true;
55         while(head>=1 && tail<=N)
56         {
57             if(Rope[head]+Rope[tail]!=ca+cb)
58             {
59                 flag=false;
60                 break;
61             }
62             head--;
63             tail++;
64         }
65         if(flag)
66                 Ans++;
67     }
68     printf("%d\n",Ans);
69 }
70
71 int main()
72 {
73     freopen("folding.in","r",stdin);
74     freopen("folding.out","w",stdout);
75     init();
76     solve();
77     return 0;
78 }

沒有留言:

張貼留言