HDU 1029 一道微软面试题
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 一道微软面试题的更多相关文章
- 每日微软面试题——day 6(打印所有对称子串)
每日微软面试题——day 6(打印所有对称子串) 分类: 2.数据结构与算法2011-08-14 14:27 9595人阅读 评论(15) 收藏 举报 面试微软string测试systemdistan ...
- HDU 1029 Ignatius and the Princess IV --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- 一道 JavaScript 面试题
有一道 JavaScript 面试题. f = function () { return true; }; g = function () { return false; }; (function() ...
- 怒刷DP之 HDU 1029
Ignatius and the Princess IV Time Limit:1000MS Memory Limit:32767KB 64bit IO Format:%I64d &a ...
- 一道sql面试题(查询语句)
一道sql面试题(查询语句) id name age 1 a 11 2 b 11 3 c 12 4 d 13 5 e ...
- 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 ...
- 一道经典面试题-----setTimeout(function(){},0)
一道经典面试题-----setTimeout(function(){},0) 转载: http://www.w3cfuns.com/notes/17398/e8a1ce8f863e8b5abb5300 ...
- 一道Python面试题
无意间,看到这么一道Python面试题:以下代码将输出什么? def testFun(): temp = [lambda x : i*x for i in range(4)] return ...
- new与属性访问的顺序,从一道JS面试题说起
这段时间一直在研究设计模式,在看工厂模式的时候,看到一段代码 VehicleFactory.prototype.createVehicle = function ( options ) { if( o ...
随机推荐
- fiddler篡改请求数据
有时需要修改请求或返回结果来验证网站存在的漏洞,因此需要使用到fiddler的断点功能. 如何修改请求前数据? 1.设置请求前断点 Rules--Automatic breakpoints--befo ...
- mvc 让伪静态变得简单
IIS 部署后访问*.* config 配置: <modules runAllManagedModulesForAllRequests="true"> < ...
- BZOJ3230 相似子串[后缀数组+二分+st表]
BZOJ3230 相似子串 给一个串,查询排名i和j的子串longest common suffix和longest common prefix 思路其实还是蛮好想的,就是码起来有点恶心.可以发现后缀 ...
- [SHOI 2017] 分手是祝愿
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4872 [算法] 首先发现 , 对于一个开关 , 按下2次和没按是等价的 , 因此每个 ...
- Linux中vsftpd安装和配置
目录 Redhat/CentOS安装vsftp软件 Ubuntu/Debian安装vsftp软件 Redhat/CentOS安装vsftp软件 1. 安装vsftp $ yum install vsf ...
- stm32之入门知识
一.stm32最小系统 stm32最小系统组成如下(除了stm32芯片外): 1.电源模块,3.3V电源,需要用稳压器件,有时要用感容网络产生stm32所使用的模拟电源. 2.时钟模块,有源或者无源晶 ...
- MAF框架的使用限制
虽然MAF实现了插件式开发,动态热插拨,AppDomain隔离等诸多优点,但是正因为它复杂的功能机制也带来了很多其它方面的使用限制,下面列出官方给出的MAF框架的使用限制. 1) 在应用主程序显示的插 ...
- vs2013错误 1 Unable to find messages file 'cscui.dll'
最近使用VS编译的时候,提示错误 错误 1 Unable to find messages file 'cscui.dll' 搜索后,几乎只有1个结果,就是搜索cscui.dll 把正常大小的文件替 ...
- FZU2056 最大正方形(二分答案)
Problem 2056 最大正方形 Accept: 171 Submit: 516Time Limit: 1000 mSec Memory Limit : 32768 KB Probl ...
- 2-3 Flutter开发环境与iOS开发环境设置(Mac)
Mac下环境搭建 先不看了 都是Mac下的环境搭建