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 ...
随机推荐
- listen 66
Frog Species Found in Big Apple Scientists discover new species all the time—on the order of 15,000 ...
- Python: scikit-image Blob detection
这个用例主要介绍利用三种算法对含有blob的图像进行检测,blob 或者叫斑点,就是在一幅图像上,暗背景上的亮区域,或者亮背景上的暗区域,都可以称为blob.主要利用blob与背景之间的对比度来进行检 ...
- docker镜像管理基础
[root@node01 ~]# docker pull quay.io/coreos/flannel:v0.10.0-amd64 v0.10.0-amd64: Pulling from coreos ...
- [Forward]Improving Web App Performance With the Chrome DevTools Timeline and Profiles
Improving Web App Performance With the Chrome DevTools Timeline and Profiles We all want to create h ...
- stm32之存储系统
一.STM32系统结构 要想深刻理解STM32的存储器,需要首先知道STM32的系统结构. 如Figure 1,是STM32系统结构框图. 根据STM32 Reference manual (RM00 ...
- [hdu3586]Information Disturbing树形dp+二分
题意:给出一棵带权无向树,以及给定节点1,总约束为$m$,找出切断与所有叶子节点联系每条边所需要的最小价值约束. 解题关键:二分答案,转化为判定性问题,然后用树形dp验证答案即可. dp数组需要开到l ...
- 使用API修改控制台输出的颜色(前景色和背景色)
1.api原型 SetConsoleTextAttribute BOOL WINAPI SetConsoleTextAttribute( _In_ HANDLE hConsoleOutput, // ...
- HDU 3572 Task Schedule (最大流)
C - Task Schedule Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- 在多台手机上批量安装apk
1.手机要打开adb调试 2.该程序可以实现台android手机的多个apk批量安装 1.getSeriaoNum.py模块,该模块获取已连接手机的序列号 import os import threa ...
- 2. docker 容器的管理(创建、查看、启动、终止、删除)
一.创建 docker create:创建容器,处于停止状态. centos:latest:centos容器:最新版本(也可以指定具体的版本号). 本地有就使用本地镜像,没有则从远程镜像库拉取. 创建 ...