方法一:    直接进行排序,输出第(n+1)/2位置上的数即可。 (容易超时,关闭同步后勉强卡过)

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; int n;
int a[];
int main()
{
ios::sync_with_stdio(false);
while(cin>>n)
{
for(int i=;i<n;i++)
cin>>a[i];
sort(a,a+n);
cout<<a[(n+)/]<<endl;
}
}

方法二:原 https://www.cnblogs.com/kuangbin/archive/2011/07/30/2122217.html

在一个序列中如果去掉2个不同的元素,
那么原序列中的多元素,在新的序列中还是多元素,
因此我们只要按照序列依次扫描,先把t赋值给result,
增加个计数器,cnt = 1;然后向右扫描,
如果跟result相同,则cnt++,不同,那么cnt --,
这个真是我们从上面那个结论里得出的,一旦cnt == 0了,
那么必定c不是多元素,这个时候把t赋值为result,cnt = 1;,
重复该过程,知道结束,这个时候,result就是多元素,
这个的时间复杂度为n。

这个算法的正确性在于因为我们要输出的Ans的次数是大于等于n/2+1的,也就是说它出现的次数是大于其他所有数出现次数之和的

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; int n,t,cnt,res; int main()
{
ios::sync_with_stdio(false);
while(cin>>n)
{
cnt=;
for(int i=;i<n;i++)
{
cin>>t; //复杂度为 n
if(cnt==) //这意味着重新一个数
{
res=t; //重新的数 赋给res
cnt++;
}
else
{
if(t==res) cnt++;
else cnt--;
}
}
cout<<res<<endl;
}
}

然而时间上 也是勉强过

Ignatius and the Princess IV (简单DP,排序)的更多相关文章

  1. HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)

    此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...

  2. hdu 1028 Ignatius and the Princess III 简单dp

    题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...

  3. 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 ...

  4. kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  5. HDU 1029 Ignatius and the Princess IV --- 水题

    HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...

  6. (Arrays.sort() 或 map) Ignatius and the Princess IV hdu1029

    Ignatius and the Princess IV 链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029 借鉴链接:https://blog.csd ...

  7. hdu 1029 Ignatius ans the Princess IV

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  8. Ignatius and the Princess IV

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  9. Ignatius and the Princess IV(乱搞一发竟然过了)

    B - Ignatius and the Princess IV Time Limit:1000MS     Memory Limit:32767KB     64bit IO Format:%I64 ...

随机推荐

  1. Mui manifest.json文档说明

    Mui官方地址:https://ask.dcloud.net.cn/article/94 保存在这里,太难找了!!!!!! 以下是完整的manifest.json配置文件,在HBuilder|HBui ...

  2. formSelects隐藏功能键

    隐藏后 方法: // selectId就是select的id,这是在select渲染完后执行 $("#selectId").parent().find(".xm-sele ...

  3. Java注解-注解处理器、servlet3.0|乐字节

    大家好,我是乐字节的小乐,上次给大家带来了Java注解-元数据.注解分类.内置注解和自定义注解|乐字节,这次接着往下讲注解处理器和servlet3.0 一.注解处理器 使用注解的过程中,很重要的一部分 ...

  4. linux input子系统详解

    首先,什么是linux的子系统: 输入子系统由驱动层.输入子系统核心.事件处理层三部分组成.一个输入事件,如鼠标移动通过Driver->Input core->Event handler- ...

  5. 035 Android Volley框架进行网络请求

    1.volley入门介绍 开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行HTTP通 ...

  6. 页面数据加载完成时,显示loading页面.数据加载完,loading隐藏.

    一,引入三个文件 jQuery版本使用 jQuery v1.7.1 jquery-easyui文件中,引入easyui-lang-zh_CN.js的js 做数据加载时使用jquery.blockui. ...

  7. [转帖]Linux内核系统体系概述

    Linux内核系统体系概述 https://www.cnblogs.com/alantu2018/p/8447369.html Linux 内核主要由 5 个模块构成,它们分别是: 进程调度模块 用来 ...

  8. RabbitMQ基础命令rabbitmqctl

    官网文档 https://www.rabbitmq.com/rabbitmqctl.8.html 一般操作命令后台管理页面都有的,部分没有(应用程序管理,和集群管理). 直接使用命令,必须配置环境变量 ...

  9. Django开发常用方法及面试题

    目录 1.对Django的认识? 2.Django .Flask.Tornado的对比 3.什么是wsgi,uwsgi,uWSGI? 4. django请求的生命周期? 5. 简述什么是FBV和CBV ...

  10. Python学习路线图【转载】

    文章来源:https://blog.csdn.net/u014044812/article/details/88079011