You Who?

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 801   Accepted: 273

Description

On the first day of first grade at Friendly Elementrary School, it is customary for each student to spend one minute talking to every classmate that he or she does not already know. When student Bob sees an unfamilar face, he says ``You who?'' A typical response is ``Me Charlie, you who?'' Then Bob says, ``Me Bob!'' and they talk for a minute. It's very cute. Then, after a minute, they part and each looks for another stranger to greet. This takes time. In class of twenty-nine or thirty mutual strangers, it takes 29 minutes; time that, according to teachers, could be better spent learning the alphabet. Of course, it is rare to have a first grade class where nobody knows anyone else; there are neighbors and playmates who already know each other, so they don't have to go through the get-to-know-you minutes with each other.
The two first grade teachers have requested that, to save time,
students be allocated to their two classes so that the difference in the
sizes of the classes is at most one, and the time it takes to complete
these introductions is as small as possible. There are no more than 60
students in the incoming first grade class.

How can the assignment of students to classes be made? Your job is to write the software that answers the question.

Input

The
school records include information about these student friendships,
represented as lists of numbers. If there are 29 students, then they
are represented by the numbers 1 to 29. The record for a single student
includes, first, his/her student identification number (1 to 29, in
this example), then the number of his/her acquaintances, then a list of
them in no particular order. So, for example, this record

17 4 5 2 14 22

indicates that student 17 knows 4 students: 5, 2, and so on. The
records for all the students in the incoming class are represented as
the list of numbers that results from concatenating all the student
records together. Spaces and line breaks are irrelevent in this format.
Thus, this

1 1 2 2 1 1

is a whole database, indicating that there are only two students in the incoming class, and they know each other; and this

1 2 3 4

2 2 3 4

3 2 1 2

4 2 1 2

indicates that 1 doesn't know 2, and 3 doesn't know 4, but all other pairs know each other.

The database has been checked for consistency, so that if A knows B, then B knows A.

Output

Your
output should begin with a number that tells how long it will take to
complete the introductions in the best possible legal class assignment.
For the simple two student problem above, the only legal answer is

0

Sample Input

  1. 1 2 3 4
  2. 2 2 3 4
  3. 3 2 1 2
  4. 4 2 1 2

Sample Output

  1. 0

Hint

