The meaning of problems
https://vjudge.net/problem/CodeForces-1257E
Three, everyone has some figures are combined . 1 ~ n-, each one can give a digital another person has to ask the minimum number of operations, such that the first person has . 1 ~ i number of the second person have i + . 1 ~ J numbers, a third person has J + . 1 ~ n- number, i.e. the first person as a prefix, a second intermediate portion of the individual, the third person suffix. Note: You can have one or two people do not have final figures.
Thinking
First things simple, consider the case of only two people. Provided CNT1 [x] x represents the number of the first number before the individual has, CNT2 [x] x represents the number of the second person has a number of front, the prefix is obtained if the first person 1 ~ i, a second suffix to obtain individual i + 1 ~ n, then consideration of cnt2 [i] + cnt1 [n] -cnt1 [i].
Consider three cases, similarly, if the first person to obtain 1 ~ i, the second person to obtain i + 1 ~ j, a third person to get j + 1 ~ n, then consideration of cnt2 [i] + cnt3 [i ] + cnt1 [j] -cnt1 [i] + cnt3 [j] -cnt3 [i] + cnt1 [n] -cnt1 [j] + cnt2 [n] -cnt2 [j], to give simplification: cnt2 [i] -cnt1 [i] + cnt3 [j] + cnt1 [n] + cnt2 [n] -cnt2 [j], i.e. cnt2 [i] -cnt1 [i] + cnt1 [n] + cnt2 [n] + cnt3 [j ] -cnt2 [j], so i enumerate, the minimum value of this formula, found that two left and j-related items on the formula influential, and j> = i, then transformed into enumeration problem i , seeking cnt2 [i] -cnt1 [i] + min (cnt3 [i ~ n] -cnt2 [i ~ n]) + cnt1 [n] + cnt2 [n], we only use maintenance cnt3 [j] -cnt2 [ j] suffix to the minimum.
Note: Because there may be one or two people without any number, then the time i and j is equal, such as when there is no second person i == number j, j when i == 0 == no personal 1,2 number, when the second i == j == n, the number of three persons, i == 0 && j == n when no personal number 1, 3, and so the situation
Code
#include<bits/stdc++.h> using namespace std; #define inf 0x3f3f3f3f #define ll long long const int N=200005; const int mod=1e9+7; const double eps=1e-8; const double PI = acos(-1.0); #define lowbit(x) (x&(-x)) int a[4][N]; int c[4][N],mn[N]; int main() { std::ios::sync_with_stdio(false); int k1,k2,k3; cin>>k1>>k2>>k3; int n=k1+k2+k3; for(int i=1;i<=k1;i++) { cin>>a[1][i]; ++c[1][a[1][i]]; } for(int i=1;i<=k2;i++) { cin>>a[2][i]; ++c[2][a[2][i]]; } for(int i=1;i<=k3;i++) { cin>>a[3][i]; ++c[3][a[3][i]]; } for(int i=1;i<=n;i++) { for(int j=1;j<=3;j++) { c[j][i]+=c[j][i-1]; } } mn[n+1]=inf; for(int i=n;i>=0;i--) { mn[i]=min(mn[i+1],c[3][i]-c[2][i]); } int ans=inf; for(int i=0;i<=n;i++) { ans=min(ans,c[2][i]-c[1][i]+mn[i]+c[1][n]+c[2][n]); // cout<<i<<" "<<ans<<endl; } cout<<ans<<endl; return 0; }