Gym 101190H Hard Refactoring

来源:互联网 发布:手机淘宝等级查询 编辑:程序博客网 时间:2024/06/05 16:29

题意:

给你一些x的区间范围,有规定的输入格式,如果在(-32768 ~ 32767)范围之间的任意整数都能在x的这些区间中,则输出 “true”,如果这些范围中的部分数在x的这些区间中,则输出并合并这些区间,如果都不在,则输出“false”;

暴力。这道题注意的细节太多,wa了太多次才发现其中的坑, 注意  x >= 5 && x <= 32769 的输出是 x >= 5;   

还有 

x >= 1 && x <=0 ||

x >= 5 && x <= 8

这组输出是 x >= 5 && x <= 8, (遇到一个错误区间,跳过就好)

代码:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <string>#include <queue>#include <stack>#include <map>#include <cmath>using namespace std;const int INF = 0x3f3f3f3f;struct Interval{    int L, R;    bool operator < (const Interval &t) const    {        if (L != t.L) return L < t.L;        return R < t.R;    }} interval[1010], ans[1010];int n, vis[1010];int main(){    freopen("hard.in", "r", stdin);    freopen("hard.out", "w", stdout);    n = 0;    memset(vis, 0, sizeof(vis));    bool ok = true;    char c;    string s;    while (cin >> c)    {        cin >> s;        if (s[0] == '>')        {            cin >> interval[n].L >> c;            if (c == '|')            {                interval[n++].R = INF;                cin >> c;            }            else if (c == '&')            {                cin >> c >> c >> s >> interval[n].R;                n++;            }            else            {                interval[n++].R = INF;                break;            }        }        else if (s[0] == '<')        {            interval[n].L = -INF;            cin >> interval[n++].R;            cin >> c;            if (c != '|') break;            cin >> c;        }    }    sort(interval, interval + n);    int cnt = 0;    for (int i = 0; i < n; i++)    {        if (vis[i]) continue;        vis[i] = 1;        ans[cnt].R = interval[i].R;        ans[cnt].L = interval[i].L;        for (int j = i + 1; j < n; j++)        {            if (vis[j]) continue;            if (ans[cnt].R + 1 < interval[j].L) break;            ans[cnt].R = max(ans[cnt].R, interval[j].R);            vis[j] = 1;        }        cnt++;    }    if(cnt == 1)    {        if(ans[0].L > ans[0].R)        {            cout << "false" << endl;            return 0;        }        if (ans[0].L <= -32768 && ans[0].R >= 32767)        {            cout << "true" << endl;            return 0;        }        else if (ans[0].R < -32768 || ans[0].L > 32767)        {            cout << "false" << endl;            return 0;        }        else        {            if (ans[0].L <= -32768)      cout << "x <= " << ans[0].R << endl;            else if (ans[0].R >= 32767)  cout << "x >= " << ans[0].L << endl;            else   cout << "x >= " << ans[0].L << " && x <= " << ans[0].R << endl;            return 0;        }    }    bool flag = true;    for (int i = 0; i < cnt; i++)    {        if(ans[i].L > ans[i].R) continue;        if (ans[i].L <= -32768)        {            if(ans[i].R >= -32768 && flag)            {                cout << "x <= " << ans[i].R;                flag = false;            }            else if(ans[i].R >= -32768 && !flag) cout << " ||" << endl << "x <= " << ans[i].R;        }        else if (ans[i].R >= 32767)        {            if(ans[i].L <= 32767 && flag)            {                cout << "x >= " << ans[i].L;                flag = false;            }            else if(ans[i].L <= 32767 && !flag) cout << " ||" << endl << "x >= " << ans[i].L;        }        else        {            if((ans[i].L <= 32767 || ans[i].R >= -32768) && flag)            {                cout << "x >= " << ans[i].L << " && x <= " << ans[i].R;                flag = false;            }            else if((ans[i].L <= 32767 || ans[i].R >= -32768) && !flag)                cout << " ||" << endl << "x >= " << ans[i].L << " && x <= " << ans[i].R;        }    }    if(flag) cout << "false" << endl;    return 0;}


原创粉丝点击