LA 3713 宇航员分组
题目链接:http://vjudge.net/contest/142615#problem/B
题意:有A,B,C三个人物要分配个N个宇航员,每个宇航员恰好要分配一个任务,设平均年龄为X,只有年龄大于或等于X的宇航员才能分配任务A。只有年龄严格小于X的宇航员才能分配任务B。而任务C没有限制。有M对宇航员相互讨厌,因此不能分配到同一任务。编程找出一个满足上述所有要求的任务分配方案。
分析:
2-SAT。
建图:
肯定是不能同时去 C 的

同一类的话:

那么就是2a或者2b了,到底是哪个,就得看年龄了。
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = + ; struct TwoSAT
{
int n;
vector<int> G[maxn*];
bool mark[maxn*];
int S[maxn*], c; bool dfs(int x)
{
if (mark[x^]) return false;
if (mark[x]) return true;
mark[x] = true;
S[c++] = x;
for (int i = ; i < G[x].size(); i++)
if (!dfs(G[x][i])) return false;
return true;
} void init(int n)
{
this->n = n;
for (int i = ; i < n*; i++) G[i].clear();
memset(mark, , sizeof(mark));
} // x = xval or y = yval
void add_clause(int x, int xval, int y, int yval)
{
x = x * + xval;
y = y * + yval;
G[x^].push_back(y);
G[y^].push_back(x);
} bool solve()
{
for(int i = ; i < n*; i += )
if(!mark[i] && !mark[i+])
{
c = ;
if(!dfs(i))
{
while(c > ) mark[S[--c]] = false;
if(!dfs(i+)) return false;
}
}
return true;
}
}; int n, m, total_age, age[maxn]; int is_young(int x)
{
return age[x] * n < total_age;
} TwoSAT solver; int main()
{
while(scanf("%d%d", &n, &m) == && n && m)
{
total_age = ;
for(int i = ; i < n; i++)
{
scanf("%d", &age[i]);
total_age += age[i];
} solver.init(n);
for(int i = ; i < m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
if(a == b) continue;
solver.add_clause(a, , b, ); // 不能同去任务C
if(is_young(a) == is_young(b)) // 同类宇航员
solver.add_clause(a, , b, ); // 不能同去任务A或者任务B
} if(!solver.solve()) printf("No solution.\n");
else for(int i = ; i < n; i++)
if(solver.mark[i*]) printf("C\n"); // x[i]=false,去任务C
else if(is_young(i)) printf("B\n"); // x[i]=true的年轻宇航员去任务B
else printf("A\n"); // x[i]=true的年长宇航员去任务A
}
return ;
}
LA 3713 宇航员分组的更多相关文章
- LA 3713
The Bandulu Space Agency (BSA) has plans for the following three space missions: Mission A: Landing ...
- 【LA3713 训练指南】宇航员分组 【2-sat】
题意 有A,B,C三个任务要分配给n个宇航员,其中每个宇航员恰好要分配一个任务.设所有n个宇航员的平均年龄为x,只有年龄大于或等于x的宇航员才能分配任务A:只有年龄严格小于x的宇航员才能分配任务B,而 ...
- LA 3713 Astronauts
给个题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=sh ...
- 2-SAT 问题与解法小结
2-SAT 问题与解法小结 这个算法十分的奇妙qwq... 将一类判定问题转换为图论问题,然后就很容易解决了. 本文有一些地方摘录了一下赵爽<2-SAT解法浅析> (侵删) 一些概念: \ ...
- 图论$\cdot$2-SAT问题
2-SAT问题是这样的:有$n$个布尔变量$x_i$,另有$m$个需要满足的条件,每个条件的形式都是“$x_i$为真/假或者$x_j$为真/假”.比如:"$x_1$为真或者$x_3$为假“. ...
- [UOJ317]【NOI2017】游戏 题解
题意 小 L 计划进行 \(n\) 场游戏,每场游戏使用一张地图,小 L 会选择一辆车在该地图上完成游戏. 小 L 的赛车有三辆,分别用大写字母 A.B.C 表示.地图一共有四种,分别用小写字 ...
- LA 3268 号码簿分组(最大流+二分)
https://vjudge.net/problem/UVALive-3268 题意: 有n个人和m个组.一个人可能属于很多组.现在请你从某些组中去掉几个人,使得每个人只属于一个组,并使得人数最多的组 ...
- GPRS GPRS(General Packet Radio Service)是通用分组无线服务技术的简称,它是GSM移动电话用户可用的一种移动数据业务,属于第二代移动通信中的数据传输技术
GPRS 锁定 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . GPRS(General Packet Radio Service)是通用分组无线服务技术的简称,它是GSM移动电话用户可 ...
- LINQ 的查询_联表、分组、排序
1.查询 var v = from s in db.Set<ScoreInfo>().ToList()group s by s.subject into scoreselect new{ ...
随机推荐
- error CS0016: 未能写入输出文件
win7 下解决办法: 1.打开C:\Windows ,找到 TEMP 文件夹 2. 进行权限设置,点击编辑,找到 IIS-User,勾选所有权限
- filter的详细配置
我们已经了解了filter的基本用法,还有一些细节配置在特殊情况下起作用. 在servlet-2.3中,Filter会过滤一切请求,包括服务器内部使用forward转发请求和<%@ includ ...
- jQuery 插件autocomplete
jQuery 插件autocomplete 自动加载 参考: http://www.cnblogs.com/Peter-Zhang/archive/2011/10/22/2221147.html ht ...
- PHP版本VC6与VC9、Thread Safe与None-Thread Safe等的区别
PHP版本VC6与VC9.Thread Safe与None-Thread Safe等的区别 [摘要]PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言,在PHP发 ...
- mysql导出导入
1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u dbuser -p dbname > dbname.sql 2.导出一个表 ...
- centos同步北京时间
yum install ntp ntpdate #ntpdate -u 202.120.2.101 //写入硬件 #hwclock -w 以下是国内常见的NTP服务器 ntp.sjtu.edu.cn ...
- NULL-safe equal
http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to ,=NULL,NULL=NULL; ...
- Git reset 常见用法
Git reset 1. 文件从暂存区回退到工作区 2. 版本回退 1.1 git reset HEAD filename :回退文件,将文件从暂存区回退到工作区 //也可以使用 git reset ...
- kafka 生产者java编码
public class KafkaProducerDemo { public static void main(String[] args) throws InterruptedException ...
- 换手率的公司使用MQTT的框架
向换手率公司学习.该公司貌似也使用 Golang