Team Tic Tac Toe & Out of Sorts II & Family Tree

这篇博客包含了三个不同的主题:农场上的多牛井字游戏,其中介绍了如何判断游戏胜利者;Bessie改进了她的冒泡排序算法,分析了新的代码会打印“moo”的次数;最后,讨论了如何确定农场上两头奶牛之间的家庭关系。
摘要由CSDN通过智能技术生成

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Farmer John owns 26 cows, which by happenstance all have names starting with different letters of the alphabet, so Farmer John typically refers to each cow using her first initial – a character in the range A…Z.

The cows have recently become fascinated by the game of tic-tac-toe, but since they don’t like the fact that only two cows can play at a time, they have invented a variant where multiple cows can play at once! Just like with regular tic-tac-toe, the game is played on a 3×3 board, only instead of just Xs and Os, each square is marked with a single character in the range A…Z to indicate the initial of the cow who claims that square.

An example of a gameboard might be:

COW
XXO
ABC
The cows fill in each of the nine squares before they become confused about how to figure out who has won the game. Clearly, just like with regular tic-tac-toe, if any single cow has claimed an entire row, column, or diagonal, that cow could claim victory by herself. However, since the cows think this might not be likely given the larger number of players, they decide to allow cows to form teams of two, where a team of two cows can claim victory if any row, column, or diagonal consists only of characters belonging to the two cows on the team, and moreover if characters from both cows (not just one) are used in this row, column, or diagonal.

Please help the cows figure out how many individuals or two-cow teams can claim victory. Note that the same square on the game board might possibly be usable in several different claims to victory.

Input
The input consists of three lines, each of which is three characters in the range A…Z.

Output
Output should consist of two lines. On the first line, output the number of individual cows who can claim victory. On the second line, output the number of two-cow teams that could claim victory.

Example
inputCopy
COW
XXO
ABC
outputCopy
0
2
Note
In this example, no single cow can claim victory. However, if cows C and X team up, they can win via the C-X-C diagonal. Also, if cows X and O team up, they can win via the middle row.
思路:分别判断每行,每列,对角线

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll ans1=0,ans2=0;
char a[10][10];
bool win[50],win2[50][50];
void go(char x,char y,char z){
	if(x==y && y==z)
		win[x-'A']=1;
	else if(x==y && x!=z)
		win2[x-'A'][z-'A']=win2[z-'A'][x-'A']=1;
	else if(x==z && x!=y)
		win2[x-'A'][y-'A']=win2[y-'A'][x-'A']=1;
	else if(y==z && x!=y)
		win2[x-'A'][y-'A']=win2[y-'A'][x-'A']=1;
		
}
int main(){
	for(ll i=0;i<3;i++)
		scanf("%s",a[i]);
	for(ll i=0;i<3;i++){
		go(a[i][0],a[i][1],a[i][2]);
		go(a[0][i],a[1][i],a[2][i]);
	}
		go(a[0][0],a[1][1],a[2][2]);
		go(a[0][2],a[2][0],a[1][1]);
	for(ll i=0;i<26;i++)
		ans1+=win[i];
	for(ll i=0;i<26;i++)
		for(ll j=i+1;j<26;j++){
			ans2+=win2[i][j];
		}
	printf("%lld\n%lld\n",ans1,ans2);
	return 0;
}

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Keeping an eye on long term career possibilities beyond the farm, Bessie the cow has started learning algorithms from various on-line coding websites.

Her favorite algorithm thus far is “bubble sort”. Here is Bessie’s initial implementation, in cow-code, for sorting an array A of length N.

sorted = false

while (not sorted):

sorted = true

moo

for i = 0 to N-2:

  if A[i+1] < A[i]:

     swap A[i], A[i+1]

     sorted = false

Apparently, the “moo” command in cow-code does nothing more than print out “moo”. Strangely, Bessie seems to insist on including it at various points in her code.

After testing her code on several arrays, Bessie learns an interesting observation: while large elements can be pulled to the end of the array very quickly, it can take small elements a very long time to “bubble” to the front of the array (she suspects this is how the algorithm gets its name). In order to try and alleviate this problem, Bessie tries to modify her code so that it scans forward and then backward in each iteration of the main loop, so that both large and small elements have a chance to be pulled long distances in each iteration of the main loop. Her code now looks like this:

sorted = false

while (not sorted):

sorted = true

moo

for i = 0 to N-2:

  if A[i+1] < A[i]:

     swap A[i], A[i+1]

for i = N-2 downto 0:

  if A[i+1] < A[i]:

     swap A[i], A[i+1]

for i = 0 to N-2:

  if A[i+1] < A[i]

     sorted = false

Given an input array, please predict how many times “moo” will be printed by Bessie’s modified code.

Input
The first line of input contains N (1≤N≤100,000). The next N lines describe A[0]…A[N−1], each being an integer in the range 0…109. Input elements are not guaranteed to be distinct.

Output
Print the number of times “moo” is printed.

