标签:c++ 线段覆盖
/*
本算法的缺点 在于开的空间太大
分三类情况
线段
(-10,-1)在负区间
(-10,10)双区间
(1,10)正区间
一下给出正区间的代码,已考虑小数
思路是
绝对正区间,覆盖到数轴 sz[]数组上 小数部分 用sum1 累计
*/
//#include "header.h" //AnycodeX includes the header.h by default, needn‘t cancle the notation.
#include <bits/stdc++.h>
using namespace std;
#define max 1000 //数轴长度
int sz[2*max]; // 以max 位置为0 sz[0]为 -max 最后一个为max -1
#define n 10 //测试线段条数 坐标入下(a[],b[])
double a[n]={1,3,5,7,9,11,13,15,17,19};
double b[n]={2,4,6,8,10,12,14,16,18,20};
double funadd(int s,int t)
{
for(int i=s;i<=t;i++)
{
sz[i]=1;
}
}
double fun()
//数轴上置1 小数部分 用sum1 累计 sum2为整数部分求和
{ double sum=0;double sum1=0;double sum2=0;
int s=0; int t=0;
for(int i=0;i<n;i++)
{
s=ceil(a[i]);t=floor(b[i]);
sum1+=s-a[i];sum1+=b[i]-t;
funadd(a[i],b[i]);
}
for(int i=0;i<2*max;i++)
{
if(sz[i]==1)sum2+=1;
}
sum=sum1+sum2;
return sum;
}
int main()
{
cout<<fun()<<endl;
cout << "Hello,C++ world of AnycodeX!" << endl;
return 0;
}标签:c++ 线段覆盖
原文地址:http://wzsts.blog.51cto.com/10251779/1837086