http://acm.hdu.edu.cn/showproblem.php?pid=1029

给定一个数组,其中有一个相同的数字是出现了大于等于(n + 1) / 2次的。要求找出来、

1、明显排序后,中间那个位置的就是ans,复杂度O(nlogn)

2、

考虑分治

假设那个人是ans,那么对于其他人,我都用一个ans去代替它。就是他们两个一起死了,从这个数组中删除。

那么我最后剩下的那个人当然还是ans,因为它人数都大于一半了。

同时删除了两个没关的人,那更好了。如果枚举的那两个人都是ans,那么只能记录次数++,

关键是如何模拟这个过程。能做到O(n)的复杂度。

考虑用ans和anstime表示答案和答案那个人出现的次数。

1、如果anstime==0,就是还没确定谁是ans,那么就选当前这个人作为答案。

2、否则,如果当前枚举的这个人还是ans,那么记录次数+1,否则记录次数-1(大家一起死了)

最后ans就是答案。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e6 + ;
LL a[maxn];
int n;
void work() {
for (int i = ; i <= n; ++i) {
scanf("%I64d", &a[i]);
}
LL ans = , anstime = ;
for (int i = ; i <= n; ++i) {
if (anstime == ) {
ans = a[i];
anstime++; //出现了一次
} else {
if (ans == a[i]) { //同一个人的话,出现次数++
anstime++;
} else anstime--; //否则同时删除这两个人
}
}
printf("%d\n", ans);
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
while (scanf("%d", &n) != EOF) work();
return ;
}

HDU 1029 一道微软面试题的更多相关文章

  1. 每日微软面试题——day 6(打印所有对称子串)

    每日微软面试题——day 6(打印所有对称子串) 分类: 2.数据结构与算法2011-08-14 14:27 9595人阅读 评论(15) 收藏 举报 面试微软string测试systemdistan ...

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

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

  3. 一道 JavaScript 面试题

    有一道 JavaScript 面试题. f = function () { return true; }; g = function () { return false; }; (function() ...

  4. 怒刷DP之 HDU 1029

    Ignatius and the Princess IV Time Limit:1000MS     Memory Limit:32767KB     64bit IO Format:%I64d &a ...

  5. 一道sql面试题(查询语句)

    一道sql面试题(查询语句)   id name age 1  a        11 2  b        11 3  c        12 4  d        13 5  e        ...

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

  7. 一道经典面试题-----setTimeout(function(){},0)

    一道经典面试题-----setTimeout(function(){},0) 转载: http://www.w3cfuns.com/notes/17398/e8a1ce8f863e8b5abb5300 ...

  8. 一道Python面试题

    无意间,看到这么一道Python面试题:以下代码将输出什么? def testFun():    temp = [lambda x : i*x for i in range(4)]    return ...

  9. new与属性访问的顺序,从一道JS面试题说起

    这段时间一直在研究设计模式,在看工厂模式的时候,看到一段代码 VehicleFactory.prototype.createVehicle = function ( options ) { if( o ...

随机推荐

  1. ACM学习历程—HDU1028 Ignatius and the Princess(组合数学)

    Ignatius and the Princess Description        "Well, it seems the first problem is too easy. I w ...

  2. 【C】字符串常量和字符数组

    此次博客是转载某位博主的文章,不过现在找不到了,所以先声明一下. 先贴一段代码: #include <stdio.h> int main(int argc, const char** ar ...

  3. float浮动改变display类型

    position:absolute和float都会隐式的改变display类型. 也就是说,不论之前是什么类型的元素(display:none除外),只要设置了position:absolute或fl ...

  4. bzoj 3714 [PA2014]Kuglarz——思路+最小生成树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3714 如果用s[ i ]表示前 i 个的奇偶性,那么c(i_j)表示s[ i-1 ]^s[ ...

  5. Vmware ESXi 6.5 安装手册

    1          安装前准备 1.1          硬件环境准备 无 备注: 本指导书以虚拟光驱.虚拟软驱为例,如使用物理光驱.物理软驱安装系统操作则以实际系统光盘.软盘代替. 1.2     ...

  6. C# 线程的暂停和恢复的 实现

    我们可以通过ManualResetEvent类来实现. 声明, 初始化时不执行 private static ManualResetEvent _eventWorkList = new ManualR ...

  7. Jmeter提取响应数据的结果保存到本地的一个文件

    原文地址: https://www.cnblogs.com/whitewasher/p/9504728.html 当做性能压测时,可能会需要把响应数据的一些字段统计出来.这里简单介绍一下. 1.首先把 ...

  8. Java关键字以及一些基础解释

    Java Se:Java Me 和Java Ee的基础,允许开发和部署在桌面,服务器,嵌入式环境和实时环境中使用的java程序,支持java web服务开发类 java ee:是目前java技术应用最 ...

  9. 生成分布式随机ID

    经测试,最快的一种 public class Generator { // should be between 40 (34 years) and 42 (139 years) ; // should ...

  10. query builder 在线生成sql

    http://devtools.korzh.com/easyquery/javascript/docs/javascript-query-builder-php