北大poj-1081
You Who?
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 801 | Accepted: 273 |
Description
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
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
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 2 3 4
2 2 3 4
3 2 1 2
4 2 1 2
Sample Output
0
Hint
我本来的程序中有考虑最大不认识的人数是否需要加1的情况,结果被判wrong。
最近找了个当年比赛的标准程序,发现其本意还真是只要找所有分组情况中,两组里最大不认识的人数最小的那个情况即可。
并且原题的测试数据也是这个:
Judge's Input
1 0
2 0
3 0
4 0
5 0
Judge's Output
2
2 1 2
3 3 4 5
可能编题目的作者也没有料到有这个(浅显的)问题,于是整个背景叙述就显得如此荒诞了。
POJ简化题目——班级的输出免了,但误人的背景留了下来。 顺便说一下,标程是用非常复杂的位运算实现的,这样和人多人少关系就不大了。
总人数最多是30个(于是每个人与他人的关系、每个班的组成(最多15个人)都可以用一个unsigned表示),算法就是穷举。 分析:题目对我来说感觉蛮难理解的,可能是因为数学太弱?这个题目数据比较弱,poj上最多4个students,所以算是水题了。
/*
1 2 3 4
2 2 3 4
3 2 1 2
4 2 1 2
*/ #include "stdio.h"
#include "stdlib.h"
#include "string.h" #define MAX_STUDENT 30 int gwFriendList[MAX_STUDENT+1][MAX_STUDENT+1];
int gwdivide[MAX_STUDENT+1];
int gwStudentNum; int MAX(int a, int b)
{
return (a>b) ? a : b;
} int MIN(int a, int b)
{
return (a>b) ? b : a;
} int strangeNum(int wboy, int class[])
{
int i = 0;
int j = 0;
int lone = 0;
for(i=1; i<=class[0]; i++)
{
if(wboy == class[i])
continue;
for(j=1; j<=gwFriendList[class[i]][0]; j++)
{
if(wboy == gwFriendList[class[i]][j])
break;
}
if(j > gwFriendList[class[i]][0])
lone++;
}
return lone;
} void exchangeValue(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
} void buildClass(int class[], int flag)
{
int i = 0;
int j = 0;
for(i=1; i<=gwStudentNum; i++)
{
if(gwdivide[i] == flag)
class[++j] = i;
}
class[0] = j;
} int calcLone(int class[])
{
int i = 0;
int lone = 0;
for(i=1; i<=class[0]; i++)
{
lone = MAX(lone, strangeNum(class[i], class));
}
return lone;
} int Lone()
{
int class1[MAX_STUDENT+1];
int class2[MAX_STUDENT+1];
buildClass(class1, 1);
buildClass(class2, 0);
return MAX(calcLone(class1), calcLone(class2));
} int divideClass(int now, int max)
{
int wlone = 30;
int i = 0;
if(now == max)
return Lone();
else
{
wlone = MIN(wlone, divideClass(now+1, max));
for(i=now+1; i<=max; i++)
{
if(gwdivide[i] != gwdivide[max])
{
exchangeValue(&gwdivide[i], &gwdivide[max]);
wlone = MIN(wlone, divideClass(i, max));
exchangeValue(&gwdivide[i], &gwdivide[max]);
}
}
return wlone;
}
} int main()
{
int i = 0;
int j = 0;
int wCount = 0;
int lone = 0; while(1)
{
if(EOF == scanf("%d", &j))
break;
scanf("%d", &wCount);
gwFriendList[j][0] = wCount;
gwStudentNum++;
for(i=1; i<=wCount; i++)
{
scanf("%d", &gwFriendList[j][i]);
}
wCount = 0;
}
for(i=1; i<=(gwStudentNum/2); i++)
{
gwdivide[i] = 1;
} lone = divideClass(1,gwStudentNum); printf("%d\n", lone);
return 0;
}
北大poj-1081的更多相关文章
- 北大POJ题库使用指南
原文地址:北大POJ题库使用指南 北大ACM题分类主流算法: 1.搜索 //回溯 2.DP(动态规划)//记忆化搜索 3.贪心 4.图论 //最短路径.最小生成树.网络流 5.数论 //组合数学(排列 ...
- poj 1081 To The Max
To The Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 【Java】深深跪了,OJ题目Java与C运行效率对比(附带清华北大OJ内存计算的对比)
看了园友的评论之后,我也好奇清橙OJ是怎么计算内存占用的.重新测试的情况附在原文后边. -------------------------------------- 这是切割线 ----------- ...
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- 各大OJ
北大POJ 杭电HDU 浙大ZOj 蓝桥杯 PAT
- leetcode学习笔记--开篇
1 LeetCode是什么? LeetCode是一个在线的编程测试平台,国内也有类似的Online Judge平台.程序开发人员可以通过在线刷题,提高对于算法和数据结构的理解能力,夯实自己的编程基础. ...
- OJ题目JAVA与C运行效率对比
[JAVA]深深跪了,OJ题目JAVA与C运行效率对比(附带清华北大OJ内存计算的对比) 看了园友的评论之后,我也好奇清橙OJ是怎么计算内存占用的.重新测试的情况附在原文后边. ----------- ...
- C++ 指针常见用法小结
1. 概论 2.指针基础 3. 指针进阶 4. 一维数组的定义与初始化 5. 指针和数组 6. 指针运算 7. 多维数组和指针 8. 指针形参 9. 数组形参 10. 返回指针和数组 11. 结语 ...
- 几个比較好的IT站和开发库官网
几个比較好的IT站和开发库官网 1.IT技术.项目类站点 (1)首推CodeProject,一个国外的IT站点,官网地址为:http://www.codeproject.com,这个站点为程序开发人员 ...
- C语言程序设计100例之(10):最大公约数
例10 最大公约数 问题描述 有三个正整数a,b,c(0<a,b,c<10^6),其中c不等于b.若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c. 输入数据 第 ...
随机推荐
- 整理mac上的php环境
重装了一下mb,重新编译环境太麻烦了,所以整理一下mac系统自带php环境用: OS X yosemite 10.10.5 自带apache,php 1,让apache启用php模块 sudo vi ...
- android native crash 分析
工具: addr2line arm-linux-androideabi-addr2line -aCfe libart.so 0x63006d 当libart.so包含符号表的情况下,可以查询到他的地址 ...
- linux主要的发行版及其区别和联系
1. 主要发行版 linux主要发行版有3类: (1).Debian (2).Slackware (3).Redhat (1)Debian Ubuntu 针对桌面和服务器 knopix 以安全著称 ( ...
- 安装Nvidia k80驱动步骤
安装Nvidia k80驱动步骤 ------------------ 环境介绍: CentOS6 远程终端使用Xshell -------------------- 安装Nvidia k80驱动步骤 ...
- Linux 系统把英文修改成中文界面
1.一般安装后的linux系统都是英文的界面,网上查了一下各种说法都有,我只做了如下的配置就好了,下载个中文包,改一下i18n就完事了,并没有那么复杂 下面上图文: 目前是英文的界面 2.下载个中文包 ...
- PE文件学习系列笔记四-C++实现PE文件的分析
合肥程序员群:49313181. 合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入) Q Q:408365330 E-Mail:egojit@qq.com 综述: 首 ...
- sqlite数据库的使用helper
public class SQLiteHelper { //public static String ConnectionString = "Data Source= ...
- 数据库 'MessageManage' 的事务日志已满。若要查明无法重用日志中的空间的原因,请参阅 sys.databases 中的 log_reuse_wait_desc 列。
提供两种办法:(SQL Server2008) 注意:建议使用第一种方法.第二种方法只是删除已有日志文件,日后还会继续生成. 第一种方法:清空日志. 1.打开企业管理器,直接在查询分析器里执行:(如果 ...
- Oracle 游标
游标的简介 游标的概念 游标是从数据表中提取出来的数据,以临时表的形式存放在内存中,在游标中有一个数据指针,在初始状态下指向的是首记录,利用fetch语句可以移动该指针,从而对游标中的数据进行各种操作 ...
- Linux安装脚本需要交互之如何实现自动安装
Linux中shell脚本运行时经常需要进行交互,比如安装软件的过程中对license声明的确认,需要输入yes,回车之类的确认信息.这个在自动化安装的时候就会是个问题. 通常对于这个问题比较灵活的解 ...