Pinkie Pie Eats Patty-cakes

Pinkie Pie Eats Patty-cakes

概述

很有意思的一道思维题,类似于脑筋急转弯,题意是给定一个数字序列其中可能会出现重复的数字,问怎么样排列才能让最近的两个重复数字的距离最大。

  • 刚开始的想法是先排序,然后每次都从一个数字中拿出一个排下去,例如序列 4 4 4 4 3 3 3 2 2,那么插入之后就是4 3 2 4 3 2 4 3 4,这样看起来很合理,但是如果序列为4 4 4 4 2 2 3 3,这样安排就是4 3 2 4 3 2 4 4,这样就错了

  • 换个方向安排,就是先把数量最多的放好(假设有n个,那么就有n-1个空),然后用剩下的依次往里面填,如果有和这个最多的相等的,直接等于n-1,也就是说不考虑最后面接着是否还要放。 然后统计一下除了最大的那个总共有多少个,然后除以(n-1),就是答案。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1e5 + 10;
int a[N], cnt[N];
bool cmp(int x, int y){
    return x > y;
}
void solve(){
    memset(cnt, 0, sizeof cnt);
    int n, sum = 0;
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++){
        scanf("%d", a + i);
        cnt[a[i]] ++;
    }
    
    sort(cnt + 1, cnt + 1 + n, cmp);
    int maxn = cnt[1];
    for(int i = 2; i <= n; i ++){
    	if(cnt[i] >= maxn)
    		cnt[i] = maxn - 1;
    	sum += cnt[i];
	}
	if(sum == 0){
		cout << 0 << endl;
		return;
	}
    printf("%d
", sum / (maxn - 1));
	
}
int main(){
    int t;
    scanf("%d", &t);
    while(t --){
        solve();
    }


    return 0;
}
原文地址:https://www.cnblogs.com/pureayu/p/14826830.html