码迷,mamicode.com
首页 > 其他好文 > 详细

[kuangbin带你飞] 基础DP 题集

时间:2019-11-20 13:01:45      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:bsp   大于等于   lag   容器   sum   space   c++   一个   范围   

题目链接:https://vjudge.net/contest/342715

第一题:A - Max Sum Plus Plus

 

 

第二题:B - Ignatius and the Princess IV

题意:给n个数字,n为奇数,求一个数字x,x的个数大于等于(n+1)/2

解法一:map容器

思路:n的范围为1000000,但是元素x值不明,所以我们用map的第一个元素去存x,第二个元素存x的次数

代码:

#include<bits/stdc++.h>
#include<map>
using namespace std;
#define maxn 1000000
int n;
map<int,int> mp;
//思路:采用map,第一个数存数组的数,第二个数存数的个数 
int main(){
    while(scanf("%d",&n)!=EOF){
    mp.clear();
    int num=0,flag=0,b,c;
    for(int i=1;i<=n;i++){
        scanf("%d",&b);
        if(mp.count(b)){ //map的第一个元素 
            mp[b]++;//mp[b]记录b的个数 
            if(num<mp[b]){
                num=mp[b];
                flag=b;
            }
        }
        else{
            mp.insert(pair<int,int>(b,1)); 
        }
    }
    cout<<flag<<endl;
    } 
}

 

[kuangbin带你飞] 基础DP 题集

标签:bsp   大于等于   lag   容器   sum   space   c++   一个   范围   

原文地址:https://www.cnblogs.com/Aiahtwo/p/11896587.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!