HDU-1029-Ignatius aned the Princess IV
链接:https://vjudge.net/problem/HDU-1029#author=0
题意:
给你n个数字,请你找出出现至少(n+1)/2次的数字。
思路:
dp,hash超时了,不知道是不是我的问题。
cnt记录数量,
目标数比其他数加起来还多。
所以当cnt为0时当前数为目标数,相同cnt++,不同cnt--。
代码:
#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
using namespace std; typedef long long LL; const int MAXN = 1e5 + 10;
const int INF = 0x7fffffff; int main()
{
int n;
while (~scanf("%d", &n))
//while (cin >> n)
{
int cnt = 0,a,vis;
for (int i = 1;i <= n;i++)
{
scanf("%d", &a);
if (cnt == 0)
{
vis = a;
cnt++;
}
else
{
if (a == vis)
cnt++;
else
cnt--;
}
}
printf("%d\n", vis);
} return 0;
}
HDU-1029-Ignatius aned the Princess IV的更多相关文章
- HDU 1029 Ignatius and the Princess IV --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)
HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...
- HDU 1029 Ignatius and the Princess IV (map的使用)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1029 Ignatius and the Princess IV Time Limit: 2000/10 ...
- hdu 1029 Ignatius ans the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- [ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)
Ignatius and the Princess IV Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32767K (Ja ...
- HDU 1029 Ignatius and the Princess IV (动态规划、思维)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)
此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...
- HDU 1029 Ignatius and the Princess IV
解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...
- HDU 1029 Ignatius and the Princess IV DP
kuangbin 专题 这题,有很多种解法. 第一种: 直接比较每个数出现次数. #include<iostream> #include<string> #include< ...
- HDU 1029 Ignatius and the Princess IV(数论)
#include <bits/stdc++.h> using namespace std; int main(){ int n; while(~scanf("%d",& ...
随机推荐
- the art of seo(chapter one)
preface:Andy Johns (@ibringtraffic):growth strategist@Wealthfront ***1.Search Reflecting Consciousne ...
- Myeclipse项目内容没有报错但是项目上面却有红色叉叉
当src文件夹为空的时候,git是不提交空文件夹的,所以check出来的项目中没有src文件夹,这个时候也会出现此问题.
- nyoj 1279 (河南省第九届ACM比赛 D 题)
思路:变换一下坐标系新的坐标系就是给定的两条直线,变换之后求 x,y 都严格递增的点的个数的max: 求 x,y 都严格递增的点的个数的max,按照x的从小到大排序,x相同的按照y的从大到小排序然后对 ...
- object_test.py
#方法,属性,私有化加双下划线 ''' __a 从外部无法访问,但是类的内部可以访问.实际上还是能在类外访问这些私有方法,尽管不应该这么做:s._A__a 如果不需要使用这种方法但是又不行让其他对象不 ...
- slice 和 splice 区别
splice() 方法与 slice() 方法的作用是不同的,splice() 方法会直接对数组进行修改. slice(start,end) ; start 必需.规定从何处开始选取.如果是负数,那 ...
- Django (2006, 'MySQL server has gone away') 本地重现与解决
最近我们的Django项目供Java Sofa应用进行tr调用时, 经常会出现一个异常: django.db.utils.OperationalError: (2006, 'MySQL server ...
- 机器学习之K-means算法
前言 以下内容是个人学习之后的感悟,转载请注明出处~ 简介 在之前发表的线性回归.逻辑回归.神经网络.SVM支持向量机等算法都是监督学习算法,需要样本进行训练,且 样本的类别是知 ...
- SQL 电子书
http://vdisk.weibo.com/search/?type=&sortby=default&keyword=SQL+Server&filetype=&pag ...
- sql之索引
作用: - 约束 - 加速查找 普通索引:加速查找 create index 索引名称 on 表名(列名,) drop index 索引名称 on 表名 主键索引:加速查找+不能为空+不能重复 cr ...
- python 之生产者消费者模型
进程实现: import time,random from multiprocessing import Process,Queue def producer(name,q): count= 0 wh ...