当前位置:网站首页>Gym - 102219j kitchen plates (violent or topological sequence)
Gym - 102219j kitchen plates (violent or topological sequence)
2022-07-07 09:47:00 【moyangxian】
The question : A little
. :
Practice one : A topological sort . Five plates are equivalent to a digraph with five points ,A<B amount to A One side points to B. Then do it again after drawing based on BFS Topology sorting of .
Time complexity 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;
}
Practice two :
violence .next_permutation() Work out "ABCDE" The whole arrangement , Then check whether the arrangement meets the conditions of the topic .
Time complexity 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;
}
边栏推荐
- Can flycdc use SqlClient to specify mysqlbinlog ID to execute tasks
- Binary tree high frequency question type
- 如何使用clipboard.js库实现复制剪切功能
- flinkcdc采集oracle在snapshot阶段一直失败,这个得怎么调整啊?
- How to solve the problem of golang select mechanism and timeout
- 数据库多表关联查询问题
- 消费互联网的产业链其实是很短的,它仅仅承接平台上下游的对接和撮合的角色
- PLC信号处理系列之开关量信号防抖FB
- The difference between viewpager2 and viewpager and the implementation of viewpager2 in the rotation chart
- 进程和线程的区别
猜你喜欢

iNFTnews | 时尚品牌将以什么方式进入元宇宙?

章鱼未来之星获得25万美金奖励|章鱼加速器2022夏季创业营圆满落幕

沙龙预告|GameFi 领域的瓶颈和解决方案

Binary tree high frequency question type

Netease Cloud Wechat applet

基础篇:带你从头到尾玩转注解

esp8266使用TF卡并读写数据(基于arduino)

JS逆向教程第一发

H5 web player easyplayer How does JS realize live video real-time recording?

Unity3d interface is embedded in WPF interface (mouse and keyboard can respond normally)
随机推荐
Natapp intranet penetration
JS judge whether checkbox is selected in the project
IIS faked death this morning, various troubleshooting, has been solved
2016 CCPC Hangzhou Onsite
Vs2013 generate solutions super slow solutions
How to use clipboard JS library implements copy and cut function
How to speed up video playback in browser
sql 里面使用中文字符判断有问题,哪位遇到过?比如value&lt;&gt;`无`
如何成为一名高级数字 IC 设计工程师(5-3)理论篇:ULP 低功耗设计技术精讲(下)
Octopus future star won a reward of 250000 US dollars | Octopus accelerator 2022 summer entrepreneurship camp came to a successful conclusion
基础篇:带你从头到尾玩转注解
高斯消元
哈夫曼编码压缩文件
csdn涨薪技术-浅学Jmeter的几个常用的逻辑控制器使用
thinkphp3.2信息泄露
战略合作|SubQuery 成为章鱼网络浏览器的秘密武器
Information Security Experiment 2: using x-scanner scanning tool
Impression notes finally support the default markdown preview mode
【frida实战】“一行”代码教你获取WeGame平台中所有的lua脚本
flinkcdc 用sqlclient可以指定mysqlbinlog id执行任务吗