HDU1029 --------map


Ignatius and the Princess IV

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K (Java/Others)
Total Submission(s): 15477 Accepted Submission(s): 6296

Problem Description
"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?

Input
The 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.

Output
For 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
1

我的行不通的代码:


#include<iostream>
#include<algorithm>
#include<stdlib.h>
#define N 1000000
using namespace std;
int comp(int a,int b)
{
return a<b;
}

int main()
{
int i,n,m;
int a[N],b[N];
while(cin>>n)
{
for(i=0;i<n;i++)
{
cin>>a[i];
b[i]=1;
}
sort(a,a+n,comp);
for(i=1;i<n;i++)
{
if(a[i]==a[i-1])
b[i]+=b[i-1];
else
continue;
}
for(i=0;i<n;i++)
{
if(b[i]==(n+1/2))
{
cout<<a[i]<<endl;
break;
}
}
}
return 0;
}

数据太大这样做行不通

AC:


#include <iostream>
#include <map>
using namespace std;

int main()
{
int n,d,ans;
while(cin>>n)
{
map<int,int> m;
for(int i=1;i<=n;i++)
{
scanf("%d",&d);
m[d]++;
if(m[d]==(n+1)/2)
ans=d;
}
printf("%d\n",ans);
}
return 1;
}

HDU1029 --------map

时间: 2024-08-01 06:09:37

HDU1029 --------map的相关文章

(Arrays.sort() 或 map) Ignatius and the Princess IV hdu1029

Ignatius and the Princess IV 链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029 借鉴链接:https://blog.csdn.net/tigerisland45/article/details/52146154 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others)Total Submission(s): 4

python练习之map()和reduce()函数

利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字.输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']: 1 def normalize(name): 2 name=name.lower() 3 name=name[0].upper()+name[1:] 4 return name 5 6 7 8 9 10 # 测试: 11 L1 = ['adam', 'LISA', 'barT'] 12 L2 = l

ArrayList以及Map小练

ArrayList常用方法 public static void main(String[] args) { List list = new ArrayList(); List list1 = new ArrayList(); for (int i = 0; i < 5; i++) { list.add(i, "string"+i);//add(E e)向某集合的尾部追加 list1.add(i, "string"+(i+10)); } List list2

python之Map函数

# map()函数使用举例 # 功能:map()接受一个函数f和一个或多个list,将f依次作用在list的每个元素,得到一个新的列表 # 语法:map(方法名,列表,[列表2]) # 注意:map()函数的返回值需要强制转换成list类型,且不改变原列表值 list_1 = [1, 2, 3, 4, 5] list_2 = [1, 2, 3, 4, 5] # 单个参数 def double_function(number): return number * 2 list_result = li

14:Challenge 7(map大法好)

总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  262144kB 描述 给一个长为N的数列,有M次操作,每次操作是以下两种之一: (1)修改数列中的一个数 (2)求数列中某个值出现了多少次 输入 第一行两个正整数N和M.第二行N的整数表示这个数列.接下来M行,每行开头是一个字符,若该字符为'M',则表示一个修改操作,接下来两个整数x和y,表示把x位置的值修改为y:若该字符为'Q',则表示一个询问操作,接下来一次整数x,表示求x这个值出现了多少次. 输出 对每一

数据结构Set和Map

一.数据结构 Set 集合的基本概念:集合是由一组无序且唯一(即不能重复)的项组成的.这个数据结构使用了与有限集合相同的数学概念,应用在计算机的数据结构中.  特点:key 和 value 相同,没有重复的 value.ES6 提供了数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. 1. 如何创建一个 Set const s = new Set([1, 2, 3]); 2.属性 console.log(s.size); // 3 3.Set 类的方法 --set.add(v

spark 教程三 spark Map filter flatMap union distinct intersection操作

RDD的创建 spark 所有的操作都围绕着弹性分布式数据集(RDD)进行,这是一个有容错机制的并可以被并行操作的元素集合,具有只读.分区.容错.高效.无需物化.可以缓存.RDD依赖等特征 RDD的创建基础RDD 1.并行集合(Parallelized Collections):接收一个已经存在的Scala集合,然后进行各种并行运算 var sc=new SparkContext(conf) var rdd=sc.parallelize(Array(2,4,9,3,5,7,8,1,6)); rd

JSON字符串-赋张最初接触后台从map转json的方法

**************************************** json数组: ****************************************************** 后台传回前台 和 前台传回后台的都是json字符串 ****************************************************** 将java中的map,list等转成json格式 (map转成JSONObject   list转成JSONArray) 前者的类

数组中出现最多的数,以及接口 Map.Entry&lt;K,V&gt;

1 package test.tools; 2 3 import java.util.Collection; 4 import java.util.Collections; 5 import java.util.HashMap; 6 import java.util.Map; 7 8 public class TestArr { 9 10 public static void MaxCount(int[] arr) { 11 Map<Integer, Integer> map = new Ha