In Love
题目连结
题目翻译
输入t次(执行t次)
当输入+lr及 ==>增加一组集合进入空间[l,r]
当输入-lr及 ==>删除一组在空间中的集合[l,r] (保证存在于空间)
试问是否能找到两组集合不相交
另如[1,2]与[3,4]不相交
但[1,2]与[2,2]相交(于2)
P.S L<=R !!!
解题
贪心演算法因为l恆<=r ==>所以只要在R的最小值<L的最大值时表示一定能找到至少一组不相交的两条线
举例 当 R(min)=2 < L(max)=3 时
必至少有一组R=(1,2)或(2,2) ; L=(3,3)或(3,>3)
code
#include <iostream>#include <vector>#include <map>#include <algorithm>#include <set>#define nmsl INT_MAXusing namespace std;/*//* 题目:In Love from 解题日期:2023/12/08 解题者:神里绫华的狗(dasabi7414) 题目连结:https://codeforces.com/contest/1883/my //* */// 建立一个set ,可以在值进入时就排序multiset<int, less<int>> L, R;int main(int argc, char const *argv[]){ int t; cin >> t; while (t--) { char c; int r, l; cin >> c; cin >> l >> r; // 当使用这输入 '+'>>l>>r 时插入multiset(因为有less<int>所以会排序==>由小到大) if (c == '+') { L.insert(l); R.insert(r); } // 当使用者输入 '-'>>l>>r 时找出l,r之位置并删除(使用find) else if (c == '-') { L.erase(L.find(l)); R.erase(R.find(r)); } // 当只有一组时(l,r各一个)==>输出no if (L.size() <= 1) { cout << "NO" << endl; } // 使用指标(因为end只的是空间故要再少1)找到位置并己较其内的值 else if (*R.begin() < *(--L.end())) { cout << "YES" << endl; } else { cout << "NO" << endl; } /* 我连测试时用的输出都留下来.... for (auto i : v) { cout << i << " "; } cout << endl; */ } return 0;}