2023/12/17“傳智杯”第四題求助
題的大致內(nèi)容是:
小紅拿到了一個(gè)元素是區(qū)間的空集合,如“(4,5)”“(8,40)”,在輸入一個(gè)n后,對這個(gè)集合進(jìn)行n次操作,她需要判斷每次操作后集合里是否存在相交的區(qū)間。是則輸出“Yes”,否則輸出“No”。
而操作方式只會有“+ a b”“- x y”,并且保證每次“- x y”的操作時(shí)一定存在(x,y)。
1<=n<=105
0<=a,b,x,y<=109
輸入樣例:
5 + 1 2 + 2 3 + 4 5 + 4 6 - 4 5
輸出結(jié)果:
No No No Yes No
以下是我當(dāng)時(shí)寫的代碼,但是一直有樣例跑不過去,也沒有提示超時(shí)了??????
#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; }
歡迎大佬批評指正,也希望可以看看大佬的代碼??
#懸賞#