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 onlynpieces 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 allnletters placed in some order. Nikita wondered if he can compile at least six different words of lengthnfrom 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 integernthat is the number of pieces with letters (1 ≤n≤ 100). The second line describes these pieces asnintegers 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 lengthn, 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";
}
分享到:
相关推荐
acm.timus.ru最全代码
Timus ... Problems code. (Mainly Java implementation) Chinese 刷题代码,主要是java实现,可能会有其他语言代码 代码来自LeetCode / NowCoder / timus 等 site url LeetCode NowCoder Timus LeetCode-cn
acm.timus.ru解决方案 这些是我对ACM.TIMUS.RU问题的解决方案
In the present world you frequently meet a lot of call numbers and they are going to be longer and longer. You need to remember such a kind of numbers. One method to do it in an easy way is to assign ...
timus.ru_solutions 使用的语言:Python 使用的Python版本:3.9 我可以用Python语言解决的一些问题在“ python”目录中。 有些解决方案可以在Java中运行,但确切的解决方案/算法在Python中不起作用! 不知道为什么我...
acm.timus.ru 1709 problem
timus.ru_solutions_python 使用的python:3.9 我可以用Python语言解决的一些问题在python目录中。 有些解决方案可以在Java中运行,但确切的解决方案/算法在Python中不起作用! 我不知道为什么
资源分类:Python库 所属语言:Python 资源全名:timus-sender-0.1.1.post1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Timus上习题解答与代码参考,这一部分对应于Timus上的Beginner部分的习题
timus:Timus在线法官问题
https://acm.timus.ru/problem.aspx?space=1&num=1119 题目答案
将图表添加到Timus Online Judge配置文件 该扩展程序将已解决问题的数量图表添加到Timus Online Judge的个人资料和比较器中。 功能:*概要文件和比较中已解决问题计数的图表*向图表中添加更多用户,删除它们,自定义...
timus OJ 1197 lonesome kinght
Timus图表 将图表添加到Timus Online Judge个人资料中 特征 概要文件中和比较期间已解决问题的计数图表 向图表添加更多用户,删除它们,自定义图例颜色 缓存配置文件数据 隐藏图表 (可选)突出显示最近两个月内接受...
蒂莫斯 该文件夹包含用Python编写的文件(主要是Python2.7,有些是Python3.4)我通过这些文件在timus上通过了相应的测试。
http://acm.timus.ru 俄罗斯乌拉尔大学在线题库 SGU http://acm.sgu.ru/ 俄罗斯圣萨拉托夫州大学在线题库 ELJ http://acm.mipt.ru/judge/bin/problems.pl?lang=en file:///M|/acm/ACM大量习题题库及建议培养计划.txt...
timus-online-judge Timus Online Judge 是俄罗斯最大的带有自动评判系统的编程问题档案。 问题主要是从在乌拉尔联邦大学、乌拉尔锦标赛、乌拉尔 ACM ICPC 次区域比赛和彼得罗扎沃茨克训练营举办的比赛中收集的。 ...
语言:English将图表添加到Timus Online Judge个人资料中该扩展程序将已解决问题的数量图表添加到Timus Online Judge的个人资料和比较器中。功能:*概要文件和比较中已解决问题计数的图表*向图表中添加更多用户,删除...
http://acm.timus.ru/problem.aspx?space=1&num=1362 一道树形动态规划的题目解答,ural1362
Pascal acm_timus_ural_1148.pas