点头(1163)
题目链接:here~~~
本题巧妙运用并查集记录每个点的前驱,更快查找出来目标状态(而且排序更加巧妙)代码某位大牛所写ORZ
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; pair<int,int> p[50005]; int father[50005]; int find(int x){ if(x <= 0) return -1; return father[x] = (x == father[x])? x - 1 : find(father[x]); } int main() { int n; cin >> n; for(int i = 0;i < n; i++){ cin>>p[i].second>>p[i].first; p[i].first = - p[i].first; if(p[i].second > n) p[i].second = n; } for(int i = 0;i <= n;i++) father[i] = i; sort(p,p+n); long long ans = 0; for(int i = 0;i < n;i ++) { int t = find(p[i].second); if(t >= 0) ans -= p[i].first; } cout<<ans<<endl; }