当前位置:网站首页>Gym - 102219J Kitchen Plates(暴力或拓扑序列)
Gym - 102219J Kitchen Plates(暴力或拓扑序列)
2022-07-07 07:09:00 【moyangxian】
题意:略
题记:
做法一:拓扑排序。五个盘子相当于一个有五个点的有向图,A<B相当于A有一条边指向B。那么建图后做一遍基于BFS的拓扑排序即可。
时间复杂度O(5+5)
#include<bits/stdc++.h>
using namespace std;
const int N=10;
int head[N],tot;
int s[N];
vector<int>ans;
struct Edge{
int to,next;
}e[N];
void init(){
memset(head,-1,sizeof(head));
memset(s,0,sizeof(s));
tot=0;
}
void addedge(int u,int v){
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
bool topsort(){
queue<int>q;
for(int i=0;i<5;i++)
if(!s[i]){
q.push(i);
ans.push_back(i);
}
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];~i;i=e[i].next){
int v=e[i].to;
if(--s[v]==0){
q.push(v);
ans.push_back(v);
}
}
}
return ans.size()==5;
}
int main(){
init();
for(int i=0;i<5;i++){
char u,v,op;
cin>>u>>op>>v;
if(op=='>') swap(u,v);
addedge(u-'A',v-'A');
s[v-'A']++;
}
if(!topsort()) cout<<"impossible"<<endl;
else{
for(int i=0;i<ans.size();i++)
cout<<(char)(ans[i]+'A');
cout<<endl;
}
return 0;
}
做法二:
暴力。next_permutation()算出"ABCDE"的全排列,然后查看排列是否符合题目条件即可。
时间复杂度O(5!)
#include<bits/stdc++.h>
using namespace std;
const int N=10;
char a[]={
'A','B','C','D','E'};
char s[5][3];
bool check(){
for(int i=0;i<5;i++){
int p1,p2;
for(int j=0;j<5;j++){
if(a[j]==s[i][0]) p1=j;
if(a[j]==s[i][2]) p2=j;
}
if(p1>p2) return false;
}
return true;
}
int main(){
for(int i=0;i<5;i++){
cin>>s[i];
if(s[i][1]=='>') swap(s[i][0],s[i][2]);
}
do{
if(check()) break;
}while(next_permutation(a,a+5));
if(check()) for(int i=0;i<5;i++) cout<<a[i];
else cout<<"impossible"<<endl;
return 0;
}
边栏推荐
- 華為HCIP-DATACOM-Core_03day
- 2020浙江省赛
- AI从感知走向智能认知
- Unity3d interface is embedded in WPF interface (mouse and keyboard can respond normally)
- CSDN salary increase technology - learn about the use of several common logic controllers of JMeter
- esp8266使用TF卡并读写数据(基于arduino)
- liunx命令
- ViewPager2和VIewPager的區別以及ViewPager2實現輪播圖
- Redis common commands
- Switching value signal anti shake FB of PLC signal processing series
猜你喜欢
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform
印象笔记终于支持默认markdown预览模式
Loxodonframework quick start
JS逆向教程第一发
How to speed up video playback in browser
农牧业未来发展蓝图--垂直农业+人造肉
flex弹性布局
Unity shader (learn more about vertex fragment shaders)
[4G/5G/6G专题基础-147]: 6G总体愿景与潜在关键技术白皮书解读-2-6G发展的宏观驱动力
iNFTnews | 时尚品牌将以什么方式进入元宇宙?
随机推荐
如何成为一名高级数字 IC 设计工程师(5-3)理论篇:ULP 低功耗设计技术精讲(下)
小程序滑动、点击切换简洁UI
大佬们,请问 MySQL-CDC 有什么办法将 upsert 消息转换为 append only 消
【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯
Add new item after the outbound delivery order of SAP mm sto document is created?
Information Security Experiment 3: the use of PGP email encryption software
基础篇:带你从头到尾玩转注解
面试被问到了解哪些开发模型?看这一篇就够了
Dynamics 365online applicationuser creation method change
How to use clipboard JS library implements copy and cut function
Kubernetes cluster capacity expansion to add node nodes
Integer or int? How to select data types for entity classes in ORM
VSCode+mingw64
如何成为一名高级数字 IC 设计工程师(1-6)Verilog 编码语法篇:经典数字 IC 设计
Windows starts redis service
在EXCEL写VBA连接ORACLE并查询数据库中的内容
基于智慧城市与储住分离数字家居模式垃圾处理方法
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
VSCode+mingw64+cmake
JS inheritance prototype