B - Ignatius and the Princess IV DP

#include<iostream>
#include<vector>
using namespace std;
int cnt[999999];
int main()
{
    int time,n,limit;
    vector<int> been;
    while(cin>>time)
    {
    int a = 1;
    memset(cnt,0,sizeof(cnt));
    limit = (time+1)/2;
    while(time--)
    {
        cin>>n;
        cnt[n]++;
        if(cnt[n]>=limit&&a)
        {
            cout<<n<<endl;
            a=0;
        }
    }
    }
}

普通cnt数组

DP版本:

include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
    _int64 n,a;
    while(scanf("%I64d",&n)!=EOF)
    {
        _int64 b=0,c;
        while(n--)
        {
            scanf("%I64d",&a);
            if(b==0)
            {
                c=a;
                b++;
            }
            else
            {
                if(c==a)
                    b++;
                else
                    b--;
            }
        }
        printf("%I64d\n",c);
    }
    return 0;
时间: 2024-08-01 13:02:09

B - Ignatius and the Princess IV DP的相关文章

HDU 1029 Ignatius and the Princess IV DP

kuangbin 专题 这题,有很多种解法. 第一种: 直接比较每个数出现次数. 1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cstdio> 6 #include<set> 7 #include<map> 8 #include<vector> 9 #include<

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

HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出现的次数, 若次数一旦达到(n+1)/2,即输出a[i] 注意能出现(n+1)/2次数的最多只有一个 /* HDU 1029 *Ignatius and the Princess IV --- dp */ #include <cstdio> #include <cstring> #in

HDU 1029: Ignatius and the Princess IV

Ignatius and the Princess IV ///@author Sycamore ///@date 8/8/2017 #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; while (cin >> n) { map<int, int>v; int t,ans=-1; for (int i = 1;

HDU1029 Ignatius and the Princess IV

问题链接:HDU1029 Ignatius and the Princess IV.基础练习题,用C++语言编写. 题意简述:输入n(n是奇数),然后输入n个整数,求出现(n+1)/2次的整数. 问题分析:n是奇数,(n+1)/2是n的一半以上,只要将n个数据排序,出现(n+1)/2次的整数必然会出现在中间位置. 本问题使用C++语言编写的原因是函数sort()的参数简单,使用方便. AC的C++语言程序如下: /* HDU1029 Ignatius and the Princess IV */

[2016-03-27][HDU][1029][Ignatius and the Princess IV]

时间:2016-03-30 22:03:01 星期三 题目编号:[2016-03-27][HDU][1029][Ignatius and the Princess IV] 题目大意:给定n个数字,输出其中出现次数超过n/2的数字 #include <algorithm> #include <cstdio> using namespace std; const int maxn = 1E6 + 10; int a[maxn]; int main(){ int n; while(~sc

Ignatius and the Princess IV

Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others)Total Submission(s): 24410    Accepted Submission(s): 10261 Problem Description "OK, you are not too bad, em... But you can never pass the

杭电 1029 Ignatius and the Princess IV

http://acm.hdu.edu.cn/showproblem.php?pid=1029 Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others) Total Submission(s): 16754    Accepted Submission(s): 6730 Problem Description "OK, you ar

[ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)

笨蛋的难题(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述        笨蛋之所以称为笨蛋,是因为他有点路痴.他一旦不高兴,就必然一个人漫无目的的出去走走.今天下雨了,他又不高兴了,怎么办?那就出去走呗,这不又丢了,这次幸好记下出来时的方向,并且在一张纸上密密麻麻的记下了他拐的弯(他很聪明吧,拐的弯都是90度的弯),0代表左拐,1代表右拐,那么多0.1,他实在看不下去了,正好遇见善良加聪明的你,你能告诉他,他现在面向哪吗? 输入 多组测试数据 第一行 输入:

Ignatius and the Princess IV 水

B - Ignatius and the Princess IV Time Limit:1000MS     Memory Limit:32767KB     64bit IO Format:%I64d & %I64u Submit Status Description "OK, you are not too bad, em... But you can never pass the next test." feng5166 says. "I will tell y