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$段,考虑最少能够减少多少划分 ...
随机推荐
- 【Git】Git初始化一个仓库
文章目录 初始化仓库 检查当前文件状态 跟踪新文件 提交更新 跳过使用暂存区域 移除文件 添加远程仓库 推送到远程仓库 简单记录-慕课网 从0开始 独立完成企业级Java电商网站开发 Git初始化一个 ...
- 【Oracle】10g rac如何开启归档和关闭归档
开启归档: 1.设置想设置的归档的位置,我们这里归档的位置为ASM磁盘组,磁盘组的名称为DATA alter system set log_archive_dest_1='location=+DATA ...
- 【RAC】oracle11g r2 rac环境删除节点步骤
1.移除数据库实例 如果节点运行了service首先需要删除service使用dbca图形化界面删除节点依次选择 Real Application Clusters -- > Instance ...
- os-Bytes环境变量劫持
信息收集 netdiscovery -i eth0 nmap -sV -sC 192.168.43.74 -oA os-Bytes gobuster -u 192.168.43.74 -w /usr/ ...
- git 基本命令和操作
设置全局用户名+密码 $ git config --global user.name 'runoob' $ git config --global user.email test@runoob.com ...
- Py装饰器
装饰器: 1.定义,什么是装饰器 装饰器本质是一个函数,它是为了给其他函数添加附加功能 2.装饰器的两个原则 原则1 不修改被修饰函数的源代码原则2 不修改被修饰函数的调用方式 3.首先来看一 ...
- 安卓开发视频教程!想找工作的你还不看这份资料就晚了!Android校招面试指南
前言 准备面试其实已经准备了挺久了,当时打算面试准备了差不多以后,跟公司谈谈涨薪的事情,谈不拢的话,就年后直接找其他的公司.谁想到婚假还没休完,老板就在公司宣布了撤出上海的决定,愿意去深圳的就去,不愿 ...
- 【python刷题】LRU
什么是LRU? LRU是Least Recently Used的缩写,即最近最少使用,是一种常用的页面置换算法,选择最近最久未使用的页面予以淘汰.该算法赋予每个页面一个访问字段,用来记录一个页面自上次 ...
- numpy、pandas学习二
#numpy中arrary与pandas中series.DataFrame区别#arrary生成数组,无索引.列名:series有索引,且仅能创建一维数组:DataFrame有索引.列名import ...
- CF175C Geometry Horse 题解
"日拱一卒,功不唐捐" 写在前面 本人因为没开long long而被迫参考楼下思路重构代码,最后发现这个问题加了long long才得以AC 进入正题 -->这是题面 这是百 ...