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
Sample Output
Hint
As an example, here are the segments and their optimal arrangement into layers for N = 4.
题解:给你长度为n的线段,有n*(1+n)/2个子线段,将他们压缩,使压缩后线段最少,就是找规律,之前做过一次,昨天做的时候忘了,又现推的,分奇偶讨论,偶数为偶数递增,奇数为奇数递增。
代码如下:
#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <ctime>
#define maxn 10007
#define N 107
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.000000001
#define mod 1000000007
using namespace std;
typedef long long ll;
int main()
{
int n;
cin>>n;
if(n%2==0)
{
int sum=0;
for(int i=2;i<=n;i+=2)
sum+=i;
cout<<sum<<endl;
}
else
{
int sum=0;
for(int i=1;i<=n;i+=2)
sum+=i;
cout<<sum<<endl;
}
return 0;
}