1. 程式人生 > >Segments CodeForces 909B (找規律)

Segments CodeForces 909B (找規律)

arr ssi wid mov single 分享圖片 要求 num 最優

Description

You are given an integer N. Consider all possible segments (線段,劃分)on the coordinate axis with endpoints at integer points with coordinates between 0 and N, inclusive; there will be 技術分享圖片 of them.

You want to draw these segments in several layers so that in each layer the segments don‘t overlap(重疊)

(they might touch at the endpoints though). You can not move the segments to a different location on the coordinate axis.

Find the minimal number of layers(層次) you have to use for the given N.

Input

The only input line contains a single integer N (1 ≤ N ≤ 100).

Output

Output a single integer - the minimal number of layers required to draw the segments for the given N.

Sample Input

Input
2
Output
2
Input
3
Output
4
Input
4
Output
6

Hint

As an example, here are the segments and their optimal arrangement(最優排列)

into layers for N = 4.

技術分享圖片

題目意思;給你一條n長的線段,我們可以將其劃分為n*(n+1)/2個子線段。現在要求子線段在坐標軸上的位置不動,將所有的子串進行壓縮,不能出現重疊,問最少能夠得到幾層原n長的線段。 解題思路:當時是我隊友搞的這道題,我一向對找規律的題目很頭疼,他就是一一枚舉發現的規律。
 1 #include <stdio.h>
 2 using namespace std;
 3 int main()
 4 {
 5     int n,k,i,a[101];
 6     a[1]=1;
 7     a[2]=2;
 8     a[3]=4;
 9     for(i=4;i<=100;i++)
10     {
11         k=i*(i+1)/2;///總的子線段數
12         a[i]=k-a[i-1];
13     }
14     while(~scanf("%d",&n))
15     {
16         printf("%d\n",a[n]);
17     }
18     return 0;
19 }

但其實也可以這樣想,最後得到的層次數是原來長度為n的線段的若幹條可能還會有部分,又因為每一層的線段都是子串在坐標軸上不動拼接得來的,那麽我們將線段分成一個個的坐標上的點,那麽出現次數最多的那個點的次數就是層次數!!!

 1 #include<stdio.h>
 2 #include<algorithm>
 3 using namespace std;
 4 int vis[110];
 5 int main()
 6 {
 7     int n,j,i,k,ans;
 8     ans=0;
 9     scanf("%d",&n);
10     for(i=1; i<=n; i++)
11     {
12         for(j=i; j<=n; j++)///劃分子段
13         {
14             for(k=i; k<=j; k++)///將子段拆成一個個的點
15             {
16                 vis[k]++;
17             }
18         }
19     }
20     for(i=1; i<=110; i++)
21     {
22         ans=max(ans,vis[i]);
23     }
24     printf("%d\n",ans);
25     return 0;
26 }

Segments CodeForces 909B (找規律)