滴滴笔试9.17
第一题最小数字,忘记回溯了73%
第二题栅栏,用mapA了
#include <iostream> #include <map> #include <vector> using namespace std; map<int, int> m1, m2, m3; int a[100100], b[100100]; int main() { int n, p, q, x; scanf("%d%d%d",&n, &p, &q); for(int i = 0; i < n; ++i) scanf("%d", &a[i]); for(int i = 0; i < n; ++i) scanf("%d", &b[i]); for(int i = 0; i < n; ++i){ scanf("%d", &x); if(x == 1) m1[a[i]]++, m1[b[i] + 1]--; else m2[a[i]]++, m2[b[i] + 1]--; } int sum = 0, f = 0; for(auto it = m1.begin(); it != m1.end(); ++it) { sum += it->second; if(sum >= p && f == 0) { m3[it->first]++; f = 1; } else if(sum < p && f == 1) { m3[it->first]--; f = 0; } } if(f) m3[m1.end()->first]--; sum = 0, f = 0; for(auto it = m2.begin(); it != m2.end(); ++it) { sum += it->second; if(sum >= q && f == 0) { m3[it->first]++; f = 1; } else if(sum < q && f == 1) { m3[it->first]--; f = 0; } } if(f) m3[m2.end()->first]--; sum = 0, f = 0; int ans = 0, l, r; for(auto it = m3.begin(); it != m3.end(); ++it) { sum += it->second; if(sum >= 2 && f == 0) { l = it->first; f = 1; } else if(sum < 2 && f == 1) { ans += it->first - l; f = 0; } } if(f) ans += m3.end()->first - l; printf("%d\n", ans); }