HDU1029(去掉两个不同的数序列中出现频率最多的那个数不变)DP2
2017-12-30 12:56
309 查看
题目链接
题目要求:找出一个序列中出现频率大于(n+1)/2的那个数字
AC代码
题目要求:找出一个序列中出现频率大于(n+1)/2的那个数字
AC代码
/*第一种方法就是对于一个序列来说 如果我们每次去掉两个不同的数,该序列中 出现频率最多的那个数是不变的 */ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main() { int n; while(~scanf("%d",&n)) { int cnt=0,res; for(int i=0;i<n;i++) { int x; scanf("%d",&x); if(cnt==0) { res=x; cnt++; } else { if(res==x) cnt++; else cnt--; } } printf("%d\n",res); } return 0; } /*第二种方法就是排序, 找出数组中出现次数超过一半的元素 */ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=1e8+5; int a[maxn]; int main() { int n; while(~scanf< 4000 /span>("%d",&n)) { for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); printf("%d\n",a[(n+1)/2]); } return 0; }
相关文章推荐
- 求出现频率最多的那个字母和出现的次数
- HDU1029:Ignatius and the Princess IV -DP求一列数中出现次数最多的数的次数
- 一个整型数组里除了两个不同数字之外,其它的数字都出现了两次。请写程序找出这两个只出现一次的数字。
- CSU 1354 Distinct Subsequences 求不同子序列和 dp
- Python cookbook(数据结构与算法)找出序列中出现次数最多的元素算法示例
- SQL 查询重复出现次数最多的记录,按出现频率排序(SQL语句)
- Linq从两个edmx(两个数据库)中选取数据进行查找出现错误解决 (指定的 LINQ 表达式包含对与不同上下文关联的查询)
- 随机给定10个小于10的数字(出现的数字可能重复),从其中取出3个各不相同的数字可组合出多个不同的3位数,请输出这些3位数中最大的那个3位数
- HDOJ 5791 Two(dp—求两个序列的公共序列个数)
- 找出最长连续子序列,要求最多很有3个不同的数
- UVA 11235 Frequent values 非递减序列 l r范围内 出现最多的数字次数 RMQ
- (拓展)在一组成对出现的数据中找出两个不同的数
- 统计一个文档中出现频率最多的k个单词的实现
- 统计每十帧出现次数最多的那个
- YT03-递推求解课后题目-1004 计算直线的交点数-(6.7日-烟台大学ACM预备队解题报告)分析: 将n 条直线排成一个序列,两条直线最多只有一个交点,三条直线最多有两个交点,直线n 和其他n
- 求把一个序列分为两个两组,两组各自和相差最小的最大的那个和
- 给定两个整数 l 和 r ,对于所有满足1 ≤ l ≤ x ≤ r ≤ 10^9 的 x ,把 x 的所有约数全部写下来。对于每个写下来的数,只保留最高位的那个数码。求1~9每个数码出现的次数。
- 数组-10. 求整数序列中出现次数最多的数(15)
- Trick(十二)——统计 label(序列)各个标签值出现的次数以及出现次数最多的标签
- python-如何统计序列中元素的出现频率?