G. Livestock Lineup

GDUT 2020寒假训练 排位赛一 G

原题链接

题目

题目截图
Every day, Farmer John milks his 8 dairy cows, named Bessie, Buttercup, Belinda, Beatrice, Bella, Blue, Betsy, and Sue. The cows are rather picky, unfortunately, and require that Farmer John milks them in an order that respects N constraints (1≤N≤7). Each constraint is of the form “X must be milked beside Y”, stipulating that cow X must appear in the milking order either directly after cow Y or directly before cow Y.

Please help Farmer John determine an ordering of his cows that satisfies all of these required constraints. It is guaranteed that an ordering is always possible. If several orderings work, then please output the one that is alphabetically first. That is, the first cow should have the alphabetically lowest name of all possible cows that could appear first in any valid ordering. Among all orderings starting with this same alphabetically-first cow, the second cow should be alphabetically lowest among all possible valid orderings, and so on.

Input
The first line of input contains N. The next N lines each contain a sentence describing a constraint in the form “X must be milked beside Y”, where X and Y are names of some of Farmer John’s cows (the eight possible names are listed above).

Output
Please output, using 8 lines, an ordering of cows, one cow per line, satisfying all constraints. If multiple orderings work, output the one that is alphabetically earliest.

样例

input:
3
Buttercup must be milked beside Bella
Blue must be milked beside Bella
Sue must be milked beside Beatrice
output:
Beatrice
Sue
Belinda
Bessie
Betsy
Blue
Bella
Buttercup

题目大意

有八头牛排序,下面给出N个要求,每个要求中都提出了x想和y排在一起,请输出所有合法的排序方案中名字的字典序最小的

思路

枚举
用next_permutation枚举出各种可能的方案,然后对每一个方案进行检查,检查的过程就是循环判断N个要求是否都满足。
在枚举之前还进行了一下预处理,首先先把这八个名字扔到set里面得到字典序的排列,然后依据字典序从1~8建立string to int(用于输入)和int to string(用于输出) 的双映射,在枚举的时候操作的数组是{1,2,3,4,5,6,7,8}然后输出的时候用映射给映射回来就行。

数据量很小,直接暴力枚举不考虑后果,干就完了 奥利给。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<map>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
using namespace std;
map<string ,int> si;
map<int,string> is;
set<string> str;
int num[8]={1,2,3,4,5,6,7,8};
int order[10][2];
int main()
{
/*	str.insert("Bessie");
	str.insert("Buttercup");
	str.insert("Beatrice");
	str.insert("Belinda");
	str.insert("Bella");
	str.insert("Blue");
	str.insert("Betsy");
	str.insert("Sue");
	for(set<string>::iterator i=str.begin();i!=str.end();i++)
	{
		cout<<*i<<" "<<endl;
	}*/
	si["Beatrice"]=1;	is[1]="Beatrice";
	si["Belinda"]=2;	is[2]="Belinda";
	si["Bella"]=3;		is[3]="Bella";
	si["Bessie"]=4;		is[4]="Bessie";
	si["Betsy"]=5;		is[5]="Betsy";
	si["Blue"]=6;		is[6]="Blue";
	si["Buttercup"]=7;	is[7]="Buttercup";
	si["Sue"]=8;		is[8]="Sue";
	int m;
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		string str;
		cin>>str;
		order[i][0]=si[str];
		cin>>str>>str>>str>>str>>str;
		order[i][1]=si[str];
	}
	while(next_permutation(num,num+8))
	{
		bool flag=true;
		for(int i=1;i<=m;i++)
		{
			bool f=false;
			for(int j=1;j<=6;j++)
			{
				if(num[j]==order[i][0]&&(num[j-1]==order[i][1]||num[j+1]==order[i][1]))
				{
					//flag++;
					f=true;
					break;
				}
				if(num[j]==order[i][1]&&(num[j-1]==order[i][0]||num[j+1]==order[i][0]))
				{
					//flag++;
					f=true;
					break;
				}
			}
			if(f==false)
			{
				flag=false;
				break;
			}
		}
		if(flag==true)
		{
			for(int i=0;i<=7;i++)
			{
				cout<<is[num[i]]<<endl;
			}
			return 0;
		}
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:在西欧,畜牧业数量和FCH4-ruminant的减少是由一系列政策引起的:(1)欧盟通过共同农业政策(CAP)向农民提供各种激励措施,将牧场上的畜牧密度限制在每公顷1.7个畜牧单位左右。\[1\] 引用\[2\]:在这项研究中,年FCH4-ruminant是基于IPCC第二层方法计算的。与使用畜牧数量数据和默认排放因子的IPCC第一层方法相比,第二层方法使用了更详细的国家特定数据,包括总能量摄入和特定畜牧类别的甲烷转化因子,允许考虑饲料数量和质量。\[2\] 引用\[3\]:全球畜牧饲料数据,包括浓缩饲料、草料、秸秆和偶尔饲料,仅在2000年可用。根据我们的估计,2000年反刍动物消耗了总浓缩饲料的33%,比参考文献给出的值高11%。\[3\] 问题:畜牧密度是什么? 回答:畜牧密度是指单位面积上的畜牧动物数量。在西欧,通过共同农业政策的限制,畜牧密度被限制在每公顷1.7个畜牧单位左右。\[1\]畜牧密度的计算通常需要考虑畜牧数量和可用的土地面积。 #### 引用[.reference_title] - *1* *2* *3* [Revisiting enteric methane emissions from domestic ruminants and their δ](https://blog.csdn.net/weixin_39968724/article/details/117684714)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值