2023/12/17“传智杯”第四题求助
题的大致内容是:
小红拿到了一个元素是区间的空集合,如“(4,5)”“(8,40)”,在输入一个n后,对这个集合进行n次操作,她需要判断每次操作后集合里是否存在相交的区间。是则输出“Yes”,否则输出“No”。
而操作方式只会有“+ a b”“- x y”,并且保证每次“- x y”的操作时一定存在(x,y)。
1<=n<=105
0<=a,b,x,y<=109
输入样例:
5 + 1 2 + 2 3 + 4 5 + 4 6 - 4 5
输出结果:
No No No Yes No
以下是我当时写的代码,但是一直有样例跑不过去,也没有提示超时了😭😭😭
#include <cstdio> #include <iostream> #include<vector> #include<algorithm> using namespace std; struct tmp { int left; int right; }; void find(int a, int b, vector<tmp>& arr) { for (vector<tmp>::iterator it = arr.begin(); it != arr.end(); it++) { if ((*it).left == a && (*it).right == b) { arr.erase(it); return; } } } void judg(vector<tmp>& arr) { vector<tmp>::iterator it = arr.begin(); int flag = (*it).right; for (it++; it != arr.end(); it++) { if ((*it).left < flag) { cout << "Yes" << endl; return; } flag = (*it).right > flag ? (*it).right : flag; } cout << "No" << endl; return; } class mysort { public: bool operator()(tmp a, tmp b)const { return a.left < b.left; } }; int main() { vector<tmp>arr; int num = 0; cin >> num; if (num == 1) { cout << "No" << endl; return 0; } char op = 0; int numb1; int numb2; tmp temp; for (int n = 0; n < num; n++) { cin >> op; cin >> numb1; cin >> numb2; if (op == '+') { temp.left = numb1; temp.right = numb2; arr.push_back(temp); } else { find(numb1, numb2, arr); } sort(arr.begin(), arr.end(), mysort()); judg(arr); } return 0; }
欢迎大佬批评指正,也希望可以看看大佬的代码🤩
#悬赏#