Comet OJ - Contest # 10 B title beauties

### topic Link ###

 

Title effect: with n a positive integer, each positive integer representing a phrase, the same positive integer as idiom. A positive integer with a maximum of only appear three times.

Seeking an arrangement such that this arrangement, the same idiom minimum spacing maximum, the maximum value of the output of the minimum interval.

The same intervals as the number of idioms idiom middle therebetween.

Particularly, when each phrase appeared only once, the maximum value of the minimum interval considered  n-  .

 

analysis:

1, if the maximum number of idioms occur is 2:00: For example, there are two different idioms 12, they have appeared twice, in order to make the minimum interval to be the biggest, the best structure is equal to two intervals of two 1 2 interval.

       1 2 XXXXX 1 2 (X on behalf of other idioms)

So that you can make idioms appeared twice, their spacing are the same, which does not have a smaller. Then let res = number of idioms appears only once, set a number for the two types of idioms = occur.

The res = n - 2 * a, the answer is: RES + (A -. 1) .

 

2, then the phrase does not appear for the number 2, there appears the number is 3, may be so configured.   

1  2  3     X X X    1  2  3   X X     1  2  3

So you will find that the minimum interval 123 three idioms are the same, that this is odd because the X (X = 5), sub-accounts for both sides certainly has fewer side, so little of this side is the minimum interval.

Res = set number of idioms appears once, a = number of kinds of idioms appears three times. The res = n - 3 * a, the answer is: res + (a - 1) .

 

3, then for another three appearances of both idioms twice, you can also use the above ideological construct:

Because in order to minimize the maximum interval, and for idiom appears three times, preferably in a leftmost one on the far right, in the middle of a most. As for the phrase appears twice, because there is no limit "middle third idiom", then in order to increase the number of intervals idioms appear three times, the best way is best left to the idiom of the three rightmost drained appears, and then idiom appears only twice in the row.

For example, it has appeared three times: 123, appears twice in 45.

1  2  3   4  5           XX     1  2  3           X X X        4  5   1  2  3

Such as small as possible to ensure maximum and a minimum interval of 1, 2 and 2, 3 and 3 is the same as the minimum spacing (idiom which is the same for the two, which are the same interval)

But there is a limit idea: if a lot of idioms number appears twice, causing them to have to move closer to the middle, may make an appearance idiom will be less than twice the spacing interval phrase appears three times, then at the same time seeking out and min to go.

Res = set number of idioms appears once, x = number of kinds of idioms appears twice, y = number of kinds of idioms appears three times.

There are: res = n - 2 * x - 3 * y, the answer is: min (RES X + Y + - . 1, RES / 2 + X + Y - . 1)

hhh min then this is actually useless, it can be seen from the formula, the right (idiom appears a minimum interval of three) will be smaller than the left (minimum interval of two idioms appear).

 

Since then much data, record the number of idioms appear unless open map or discrete.

code show as below:

 

#include<iostream>
#include<algorithm>
using namespace std;
int n;
int a[100008],b[100008],c[100008], vis[100008];
int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++){
        scanf("%d", &a[i]);
        b[i] = a[i];
    }
    sort(b + 1, b + n + 1);
    int len = unique(b + 1, b + n + 1) - b - 1;
    for (int i = 1; i <= n; i++) c[i] = lower_bound(b + 1, b + len + 1, a[i]) - b, vis[c[i]] ++;//离散化
    int x = 0, y = 0;
    for (int i = 1; i <= len; i++){
        if (vis[i] == 2) x++;
        else if (vis[i] == 3) y++;
    }
    int res, ans;
    if ((!x) && (!y)){
        years = n;
    }
    else if (x&&(!y)){
        res = n - 2 * x;
        years = res + x - 1 ;
    }
    else if ((!x)&&y){
        res = (n - 3 * y) / 2 ;
        years = res + y - 1 ;
    }
    else{
        res = n - 2 * x - 3 * y;
        // ans = min (res + y + x - 1 res / 2 + x + y - 1); 
        ans = res / 2 + x + y - 1 ;
    }
    printf("%d\n", ans);
}

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/Absofuckinglutely/p/11887389.html