标签:clear nbsp hdu game const string span fine size
在多个连续的区间段中,选出连续重复度最高的区间,这样连续选出多个重复度最高的不相交区间,然后从第一个区间的左边已经右边开始,连续贪心即可,答案取最小值
#include<iostream> #include<string.h> #include<algorithm> #include<stdio.h> #include<vector> #define pii pair<int,int> #define pb push_back #define mp make_pair #define LL long long using namespace std; vector<pii>v; const int maxx = 1005; int a[maxx]; int b[maxx]; int n; LL go(int x){ LL ans=0; for(int i=1;i<v.size();i++){ if (v[i].first>x){ ans+=(v[i].first-x+1)/2; if (i<v.size()-1){ if ((v[i].first-x)%2){ ///长度为奇数 if (v[i+1].first>v[i].second && v[i].second-v[i].first>0){ x=v[i].first+1; }else x=v[i].first; }else x=v[i].first; }else x=v[i].first; }else { ans+=(x-v[i].second+1)/2; if (i<v.size()-1){ if ((x-v[i].second)%2){ if(v[i+1].second<v[i].first && v[i].second-v[i].first>0){ x=v[i].second-1; }else x=v[i].second; }else x=v[i].second; }else x=v[i].second; } } return ans; } int main(){ int t; scanf("%d",&t); while(t--){ v.clear(); scanf("%d",&n); scanf("%d%d",&a[1],&b[1]); int l=a[1],r=b[1]; for (int i=2;i<=n;i++){ scanf("%d%d",&a[i],&b[i]); if (a[i]<=r && b[i]>=l){ l=max(l,a[i]); r=min(r,b[i]); }else { v.pb(mp(l,r)); l=a[i]; r=b[i]; } } v.pb(mp(l,r)); LL ans=2e18; ans=min(ans,go(v[0].first)); ans=min(ans,go(v[0].second)); printf("%lld\n",ans); } return 0; }
标签:clear nbsp hdu game const string span fine size
原文地址:https://www.cnblogs.com/bluefly-hrbust/p/11381279.html