当前位置:网站首页>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;
}
边栏推荐
- 【frida实战】“一行”代码教你获取WeGame平台中所有的lua脚本
- Selenium+bs4 parsing +mysql capturing BiliBili Tarot data
- How to become a senior digital IC Design Engineer (5-3) theory: ULP low power design technology (Part 2)
- CodeForces - 1324D Pair of Topics(二分或双指针)
- 沙龙预告|GameFi 领域的瓶颈和解决方案
- Gym - 102219J Kitchen Plates(暴力或拓扑序列)
- Unity shader (data type in cghlsl)
- C# XML的应用
- JMeter JDBC batch references data as input parameters (the simplest method for the whole website)
- How to use clipboard JS library implements copy and cut function
猜你喜欢
小程序弹出半角遮罩层
Diffusion模型详解
细说Mysql MVCC多版本控制
VSCode+mingw64+cmake
Esp8266 uses TF card and reads and writes data (based on Arduino)
NATAPP内网穿透
【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform
Unity3d interface is embedded in WPF interface (mouse and keyboard can respond normally)
随机推荐
请教个问题,我用sql-client起了个同步任务,从MySQL同步到ADB,历史数据有正常同步过去
What development models did you know during the interview? Just read this one
Database multi table Association query problem
Mysql:select ... for update
liunx命令
Unity shader (to achieve a simple material effect with adjustable color attributes only)
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
第十四次试验
ViewPager2和VIewPager的區別以及ViewPager2實現輪播圖
进程间的通信方式
js逆向教程第二发-猿人学第一题
VSCode+mingw64
How to solve the problem of golang select mechanism and timeout
CSDN salary increase technology - learn about the use of several common logic controllers of JMeter
How to use clipboard JS library implements copy and cut function
Niuke - Huawei question bank (61~70)
20排位赛3
csdn涨薪技术-浅学Jmeter的几个常用的逻辑控制器使用
Dynamics 365online applicationuser creation method change
高斯消元