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 ...
随机推荐
- skynet实践(9)-随机数重复问题
最近在使用skynet的过程中,遇到需要为玩家的每次请求产生一个随机序列的场景.简化如下: main.lua中每隔1S便发出一次随机数请求: local skynet = require " ...
- Wireshark 的使用 —— 过滤器(filter)
1. 基本 ip 地址: 目的IP:ip.dst==192.168.101.8,源ip:ip.src==1.1.1.1 不区分源和目的:ip.addr == 192.168.101.8: 端口过滤: ...
- 使用svg的几种方式
<!-- 图片,背景,框架引入svg文件 --> <img src="test.svg" alt=""> <?xml versio ...
- wingide 显示中文 及 配色方案
http://lihuipeng.blog.51cto.com/3064864/923231 网上收集的方法: 显示中文: 任意文本编辑器打开:x:\Wing IDE\bin\gtk-bin\etc\ ...
- 1107 Social Clusters (30)(30 分)
When register on a social network, you are always asked to specify your hobbies in order to find som ...
- 【LeetCode】070. Climbing Stairs
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- bzoj 4883 [Lydsy1705月赛]棋盘上的守卫——并查集(思路!)
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4883 把各行和各列看成n+m个点. 如果一下能防守行和列,就是最大匹配了.这是每两个左右部点 ...
- win32 UNICODE 支持
#include <string> #ifdef _UNICODE #define tstring std::wstring #define __T(quote) L##quote #el ...
- MQTT 客户端源码分析
参看:逍遥子_mosquitto源码分析系列 参看:MQTT libmosquitto源码分析 参看:Mosquitto学习笔记 一.目录结构 首先我们还是来看一下 mosquitto-1.4.14 ...
- CentOS虚拟机通过主机WIFI上网
0 环境简介 环境如下: (1)宿主机为WIN7系统,连接内网,同时通过本机的WIFI上外网: (2)虚拟机为VMWare12下的CentOS7系统. 1 虚拟机设置 选择NAT 模式: 2 宿主机W ...