Codeforces #345 Div2 B Beautiful Paintings 构造/结论

给你n个数(n<=1000),a1,a2,,,,an(每个 数均<=1000),现在想将他们以某种方式进行排列,每个数如果比前面的数大(第一个数除外),则获得一个得分,求最大得分。

这个题怎么做很显然,就是不断地区构造严格递增子序列。但是怎么构造我想了好久,最终当时想了一个方法,就是用num[i]表示i这个数出现了几次,即每读进来一个数t,就num[t]++,然后再有一个cnt数组,cnt[i]表示出现次数>=i的数字有多少个,cnt[i]的构造方法也简单,每读进来一个数t,先num[t]++,然后再cnt[num[t]]++,如此这样下来,cnt[i]自己就实现了,相当于假如一个数t有x个,把它依次分到cnt[1],cnt[2],,,cnt[x]中,这样cnt[i]所代表的i个数肯定是互不一样的i个数,然后从cnt[1],一直扫描,ans+=cnt[i]-1,当然,当cnt[i]==0时就停止扫描了,也不记录在内。然后就可以了。
然而我是想了好久才想到的,之前一直想排序啊什么的,却感觉还是不好搞。
后来在群里提到,乐警官一语中的。
构造最优解的办法是:每次找到一个最长的递增序列,找到之后如果还有数,就再找一个。这样一共有n-1个间隔,有max个间隔不满足条件,max是出现最多的数的次数,所以答案是(n-1)-(max-1)。

所以,比赛时交的代码:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 1005
int num[maxn];
int cnt[maxn];
int main()
{
	//freopen("input.txt", "r", stdin);
	int n, t, ans = 0, l = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &t);
		num[t]++;
		cnt[num[t]]++;
	}
	for (int i = 1; i < maxn&&cnt[i] != 0; ++i)
		ans += cnt[i] - 1;
	printf("%d\n", ans);
	//while (1);
	//system("pause");
	return 0;
}



更好的方法:
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define maxn 1005
inline int mini(int a, int b)
{
	return a < b ? a : b;
}
inline int maxi(int a, int b)
{
	return a > b ? a : b;
}
int num[maxn];
int main()
{
	//freopen("input.txt", "r", stdin);
	int n, t, m = -1, ans = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
	{
		scanf("%d", &t);
		num[t]++;
	}
	for (int i = 1; i < maxn; ++i)
		m = maxi(m, num[i]);
	ans = n - m;
	printf("%d\n", ans);
	//while (1);
	//system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值