博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Segments CodeForces 909B (找规律)
阅读量:4598 次
发布时间:2019-06-09

本文共 1978 字,大约阅读时间需要 6 分钟。

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 
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
2 #include
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 }

 

转载于:https://www.cnblogs.com/wkfvawl/p/9519958.html

你可能感兴趣的文章
Flink - state管理
查看>>
Apache Kafka - KIP-42: Add Producer and Consumer Interceptors
查看>>
ArcGIS JS Demo
查看>>
webservice发布问题,部署iis后调用不成功
查看>>
Koch 分形,海岸线,雪花
查看>>
ubuntu系统下Python虚拟环境的安装和使用
查看>>
IOS7开发~新UI学起(二)
查看>>
软件过程度量和CMMI模型概述
查看>>
数据结构(DataStructure)与算法(Algorithm)、STL应用
查看>>
Linux与Windows xp操作系统启动过程
查看>>
linux运维、架构之路-Kubernetes1.13离线集群部署双向认证
查看>>
[Leetcode]Substring with Concatenation of All Words
查看>>
Gem install rmagick 报错问题~
查看>>
验证一个方法触发时机
查看>>
25句充满正能量的句子
查看>>
python学习手册笔记——27.更多实例
查看>>
Spring Cloud Alibaba 新版本发布:众多期待内容整合打包加入!
查看>>
Android Camera 使用小结
查看>>
20170908 校内模拟赛 游戏
查看>>
P1774 最接近神的人_NOI导刊2010提高(02)
查看>>