Gym

来源:互联网 发布:js操作input file 编辑:程序博客网 时间:2024/06/03 21:13

直接模拟?

训练的时候看错题,,,wa 了全场


思路: 用结构体存每个区间的左值和右值,对于不合法的区间不进行保存


如果最后合法的区间没有,就是 false

否则  合并后如果只有一个区间且覆盖全部 就是 true

在否则 就把多个区间输出

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <cmath>#include <set>#include <map>#include <stack>#include <queue>#include <ctype.h>#include <vector>#include <algorithm>#include <sstream>#define PI acos(-1.0)#define in freopen("in.txt", "r", stdin)#define out freopen("out.txt", "w", stdout)using namespace std;const int maxn = 1000 + 7, INF = 0x3f3f3f3f, mod = 1e9 + 7;string s, t;int n, v, id;int min_ = -32768, max_ = 32767;struct node {    int L, R;    node(){ L = min_; R = max_;}    node(int l_ , int r_):L(l_), R(r_) {}    bool operator <(const node &b) const{        if(L == b.L) return R > b.R;        return L < b.L;    }}a[maxn];set<node> st;void init() {    st.clear();    id = 0;    while(1) {        getline(cin, s);        stringstream ss(s);        int f = 0, lv = min_, rv = max_;        while(ss >> t) {            if(t == ">=") {                ss >> v;                lv = v;            }            else if(t == "<=") {                ss >> v;                rv = v;            }            else if(t == "||") {                f = 1;            }        }        if(lv <= rv && lv <= max_ && rv >= min_) {            a[id].L = max(a[id].L, lv);            a[id].R = min(a[id].R, rv);            id++;        }        if(f == 0) break;    }    n = id;  //  true number    //cout << n << "    +++++    " << endl;}void solve() {    if(n == 0) {        cout << "false" << endl;        return ;    }    sort(a, a+n);    int l_ = a[0].L, r_ = a[0].R;    for(int i = 1; i < n; ++i) {        if(a[i].L > r_+1) {            st.insert(node(l_, r_));            l_ = a[i].L, r_ = a[i].R;        }        else {            r_ = max(r_, a[i].R);        }    }    st.insert(node(l_, r_));    if(l_ == min_ && r_ == max_) {        cout << "true" << endl;        return ;    }    else {        int cnt = 0;        set<node>::iterator it;        for(it = st.begin(); it != st.end(); ++it) {            node tt = *it;            cnt++;            if(tt.L == -32768) {                cout << "x <= " << tt.R ;            }            else if(tt.R == 32767) {                cout << "x >= " << tt.L << endl;                return ;            }            else {                cout << "x >= " << tt.L << " && x <= " << tt.R ;            }            if(cnt != st.size()) {                cout << " ||" << endl;            }            else break;        }    }}int main() {    freopen("hard.in", "r", stdin);    freopen("hard.out", "w", stdout);    ios::sync_with_stdio(false);    init();    solve();    return 0;}