Example
inputCopy
5
1
8
5
3
2
outputCopy
2
思路:网上看的思路,树状数组,http://www.manongjc.com/article/63650.html

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll M=1e5+10;
ll n;
struct data{
	ll num,id;
	bool operator <(data x)const{return this->num<x.num;}
}a[M];
ll c[M];
ll lowbits(ll x){
	return x&(-x);
}
void upd(ll x){
	while(x<=n){
		c[x]++;
		x+=lowbits(x);
	}
}
ll query(ll x){
	ll res=0;
	while(x){
		res+=c[x];
		x-=lowbits(x);
	}
	return res;
}
int main(){
	cin>>n;
	for(ll i=1;i<=n;i++) cin>>a[i].num,a[i].id=i;
	sort(a+1,a+n+1);
	ll cnt=0,ans=1;
	for(ll i=1;i<=n;i++){
		upd(a[i].id);
		ans=max(ans,i-query(i));
	}
	cout<<ans;
	return 0;
} 

Family Tree
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Farmer John owns a family-run farm that has been passed down over several generations, with a herd of cows whose familial roots can similarly be traced back several generations on the same farm. By examining old records, Farmer John is curious how the cows in his current herd are related to each-other. Please help him in this endeavor!

Input
The first line of input contains N (1≤N≤100) followed by the names of two cows. Cow names are each strings of at most 10 uppercase letters (A…Z). Farmer John is curious about the relationship between the two cows on this line of input.

The next N lines each contain two cow names X and Y, indicating that X is the mother of Y.

Output
You should print one line of output indicating the relationship between the two cows specified on the first line of input (for simplicity, let’s call these two cows BESSIE and ELSIE for the examples below). Here are the different types of relationships that are possible:

You should output “SIBLINGS” if BESSIE and ELSIE have the same mother.
BESSIE might be a direct descendant of ELSIE, meaning that ELSIE is either the mother, grand-mother, great-grand-mother, great-great-grand-mother, etc., of BESSIE. If this is the case, you should print “ELSIE is the (relation) of BESSIE”, where (relation) is the appropriate relationship, for example “great-great-grand-mother”.
If ELSIE is a child of an ancestor of BESSIE (and ELSIE is not herself an ancestor or sister of BESSIE), then ELSIE is BESSIE’s aunt. You should output “ELSIE is the aunt of BESSIE” if ELSIE is a child of BESSIE’s grand-mother, “ELSIE is the great-aunt of BESSIE” if ELSIE is a child of BESSIE’s great-grand-mother, “ELSIE is the great-great-aunt of BESSIE” if ELSIE is a child of BESSIE’s great-great-grand-mother, and so on.
If BESSIE and ELSIE are related by any other means (i.e., if they share a common ancestor), they are cousins, and you should simply output “COUSINS”.
You should output “NOT RELATED” if BESSIE and ELSIE have no common ancestor, or neither is directly descended from the other.
The following diagram helps illustrate the relationships above, which are the only relationship types you need to consider.

Observe that some relationships like “niece” (daughter of sister) are not necessary since if BESSIE is the niece of ELSIE, then ELSIE is BESSIE’s aunt.

Example
inputCopy
7 AA BB
MOTHER AA
GGMOTHER BB
MOTHER SISTER
GMOTHER MOTHER
GMOTHER AUNT
AUNT COUSIN
GGMOTHER GMOTHER
outputCopy
BB is the great-aunt of AA
思路:码农题,分类讨论

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n;
string x,y,tmp[2][110];
struct point{
	string s;
	ll floor;
};
map<string,point> pa;
int main(){
	cin>>n>>x>>y;
	for(ll i=0;i<n;i++){
		string st1,st2;
		cin>>st1>>st2;
		if(pa[st1].s=="") pa[st1].s=st1;
		point p;
		p.s=st1;
		pa[st2]=p;
		pa[st1].floor=pa[st2].floor=-1;
	}
	string yy=x;
	pa[yy].floor=0;
	while(pa[yy].s!=yy){
		pa[pa[yy].s].floor=pa[yy].floor+1;
		yy=pa[yy].s;
		if(yy==y){
			if(pa[yy].floor==1){
				cout<<y<<" is the mother of "<<x;
				return 0;
			}
			if(pa[yy].floor>=2){
				cout<<y<<" is the ";
				for(ll i=0;i<pa[yy].floor-2;i++)
					cout<<"great-";
				cout<<"grand-mother of "<<x;
				return 0;
			}
		}
	}
	yy=y;
	ll t=0;
	while(pa[yy].s!=yy){
		yy=pa[yy].s,t++;
		if(yy==x){
			if(t==1){
				cout<<x<<" is the mother of "<<y;
				return 0;
			}
			if(t>=2){
				cout<<x<<" is the ";
				for(ll i=0;i<t-2;i++)
					cout<<"great-";
				cout<<"grand-mother of "<<y;
				return 0;
			}
		}
		if(pa[yy].floor>0){
			if(t==1){
				if(pa[yy].floor==1){
					cout<<"SIBLINGS";
					return 0;
				}
				cout<<y<<" is the ";
				for(ll i=0;i<pa[yy].floor-2;i++)
					cout<<"great-";
				cout<<"aunt of "<<x;
				return 0;
			}
			if(pa[yy].floor==1){
				cout<<x<<" is the ";
				for(ll i=0;i<t-2;i++)
					cout<<"great-";
				cout<<"aunt of "<<y;
				return 0;
			}
			if(t>1 && pa[yy].floor>1)
				cout<<"COUSINS";
			return 0;
		}
	}
	cout<<"NOT RELATED";
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值