zoukankan      html  css  js  c++  java
  • Segments CodeForces 909B (找规律)

    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
    ",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
    ",ans);
    25     return 0;
    26 }
  • 相关阅读:
    05-流程控制
    04-基础语法3
    03-基础语法2
    SQLyog-证书密钥
    博客园美化系列__看板娘
    修改博客园模板样式【完美解决方案】
    Linux-Deepin 下开启SSH远程登陆
    Mui---自己利用Vue编写的表格
    canvas--操作步骤--属性
    Mui-打开手机相册
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9519958.html
Copyright © 2011-2022 走看看