Codeforces Gym-102219 2019 ICPC Malaysia National J. Kitchen Plates (暴力,拓扑排序)

题意:给你5个\(A,B,C,D,E\)大小关系式,升序输出它们,如果所给的大小矛盾,输出\(impossible\).
题意:当时第一眼想到的就是连边然后排序,很明显是拓扑排序(然而我不会qwq,之后再补),但貌似可以直接暴力来写,用二维数组来记录两个数之间的大小关系,如果一维\(>\)二维就记录true,然后我们要对题目所给的关系进行合并,将所有大小关系弄清楚,三个for就可以搞定了,之后判断一下是否有矛盾存在,最后再统计一下大小然后再按顺序输出即可(这题的输出其实没怎么看懂,之前写的桶排输出不知道为什么不给过qaq).
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
using namespace std;
typedef pair<int,int> PII;
typedef pair<long,long> PLL; string s;
int v[100][100];
int cnt[N];
map<int,int> mp;
vector<int> rest;
int main() {
ios::sync_with_stdio(false);cin.tie(0);
for(int i=1;i<=5;++i){
cin>>s;
//初始化记录
for(int j=0;j<s.size();++j){
if(s[1]=='>') v[s[0]-'A'][s[2]-'A']=1;
else v[s[2]-'A'][s[0]-'A']=1;
}
}
//合并
for(int k=0;k<5;++k){
for(int i=0;i<5;++i){
for(int j=0;j<5;++j){
if(v[i][k] && v[k][j]) v[i][j]=1;
}
}
} for(int i=0;i<5;++i){
for(int j=0;j<5;++j){
if(v[i][j] && v[j][i]){
puts("impossible");
return 0;
}
}
}
//统计大小
for(int i=0;i<5;++i){
for(int j=0;j<5;++j){
if(v[i][j]) cnt[i]++;
}
}
for(int len=0;len<5;++len){
for(int num=0;num<5;++num){
if(cnt[num]==len) printf("%c",num+'A');
}
} return 0;
}
补:拓扑排序的裸题,直接写就行了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL; string s;
vector<char> v;
vector<char> out[N];
int in[N]; int main() {
ios::sync_with_stdio(false);cin.tie(0);
for(int i=1;i<=5;++i){
cin>>s;
if(s[1]=='>'){
in[s[0]]++;
out[s[2]].pb(s[0]);
}
else{
in[s[2]]++;
out[s[0]].pb(s[2]);
}
}
queue<char> q;
for(char i='A';i<='E';++i){
if(in[i]==0) q.push(i);
}
while(!q.empty()){
char tmp=q.front();
q.pop();
v.pb(tmp);
for(auto w:out[tmp]){
if(w!=-1){
in[w]--;
if(in[w]==0) q.push(w);
}
w=-1;
}
}
if(v.size()!=5) puts("impossible");
else{
for(auto w:v) printf("%c",w);
}
return 0;
}
Codeforces Gym-102219 2019 ICPC Malaysia National J. Kitchen Plates (暴力,拓扑排序)的更多相关文章
- Codeforces Gym-102219 2019 ICPC Malaysia National E. Optimal Slots(01背包+输出路径)
题意:给你一个体积为\(T\)的背包,有\(n\)个物品,每个物品的价值和体积都是是\(a_{i}\),求放哪几个物品使得总价值最大,输出它们,并且输出价值的最大值. 题解:其实就是一个01背包输出路 ...
- codeforces Gym 100500H H. ICPC Quest 水题
Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...
- codeforces Gym 100500C C. ICPC Giveaways 排序
Problem C. ICPC GiveawaysTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1005 ...
- Codeforces Gym 101190 NEERC 16 G. Game on Graph(博弈+拓扑)
Gennady and Georgiy are playing interesting game on a directed graph. The graph has n vertices and m ...
- Codeforces Round #532 (Div. 2) E. Andrew and Taxi(二分+拓扑排序)
题目链接:https://codeforces.com/contest/1100/problem/E 题意:给出 n 个点 m 条边的有向图,要翻转一些边,使得有向图中不存在环,问翻转的边中最大权值最 ...
- ACM/ICPC 之 数据结构-邻接表+DP+队列+拓扑排序(TSH OJ-旅行商TSP)
做这道题感觉异常激动,因为在下第一次接触拓扑排序啊= =,而且看了看解释,猛然发现此题可以用DP优化,然后一次A掉所有样例,整个人激动坏了,哇咔咔咔咔咔咔咔~ 咔咔~哎呀,笑岔了- -|| 旅行商(T ...
- Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
随机推荐
- MySQL多版本并发控制——MVCC机制分析
MVCC,即多版本并发控制(Multi-Version Concurrency Control)指的是,通过版本链维护一个数据的多个版本,使得读写操作没有冲突,可保证不同事务读写.写读操作并发执行,提 ...
- python模块详解 | filecmp
简介: filecmp是python内置的一个模块,用于比较文件及文件夹的内容,它是一个轻量级的工具,使用非常简单 两个主要的方法: filecmp.cmp(f1, f2[, shallow]) 比较 ...
- 与HBase对比,Cassandra的优势特性是什么?
在1月9日Cassandra中文社区开年活动开始之前的闲聊时间,活动的四位嘉宾就"HBase和Cassandra的对比"这一话题展开了讨论. 总的来说,HBase和Cassan ...
- 解压rpm文件
rpm2cpio zabbix-2.2.2-0.el6.zbx.src.rpm |cpio -div
- Python输出有颜色的文字
原创链接: https://www.cnblogs.com/easypython/p/9084426.html 我们在使用python运维与开发的过程中,经常需要打印显示各种信息.海量的信息堆砌在 ...
- Mysql四种通信协议(linux下本地连接的都是socket 其他都是tcp)
Mysql通信协议 - 张冲andy - 博客园 https://www.cnblogs.com/andy6/p/6204579.html
- XV6学习(8)中断和设备驱动
驱动是操作系统中用于管理特定设备的代码:驱动控制设备硬件,通知硬件执行操作,处理中断,与等待该设备IO的进程进行交互. 当设备需要与操作系统进行交互时,就会产生中断(陷阱的一种),之后内核的陷阱处理代 ...
- Python学习【第2篇】:基本数据类型
基本数据类型 字符串 ---------n1 = "xiaoxing" n2 = "admin" n3 = "123" n4 = & ...
- Trie(字典树)
没时间整理了,老吕又讲课了@ @ 概念 Trie即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种,典型应用是统计和排序大量的字符串(不限于字符串) Trie字典树主要用于存储字符串, ...
- PowerApps画布应用编码规范 和指南
花了一番功夫把PowerApps编码最佳实践的官方白皮书本地化了一下,顺便对部分产品变更做了校对和注释,欢迎大家查阅和分享. 从我实际的项目实施经验来讲,内容还是值得一读,可以帮助项目更好维护和管理. ...