ZOJ Problem Set - 1715When Can We Meet?

代码随想录
博客专家认证
2011-03-03 06:10:04
ZOJ Problem Set - 1715When Can We Meet?

--------------------------------------------------------------------------------

Time Limit: 1 Second Memory Limit: 32768 KB

--------------------------------------------------------------------------------
The ICPC committee would like to have its meeting as soon as possible to address every little issue of the next contest. However, members of the committee are so busy maniacally developing (possibly useless) programs that it is very difficult to arrange their schedules for the meeting. So, in order to settle the meeting date, the chairperson requested every member to send back a list of convenient dates by E-mail. Your mission is to help the chairperson, who is now dedicated to other issues of the contest, by writing a program that chooses the best date from the submitted lists. Your program should find the date convenient for the most members. If there is more than one such day, the earliest is the best.

Input

The input has multiple data sets, each starting with a line containing the number of committee members and the quorum of the meeting.

N Q

Here, N, meaning the size of the committee, and Q meaning the quorum, are positive integers. N is less than 50, and, of course, Q is less than or equal to N.

N lines follow, each describing convenient dates for a committee member in the following format.

M Date1 Date2 ... DateM

Here, M means the number of convenient dates for the member, which is an integer greater than or equal to zero. The remaining items in the line are his/her dates of convenience, which are positive integers less than 100, that is, 1 means tomorrow, 2 means the day after tomorrow, and so on. They are in ascending order without any repetition and separated by a space character. Lines have neither leading nor trailing spaces.

A line containing two zeros indicates the end of the input.


Output

For each data set, print a single line containing the date number convenient for the largest number of committee members. If there is more than one such date, print the earliest. However, if no dates are convenient for more than or equal to the quorum number of members, print 0 instead.


Sample Input

3 2
2 1 4
0
3 3 4 8
3 2
4 1 5 8 9
3 2 5 9
5 2 4 5 7 9
3 3
2 1 4
3 2 5 9
2 2 4
3 3
2 1 2
3 1 2 9
2 2 4
0 0


Sample Output

4
5
0
2


这是我的代码超时了,我很诧异怎么能超时呢!!!!!!!求助大家
#include <stdio.h>
#include<iostream>
using namespace std;
int main()
{
int n, q, z, t, i, max, max_i;
int a[51];
while (cin>>n>>q && (n||q))
{
for (i=1; i<=50; i++)
a[i] = 0;
while (n--)
{
scanf("%d", &z);
while (z--)
{
scanf("%d", &t);
a[t]++;
}
}
max = a[1]; max_i = 1;
for (i=2; i<=50; i++)
{
if (a[i] > max)
{
max = a[i];
max_i = i;
}
}
printf("%d\n", max>=q ? max_i : 0);
}
return 0;
}

...全文
157 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
icerainfc522 2011-03-22
  • 打赏
  • 举报
回复
相对来说,循环同样的次数,for比While速度快
还有 比较 maxi的时候可以和
scanf("%d", &t);
a[t]++;
合并
logiciel 2011-03-21
  • 打赏
  • 举报
回复
请注意:
The remaining items in the line are his/her dates of convenience, which are positive integers less than 100
因此:
int a[51];
要改为
int a[101];

还要改:
for (i=1; i<=50; i++)


llhqwertyuiop 2011-03-20
  • 打赏
  • 举报
回复
这是什么程序啊?

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