To make this problem more tractable, the following changes are being made. There will be exactly two classes. There will be no more that 30 students. The students will be divided as evenly as possible between the classes. The loneliness of a student is the number of students in his class whom he does not know. You are to arrange the classes to minimize the loneliness of the loneliest student. for the sample data ,you can arrange 1 and 3 to the first class,and 2 and 4 to the second class,then they need no time to know each other.
 
  1. 我本来的程序中有考虑最大不认识的人数是否需要加1的情况,结果被判wrong
  2. 最近找了个当年比赛的标准程序,发现其本意还真是只要找所有分组情况中,两组里最大不认识的人数最小的那个情况即可。
  3. 并且原题的测试数据也是这个:
  4. Judge's Input
  5. 1 0
  6. 2 0
  7. 3 0
  8. 4 0
  9. 5 0
  10. Judge's Output
  11. 2
  12. 2 1 2
  13. 3 3 4 5
  14. 可能编题目的作者也没有料到有这个(浅显的)问题,于是整个背景叙述就显得如此荒诞了。
  15. POJ简化题目——班级的输出免了,但误人的背景留了下来。
  16.  
  17. 顺便说一下,标程是用非常复杂的位运算实现的,这样和人多人少关系就不大了。
  18. 总人数最多是30个(于是每个人与他人的关系、每个班的组成(最多15个人)都可以用一个unsigned表示),算法就是穷举。
  19.  
  20. 分析:题目对我来说感觉蛮难理解的,可能是因为数学太弱?这个题目数据比较弱,poj上最多4students,所以算是水题了。
  1. /*
  2. 1 2 3 4
  3. 2 2 3 4
  4. 3 2 1 2
  5. 4 2 1 2
  6. */
  7.  
  8. #include "stdio.h"
  9. #include "stdlib.h"
  10. #include "string.h"
  11.  
  12. #define MAX_STUDENT 30
  13.  
  14. int gwFriendList[MAX_STUDENT+1][MAX_STUDENT+1];
  15. int gwdivide[MAX_STUDENT+1];
  16. int gwStudentNum;
  17.  
  18. int MAX(int a, int b)
  19. {
  20. return (a>b) ? a : b;
  21. }
  22.  
  23. int MIN(int a, int b)
  24. {
  25. return (a>b) ? b : a;
  26. }
  27.  
  28. int strangeNum(int wboy, int class[])
  29. {
  30. int i = 0;
  31. int j = 0;
  32. int lone = 0;
  33. for(i=1; i<=class[0]; i++)
  34. {
  35. if(wboy == class[i])
  36. continue;
  37. for(j=1; j<=gwFriendList[class[i]][0]; j++)
  38. {
  39. if(wboy == gwFriendList[class[i]][j])
  40. break;
  41. }
  42. if(j > gwFriendList[class[i]][0])
  43. lone++;
  44. }
  45. return lone;
  46. }
  47.  
  48. void exchangeValue(int *a, int *b)
  49. {
  50. int tmp;
  51. tmp = *a;
  52. *a = *b;
  53. *b = tmp;
  54. }
  55.  
  56. void buildClass(int class[], int flag)
  57. {
  58. int i = 0;
  59. int j = 0;
  60. for(i=1; i<=gwStudentNum; i++)
  61. {
  62. if(gwdivide[i] == flag)
  63. class[++j] = i;
  64. }
  65. class[0] = j;
  66. }
  67.  
  68. int calcLone(int class[])
  69. {
  70. int i = 0;
  71. int lone = 0;
  72. for(i=1; i<=class[0]; i++)
  73. {
  74. lone = MAX(lone, strangeNum(class[i], class));
  75. }
  76. return lone;
  77. }
  78.  
  79. int Lone()
  80. {
  81. int class1[MAX_STUDENT+1];
  82. int class2[MAX_STUDENT+1];
  83. buildClass(class1, 1);
  84. buildClass(class2, 0);
  85. return MAX(calcLone(class1), calcLone(class2));
  86. }
  87.  
  88. int divideClass(int now, int max)
  89. {
  90. int wlone = 30;
  91. int i = 0;
  92. if(now == max)
  93. return Lone();
  94. else
  95. {
  96. wlone = MIN(wlone, divideClass(now+1, max));
  97. for(i=now+1; i<=max; i++)
  98. {
  99. if(gwdivide[i] != gwdivide[max])
  100. {
  101. exchangeValue(&gwdivide[i], &gwdivide[max]);
  102. wlone = MIN(wlone, divideClass(i, max));
  103. exchangeValue(&gwdivide[i], &gwdivide[max]);
  104. }
  105. }
  106. return wlone;
  107. }
  108. }
  109.  
  110. int main()
  111. {
  112. int i = 0;
  113. int j = 0;
  114. int wCount = 0;
  115. int lone = 0;
  116.  
  117. while(1)
  118. {
  119. if(EOF == scanf("%d", &j))
  120. break;
  121. scanf("%d", &wCount);
  122. gwFriendList[j][0] = wCount;
  123. gwStudentNum++;
  124. for(i=1; i<=wCount; i++)
  125. {
  126. scanf("%d", &gwFriendList[j][i]);
  127. }
  128. wCount = 0;
  129. }
  130. for(i=1; i<=(gwStudentNum/2); i++)
  131. {
  132. gwdivide[i] = 1;
  133. }
  134.  
  135. lone = divideClass(1,gwStudentNum);
  136.  
  137. printf("%d\n", lone);
  138. return 0;
  139. }
  1.  
  1.  

