Timus 2011. Long Statement 排列组合的运用

Nikita, a schoolboy, is currently taking part in one of programming contests. He is really upset because all the problem statements are so long and unclear. So he took the statement of the first problem and cut it into pieces in such a way that each piece contained exactly one letter. After that, he threw away all pieces with letter other than “a”, “b” or “c”. Now he has only npieces and wants to compile from them his own statement that should be shorter and clearer than the original one.
The new statement should be a single word compiled from all nletters placed in some order. Nikita wondered if he can compile at least six different words of length nfrom the letters. If this is not true, he will be ruined and will start solving other problems. Help Nikita to answer this monumental question!

Input

The first line contains an integer nthat is the number of pieces with letters (1 ≤ n≤ 100). The second line describes these pieces as nintegers from 1 to 3. 1 represents a piece with letter “a”, 2 represents a piece with letter “b”, 3 represents a piece with letter “c”.

Output

If Nikita can compile at least six different words of length n, output “Yes”. Otherwise output “No”.

Sample

input output
6
1 2 2 3 3 3
Yes


本题我使用了permutation的知识去解决。

就是把 1 2 2 3 3 3 看着是一个排列,然后求6次下一个排列,如果无重复,那么就是Yes,如果有重复,那么就是No了。

求排序的时间效率是O(n),所以本算法的速度还是相当快的。

能够运用上学过的知识,感觉真是太好了。

#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;

bool permuteLongStatement(vector<int> &rs, vector<int> &tmp)
{
	int i = tmp.size() - 2;
	for ( ; i >= 0 && tmp[i] >= tmp[i+1]; i--);
	if (i < 0)
	{
		reverse(tmp.begin(), tmp.end());
		return tmp != rs;
	}
	int j = tmp.size() - 1;
	for ( ; tmp[j] <= tmp[i]; j--);
	swap(tmp[i], tmp[j]);
	reverse(tmp.begin()+i+1, tmp.end());
	return tmp != rs;
}

void LongStatement2011()
{
	int n;
	cin>>n;
	vector<int> rs(n);
	for (int i = 0; i < n; i++)
	{
		cin>>rs[i];
	}
	vector<int> tmp(rs);
	bool ok = true;
	for (int i = 0; i < 5; i++)//注意这里是5不是6,因为第一个不permute前算一个
	{
		if (!permuteLongStatement(rs, tmp))
		{
			ok = false;
			break;
		}
	}
	if (ok) cout<<"Yes";
	else cout<<"No";
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值