Timus 2011. Long Statement 排列组合的运用
来源:互联网 发布:车贷app管理系统源码 编辑:程序博客网 时间:2024/05/22 16:04
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 n letters placed in some order. Nikita wondered if he can compile at least six different words of length n from 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 n that is the number of pieces with letters (1 ≤ n ≤ 100). The second line describes these pieces as n integers 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
61 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";}
1 0
- Timus 2011. Long Statement 排列组合的运用
- Python的排列组合运用
- URAL 2011. Long Statement (数论)
- 排列组合之next_permutation函数的运用
- 指数型母函数运用的实例(排列组合)
- 【SQL】JDBC之Statement类中“批处理”的综合运用
- URAL 1881. Long problem statement
- URAL 1881Long problem statement 模拟
- C#语法灵活运用之排列组合算法
- Long Long 的使用
- 运用Handler与线程的sleep(long)的安卓定时器
- int ,long , long long类型的范围
- int ,long , long long类型的范围
- int ,long , long long类型的范围
- int ,long , long long类型的范围
- int 、long 、long long 的表示范围
- int ,long , long long类型的范围
- int,long,long long,__int64的范围
- 广州电子路考视频发布 2014广州电子路考考点
- oracle行列转换总结
- tomcat 启动不了
- IT痴汉的工作现状8-三思而后行
- 《Head First Programming》---python 10_GUI Mixer2
- Timus 2011. Long Statement 排列组合的运用
- 防守阵地 I
- The serializable class Proname does not declare a static final serialVersionUID field of type long
- 路考步骤七步走 科目三考试一定没问题!
- 新驾考科目三-2014新交规科目三大路考试技巧
- 分享,创造的灵魂——分享广东省”英课“计划青年教师信息化新课堂计划试验课程申报表
- 广州驾校考试实际道路考试注意事项(图)
- 排序算法之堆排序
- 在线教育