北大poj-1081的更多相关文章

  1. 北大POJ题库使用指南

    原文地址:北大POJ题库使用指南 北大ACM题分类主流算法: 1.搜索 //回溯 2.DP(动态规划)//记忆化搜索 3.贪心 4.图论 //最短路径.最小生成树.网络流 5.数论 //组合数学(排列 ...

  2. poj 1081 To The Max

    To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. 【Java】深深跪了,OJ题目Java与C运行效率对比(附带清华北大OJ内存计算的对比)

    看了园友的评论之后,我也好奇清橙OJ是怎么计算内存占用的.重新测试的情况附在原文后边. -------------------------------------- 这是切割线 ----------- ...

  4. POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14021   Accepted: 5484   Specia ...

  5. 各大OJ

    北大POJ 杭电HDU 浙大ZOj 蓝桥杯 PAT

  6. leetcode学习笔记--开篇

    1 LeetCode是什么? LeetCode是一个在线的编程测试平台,国内也有类似的Online Judge平台.程序开发人员可以通过在线刷题,提高对于算法和数据结构的理解能力,夯实自己的编程基础. ...

  7. OJ题目JAVA与C运行效率对比

    [JAVA]深深跪了,OJ题目JAVA与C运行效率对比(附带清华北大OJ内存计算的对比) 看了园友的评论之后,我也好奇清橙OJ是怎么计算内存占用的.重新测试的情况附在原文后边. ----------- ...

  8. C++ 指针常见用法小结

    1. 概论 2.指针基础 3. 指针进阶 4. 一维数组的定义与初始化 5. 指针和数组 6. 指针运算 7. 多维数组和指针 8. 指针形参 9. 数组形参 10. 返回指针和数组 11. 结语   ...

  9. 几个比較好的IT站和开发库官网

    几个比較好的IT站和开发库官网 1.IT技术.项目类站点 (1)首推CodeProject,一个国外的IT站点,官网地址为:http://www.codeproject.com,这个站点为程序开发人员 ...

  10. C语言程序设计100例之(10):最大公约数

    例10        最大公约数 问题描述 有三个正整数a,b,c(0<a,b,c<10^6),其中c不等于b.若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c. 输入数据 第 ...

随机推荐

  1. 安装Nvidia k80驱动步骤

    安装Nvidia k80驱动步骤 ------------------ 环境介绍: CentOS6 远程终端使用Xshell -------------------- 安装Nvidia k80驱动步骤 ...

  2. 商业信息管理系统 Bizagi 建模pattern

    WCP 1- Sequence This pattern is used to model dependencies between tasks so that one task cannot sta ...

  3. android 使用httpclient访问网络

    在主活动类中,调用一个线程访问网络(android4.0以上耗时的操作不能放在主线程中):       //声明两个Button对象,与一个TextView对象private TextView mTe ...

  4. mysql数据库的导入导出

    当我们在操作数据库的时候,难免会遇到数据导入导出的一些操作,今天突然学到了这个知识点,特意来给大家分享. 我用的是data的这条数据 1.使用数据 mysql> use data; Databa ...

  5. java String

    实例一.substring(int beginIndex,int endIndex) String end ="2007-12-31";System.out.println(end ...

  6. 【转】一种解决h5页面背景音乐不能自动播放的方案

    原文:http://www.cnblogs.com/wmhuang/p/5452259.html --------------------------------------------------- ...

  7. powerdesigner,eclipse整合安装

    com.sybase.powerdesigner.eclipse.link path=D:\\dbs\\dbtools\\SAP\\PowerDesigner16

  8. 二:基础概述netty

    如果不了解netty的,可以百度下,netty社区现在也比较活跃. 现在所谓的大数据,flume,storm等底层都是netty.   netty的性能模型: io模型---->异步非阻塞io ...

  9. C# 利用占位符替换word中的字符串和添加图片

    利用占位符替换word中的字符串和添加图片   ///<summary>         /// 替换word模板文件内容,包括表格中内容         /// 调用如下:WordStr ...

  10. Kotlin Groovy Style Builder

    fun main(args:Array<String>):Unit{ val x = a{ aa{ +"01.01" +"01.02" } aa{ ...