Find the hotel HDU - 3193
阿新 • • 發佈:2021-07-03
原題連結
考察:排序,字首和思想
錯誤思路:
建立d,p的樹狀陣列,對於每一個查詢是否有 <\(d[i]\)&&<\(p[i]\)的
錯誤原因:
顯然p,d是一體的不能分開.
正確思路:
結構體排序按p,d優先順序排序,對於每一個\(p[i]\),查詢\(<p[i]\)的最小\(d[i]\),如果\(>=d[i]\)則當前旅館計入答案
計入最小值直接用字首的\(f\)陣列,根本沒必要用RMQ
Code
#include <iostream> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef pair<int, int> PII; const int N = 10010; struct cmp{ bool operator()(PII x,PII y){ if(x.first==y.first) return x.second < y.second; return x.first < y.first; } }; PII p[N],ans[N]; int n,f[N],cnt; void init() { memset(f, 0x3f, sizeof f); for (int i = 1; i <= n;i++) f[i] = min(f[i - 1], p[i].second); } int main() { while(scanf("%d",&n)!=EOF) { cnt = 0; for (int i = 1; i <= n;i++) scanf("%d%d", &p[i].first, &p[i].second); sort(p + 1, p + n + 1); init(); for (int i = 1,j=1; i <= n;i++) { if(p[j].first!=p[i].first) j = i; if(f[j-1]>=p[i].second) ans[++cnt] = p[i]; } printf("%d\n", cnt); for (int i = 1; i <= cnt;i++) printf("%d %d\n", ans[i].first, ans[i].second); } return 0; }