loj 2336 「JOI 2017 Final」 绳

serious

First, all positions are the most dyed once, many times due to the dye, then, not like a start on the final dyed colors. And you can start dye lust

Because the final length of 2, then if the dye after this sequence can be finished off, then the first maximum of only two colors, there is to meet all the same color for a great Unicom block length must be an even number, but the first and last length may be odd

Proof, then sufficient conditions to permit, i.e. certain legal like this can come up with a method, a time subsequent operation. Communication with a first rearmost shrunk to a block length (this constant optimal), then a communication because then block length is an even number, it is possible that the next axisymmetric turned over, and this operation is repeated until the card 2. then, the intermediate length having a length of an odd number must not legal. first, if only a reduced intermediate to the final odd length, then you are not the last turn. If there is more then an odd communication block, then how do you turn the middle section has an odd length. If you still do not understand can ask the person (tpq)

Then consider the answer is calculated. First, the block length is an even number of communication can be split into a number of length \ (2 \) communication block, then in accordance with a first length \ (2 \) of the communication block is starting position \ ( 1 \) or \ (2 \) classification discussed, as well as to such a minimum of stain, equivalent to the maximum of dyeing position. enumeration to count each color corresponding to the answer, if two blocks are the color communication, then obviously do not change; if the two are not the color, then be dyed another color, this color is what is not considered; if a current color that is not the current color, you can not find the current color must not change the current color loss. now consider another color is what we want to try to make the most of this color position, but there may be some enumeration current position and color together, these locations to dye away, then this color contribution is the total number of occurrences ( \ (referred to as CNT \) ) - and the same number of blocks in the current color block ( \ (referred to as I {F_, J} \) ) so that a color answer. \ (I \ ) of \ (n-cnt_i- \ max_ { j \ neq i} (cnt_j-f_ {i, j}) \)

When writing, \ (f_ {i, j} \) can be put in two colors of different color blocks interconnected edge, then \ (f_ {i, j} \) is \ (i, j \) of between the number of sides. then follow \ (cnt_j \) descending enumeration another color, and if the enumeration \ (f_ {i, j} = 0 \) a \ (J \) do not enumerate the next is not necessarily superior

#include<bits/stdc++.h>
#define LL long long
#define uLL unsigned long long
#define db double

using namespace std;
const int N=1e6+10;
int rd()
{
    int x=0,w=1;char ch=0;
    while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*w;
};
int n,m,a[N],cn[N],sq[N],an[N],f[N][2];
int v[N];
bool cmp(int aa,int bb){return cn[aa]>cn[bb];}
vector<int> e[N];
void wk()
{
    for(int i=1;i<=m;++i)
    {
        vector<int>::iterator it;
        for(it=e[i].begin();it!=e[i].end();++it)
            ++v[*it];
        for(int j=1;j<=m;++j)
        {
            int x=sq[j];
            if(i==x) continue;
            an[i]=max(an[i],cn[i]+cn[x]-v[x]);
            if(!v[x]) break;
        }
        for(it=e[i].begin();it!=e[i].end();++it)
            --v[*it];
    }
}

int main()
{
    n=rd(),m=rd();
    for(int i=1;i<=n;++i)
        a[i]=rd(),++cn[a[i]];
    for(int i=1;i<=m;++i)
        an[i]=cn[i],sq[i]=i;
    sort(sq+1,sq+m+1,cmp);
    for(int i=1;i+1<=n;i+=2)
        if(a[i]!=a[i+1])
            e[a[i]].push_back(a[i+1]),e[a[i+1]].push_back(a[i]);
    wk();
    for(int i=1;i<=m;++i) e[i].clear();
    for(int i=2;i+1<=n;i+=2)
        if(a[i]!=a[i+1])
            e[a[i]].push_back(a[i+1]),e[a[i+1]].push_back(a[i]);
    wk();
    for(int i=1;i<=m;++i) printf("%d\n",n-an[i]);
    return 0; 
}

Guess you like

Origin www.cnblogs.com/smyjr/p/11551169.html