Title effect: there are n number of fence panels, P workers, each worker can be coated fence panels interval period, if the employment p-2 Q workers, the maximum number of block fence panels can be painted.
Approach: first q workers can find the maximum number of painted wood, and count the number of each board is coated. The prefix number is required and a number of coated wooden planks and coated twice.
Violence enumeration choose which of the two workers remove, and see if you deleted a few planks. To
#include<iostream> #include<cstdio> #define maxn 5010 using namespace std; int n,p,l[maxn],r[maxn],a[maxn],sum1[maxn],sum2[maxn],ans,cur; int main(){ scanf("%d%d",&n,&p); for(int i=1;i<=p;i++)scanf("%d%d",&l[i],&r[i]); for(int i=1;i<=p;i++) for(int j=l[i];j<=r[i];j++) a[j]++; for(int i=1;i<=n;i++){ sum1[i]=sum1[i-1]; sum2[i]=sum2[i-1]; if(a[i]==1)sum1[i]++; if(a[i]==2)sum2[i]++; if(a[i])cur++; } // for(int i=1;i<=n;i++)printf("%d ",sum1[i]);puts(""); // for(int i=1;i<=n;i++)printf("%d ",sum2[i]);puts(""); for(int i=1;i<=p;i++){ for(int j=i+1;j<=p;j++){ int now=cur; int l1=l[i],r1=r[i],l2=l[j],r2=r[j]; if(l1>l2){ swap(l1,l2); swap(r1,r2); } IF (R1 <L2) { // two sections and do not cover the NOW- SUM1 = [R1] -sum1 [L1- . 1 ]; now-=sum1[r2]-sum1[l2-1]; } else{ int l3=max(l1,l2); int r3=min(r1,r2); now-=sum1[r1]-sum1[l1-1]; now-=sum1[r2]-sum1[l2-1]; now-=sum2[r3]-sum2[l3-1]; } years = max (years now); } } printf("%d\n",ans); return 0; }