题目链接:https://vjudge.net/problem/Gym-101190H
题意:简单的区间合并,输出合并后的区间,如果合并后区间为空集输出false,若为(-inf, +inf)则输出true,否则输出合并后的区间范围。
思路:模拟即可,队友写的代码。
代码如下:
#include<iostream>
#include<sstream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#include<list>
#include<map>
#include<set>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a,b,c) for(int (a)=(b);(a)<=(c);(a)++)
#define drep(a,b,c) for(int (a)=(b);(a)=>(c);(a)--)
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 10000+10;
const int min_=-32768;
const int max_=32767;
struct Line
{
int l,r;
Line(int ll=min_,int rr=max_):l(ll),r(rr) {}
bool operator<(const Line& a)
{
if(l==a.l) return r<a.r;
return l<a.l;
}
}line[maxn];
struct Cun
{
int l,r;
Cun(int ll=min_,int rr=max_):l(ll),r(rr) {}
}cun[maxn];
int main()
{
freopen("hard.in", "r", stdin);
freopen("hard.out", "w", stdout);
string s;
int ok1,ok2;
int kai=1;
int sum1=0,sum2=0;
int cnt=0;
int total=0;
while(getline(cin,s))
{
int sz=s.size();
ok1=ok2=0;
sum1=sum2=0;
kai=1;
for(int i=0;i<sz;i++)
{
if(s[i]=='>'&&s[i+1]=='=') ok1=1;
else if(s[i]=='<'&&s[i+1]=='=') ok2=1;
}
if(ok1==1&&ok2==1)
{
int sign1=1,sign2=1;
for(int i=0;i<sz;i++)
{
if(s[i]>='0'&&s[i]<='9'&&kai==1)
{
sum1=sum1*10+s[i]-'0';
}
else if(s[i]>='0'&&s[i]<='9'&&kai==2)
{
sum2=sum2*10+s[i]-'0';
}
else if(s[i]=='&') kai=2;
else if(s[i]=='-'&&kai==1) sign1=-1;
else if(s[i]=='-'&&kai==2) sign2=-1;
}
sum1*=sign1;
sum2*=sign2;
if(sum1>sum2) continue;
else if(sum1<=sum2)
{
line[cnt].l=sum1;
line[cnt].r=sum2;
cnt++;
}
}
else if(ok1==1)
{
int flag=1;
for(int i=0;i<sz;i++)
{
if(s[i]>='0'&&s[i]<='9')
{
sum1=sum1*10+s[i]-'0';
}
if(s[i]=='-') flag=-1;
}
int t=flag*sum1;
if(t>max_) continue;
else
{
line[cnt].l=t;
cnt++;
}
}
else if(ok2==1)
{
int flag=1;
for(int i=0;i<sz;i++)
{
if(s[i]>='0'&&s[i]<='9')
{
sum2=sum2*10+s[i]-'0';
}
if(s[i]=='-') flag=-1;
}
int t=flag*sum2;
if(t<min_) continue;
else
{
line[cnt].r=t;
cnt++;
}
}
}
sort(line,line+cnt);
int ll,rr;
ll=line[0].l,rr=line[0].r;
for(int i=1;i<cnt;i++)
{
if(line[i].l<=rr) rr=max(rr,line[i].r);
else if(line[i].l==rr+1) rr=line[i].r;
else if(line[i].l>rr+1)
{
cun[total].l=ll;
cun[total].r=rr;
total++;
ll=line[i].l;
rr=line[i].r;
}
}
cun[total].l=ll;
cun[total].r=rr;
total++;
if(cnt==0||(cnt!=0&&total==0)) printf("false\n");
else if(cun[0].l==min_&&cun[0].r==max_) printf("true\n");
else
{
for(int i=0;i<total;i++)
{
if(i!=total-1)
{
if(cun[i].l==min_) printf("x <= %d ||\n",cun[i].r);
else if(cun[i].r==max_&&i==total-1) printf("x >= %d ||\n",cun[i].l);
else printf("x >= %d && x <= %d ||\n",cun[i].l,cun[i].r);
}
else
{
if(cun[i].l==min_) printf("x <= %d\n",cun[i].r);
else if(cun[i].r==max_&&i==total-1) printf("x >= %d\n",cun[i].l);
else printf("x >= %d && x <= %d\n",cun[i].l,cun[i].r);
}
}
}
return 0;
}