[kuangbin带你飞]专题十二 基础DP1 B - Ignatius and the Princess IV

B - Ignatius and the Princess IV

题目链接:https://vjudge.net/contest/68966#problem/B

题目:

"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.

"I will tell you an odd number N, and then N integers. There will
be a special integer among them, you have to tell me which integer is
the special one after I tell you all the integers." feng5166 says.

"But what is the characteristic of the special integer?" Ignatius asks.

"The integer will appear at least (N+1)/2 times. If you can‘t find
the right integer, I will kill the Princess, and you will be my dinner,
too. Hahahaha....." feng5166 says.

Can you find the special integer for Ignatius?

InputThe input contains several test cases. Each test case
contains two lines. The first line consists of an odd integer
N(1<=N<=999999) which indicate the number of the integers feng5166
will tell our hero. The second line contains the N integers. The input
is terminated by the end of file.

OutputFor each test case, you have to output only one line which contains the special number you have found.

Sample Input

5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1

Sample Output

3
5题意:给你一些数,求出那个出现次数大于等于(n+1)/2的数思路:用map来记录每个数出现了几次

//
// Created by hanyu on 2019/8/4.
//
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include<math.h>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int a[maxn];
        map<int,int>mp;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            mp[a[i]]++;
        }
        int m=(n+1)/2;
        for(auto it:mp)
        {
            if(it.second>=m)
            {
                printf("%d\n",it.first);
                break;
            }
        }
    }
    return 0;
}

1

原文地址:https://www.cnblogs.com/Vampire6/p/11298196.html

时间: 2024-07-29 10:45:57

[kuangbin带你飞]专题十二 基础DP1 B - Ignatius and the Princess IV的相关文章

【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 B - Ignatius and the Princess IV

http://www.cnblogs.com/joeylee97/p/6616039.html 引入一个cnt,输入元素与上一个元素相同,cnt增加,否则cnt减少,当cnt为零时记录输入元素,因为所求数字至少出现(N+1)/2次,所以最后记录元素就是所求元素 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<cmath&g

【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 G - 免费馅饼

https://vjudge.net/contest/68966#problem/G 正解一: http://www.clanfei.com/2012/04/646.html 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath> 7 #define IN

【算法系列学习】DP和滚动数组 [kuangbin带你飞]专题十二 基础DP1 A - Max Sum Plus Plus

A - Max Sum Plus Plus 1 https://vjudge.net/contest/68966#problem/A 2 3 http://www.cnblogs.com/kuangbin/archive/2011/08/04/2127085.html 4 5 /* 6 状态dp[i][j]有前j个数,组成i组的和的最大值.决策: 7 第j个数,是在第包含在第i组里面,还是自己独立成组. 8 方程 dp[i][j]=Max(dp[i][j-1]+a[j] , max( dp[i-

【算法系列学习】状压dp [kuangbin带你飞]专题十二 基础DP1 D - Doing Homework

https://vjudge.net/contest/68966#problem/D http://blog.csdn.net/u010489389/article/details/19218795 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath>

【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 E - Super Jumping! Jumping! Jumping!

https://vjudge.net/contest/68966#problem/E http://blog.csdn.net/to_be_better/article/details/50563344 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath>

【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 C - Monkey and Banana

https://vjudge.net/contest/68966#problem/C [参考]http://blog.csdn.net/qinmusiyan/article/details/7986263 [题意]:多组测试数据        每组测试数据给出n个砖块的长.宽.高,每种砖块有无穷多个,可以有三种不同的放置方法(xy;xz;yz),下面的砖要比上面的砖的长和宽都大,问最大的高度是多少. [思路]:[贪心+dp]每块砖有三种放置方法,把所有砖的所有状态都压入vector,先贪心,按面

【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 F - Piggy-Bank 【完全背包问题】

https://vjudge.net/contest/68966#problem/F http://blog.csdn.net/libin56842/article/details/9048173 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 #include<cmath> 7

[kuangbin带你飞]专题十二 基础DP1 A - Max Sum Plus Plus HDU - 1024

A - Max Sum Plus Plus HDU - 1024 题目链接:https://vjudge.net/contest/68966#problem/A 题目: 现在我觉得你在Ignatius.L的“Max Sum”问题上得到了一个AC.要成为一个勇敢的ACMer,我们总是挑战自己更难的问题.现在你面临着一个更加困难的问题. 给定连续的数字序列S 1,S 2,S 3,S 4 ... S x,... S n(1≤x≤n≤1,000,000,-32768≤Sx≤32767).我们定义函数su

[kuangbin带你飞]专题十二 基础DP1 N - Longest Ordered Subsequence POJ - 2533(最长上升子序列LIS)

N - Longest Ordered Subsequence POJ - 2533 题目链接:https://vjudge.net/contest/68966#problem/N 题目: 最长有序子序列如果a1 <a2 <... <aN,则排序ai的数字序列. 让给定数字序列(a1,a2,...,aN)的子序列为任何序列(ai1,ai2,...,aiK),其中1 <= i1 <i2 <... <iK <= N 例如,序列(1,7,3,5,9,4,8)具有有