当前位置:网站首页>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;
}
边栏推荐
- 20排位赛3
- Difference between process and thread
- [4g/5g/6g topic foundation -147]: Interpretation of the white paper on 6G's overall vision and potential key technologies -2-6g's macro driving force for development
- 如何成为一名高级数字 IC 设计工程师(5-2)理论篇:ULP 低功耗设计技术精讲(上)
- CSDN salary increase technology - learn about the use of several common logic controllers of JMeter
- Niuke - Huawei question bank (61~70)
- Create an int type array with a length of 6. The values of the array elements are required to be between 1-30 and are assigned randomly. At the same time, the values of the required elements are diffe
- 信息安全实验一:DES加密算法的实现
- 華為HCIP-DATACOM-Core_03day
- 在EXCEL写VBA连接ORACLE并查询数据库中的内容
猜你喜欢

Integer or int? How to select data types for entity classes in ORM

第一讲:包含min函数的栈

Information Security Experiment 3: the use of PGP email encryption software

How to use clipboard JS library implements copy and cut function

Unity shader (learn more about vertex fragment shaders)

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

VSCode+mingw64

基于智慧城市与储住分离数字家居模式垃圾处理方法

Difference between interface iterator and iteratable

nlohmann json
随机推荐
CSDN salary increase technology - learn about the use of several common logic controllers of JMeter
In fact, it's very simple. It teaches you to easily realize the cool data visualization big screen
Information Security Experiment 2: using x-scanner scanning tool
20排位赛3
印象笔记终于支持默认markdown预览模式
H5 web player easyplayer How does JS realize live video real-time recording?
Huawei hcip datacom core_ 03day
How to become a senior digital IC Design Engineer (5-2) theory: ULP low power design technology (Part 1)
The configuration and options of save actions are explained in detail, and you won't be confused after reading it
Impression notes finally support the default markdown preview mode
基础篇:带你从头到尾玩转注解
数据建模中利用3σ剔除异常值进行数据清洗
Where is the answer? action config/Interceptor/class/servlet
2020浙江省赛
大佬们,请问 MySQL-CDC 有什么办法将 upsert 消息转换为 append only 消
网易云微信小程序
How will fashion brands enter the meta universe?
Lesson 1: finding the minimum of a matrix
Information Security Experiment 4: implementation of IP packet monitoring program
Unity shader (to achieve a simple material effect with adjustable color attributes only)