USACO 2023 January Contest, Bronze

USACO 2023 January Contest, Bronze

这一次比赛没有中文版题目(欲哭无泪)……!!?

T1.Leaders

Farmer John has N cows (2≤N≤105). Each cow has a breed that is either Guernsey or Holstein. As is often the case, the cows are standing in a line, numbered 1…N in this order.
Over the course of the day, each cow writes down a list of cows. Specifically, cow i’s list contains the range of cows starting with herself (cow i) up to and including cow Ei (i≤Ei≤N).
FJ has recently discovered that each breed of cow has exactly one distinct leader. FJ does not know who the leaders are, but he knows that each leader must have a list that includes all the cows of their breed, or the other breed’s leader (or both).
Help FJ count the number of pairs of cows that could be leaders. It is guaranteed that there is at least one possible pair.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains N.
The second line contains a string of length N, with the ith character denoting the breed of the ith cow (G meaning Guernsey and H meaning Holstein). It is guaranteed that there is at least one Guernsey and one Holstein.
The third line contains E1…EN.
OUTPUT FORMAT (print output to the terminal / stdout):
Output the number of possible pairs of leaders.
SAMPLE INPUT:
4
GHHG
2 4 3 4
SAMPLE OUTPUT:
1
The only valid leader pair is (1,2). Cow 1’s list contains the other breed’s leader (cow 2). Cow 2’s list contains all cows of her breed (Holstein).
No other pairs are valid. For example, (2,4) is invalid since cow 4’s list does not contain the other breed’s leader, and it also does not contain all cows of her breed.
SAMPLE INPUT:
3
GGH
2 3 3
SAMPLE OUTPUT:
2
There are two valid leader pairs, (1,3) and (2,3).
SCORING
Inputs 3-5: N≤100
Inputs 6-10: N≤3000
Inputs 11-17: No additional constraints.
Problem credits: Mythreya Dharani

翻译过来大概是:每头牛有一个名单(用当前序号i到给定数字e_i一段连续的牛表示),如果他含有自己种类的所有牛的名单(下面称1种领导)或者有另一种牛的领导名单(下面称2种领导)(或者都有)他就是领导。
经过深入分析,只有第一头出现的H和G牛才有可能是1种领导。
而只有序号在他们之前的异种奶牛才有可能成为2种奶牛。
这题测试数据似乎出错4个点,但是又好像现在改对了。(错误原因好像是有一头奶牛他既是1也是2种奶牛,但是根据定义只能算一次,这是一个可以检验出错误的样例:
5
GHGHH
4 5 3 5 5
上述样例的第一头G牛他既是1也是2种牛,所以可能被算重)
真正的正确代码,一开始错4个点,现在过了。

#include <bits/stdc++.h>
using namespace std;

int n,e[100010],ansg,ans,ansh,lg[100010],lh[100010],vh[100010],g,h,G,H;
char ch[100010];

int main(){
	cin>>n;
	for(int i=1;i<=n;++i)
		cin>>ch[i];
	for(int i=n;i>=1;--i){
		if(ch[i]=='G'){
			g=i;
			break; 
		}
	}
	for(int i=n;i>=1;--i){
		if(ch[i]=='H'){
			h=i;
			break;
		}
	}
	for(int i=1;i<=n;++i){
		if(ch[i]=='G'){
			G=i;
			break;
		}
	}
	for(int i=1;i<=n;++i){
		if(ch[i]=='H'){
			H=i;
			break;
		}
	}
	for(int i=1;i<=n;++i)
		cin>>e[i];
	if(e[G]>=g)++ansg;
	else if(e[H]>=h&&e[G]>=H&&G<=H)++ans;//注意2种牛只能和特定的牛配对
	if(e[H]>=h)++ansh;
	else if(e[G]>=g&&e[H]>=G&&H<=G)++ans;
	if(e[G]>=g){
		for(int i=1;i<G;++i){
			if(i==H)continue;
			if(ch[i]=='H'&&e[i]>=G)++ans;
		}
	}
	if(e[H]>=h){
		for(int i=1;i<H;++i){
			if(i==G)continue;
			if(ch[i]=='G'&&e[i]>=H)++ans;
		}
	}
	cout<<ansg*ansh+ans<<endl;
	return 0;
}

T2.Air Cownditioning II

With the hottest recorded summer ever at Farmer John’s farm, he needs a way to cool down his cows. Thus, he decides to invest in some air conditioners.
Farmer John’s N cows (1≤N≤20) live in a barn that contains a sequence of stalls in a row, numbered 1…100. Cow i occupies a range of these stalls, starting from stall si and ending with stall ti. The ranges of stalls occupied by different cows are all disjoint from each-other. Cows have different cooling requirements. Cow i must be cooled by an amount ci, meaning every stall occupied by cow i must have its temperature reduced by at least ci units.
The barn contains M air conditioners, labeled 1…M (1≤M≤10). The ith air conditioner costs mi units of money to operate (1≤mi≤1000) and cools the range of stalls starting from stall ai and ending with stall bi. If running, the ith air conditioner reduces the temperature of all the stalls in this range by pi (1≤pi≤106). Ranges of stalls covered by air conditioners may potentially overlap.
Running a farm is no easy business, so FJ has a tight budget. Please determine the minimum amount of money he needs to spend to keep all of his cows comfortable. It is guaranteed that if FJ uses all of his conditioners, then all cows will be comfortable.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line of input contains N and M.
The next N lines describe cows. The ith of these lines contains si, ti, and ci.
The next M lines describe air conditioners. The ith of these lines contains ai, bi, pi, and mi.
For every input other than the sample, you can assume that M=10.
OUTPUT FORMAT (print output to the terminal / stdout):
Output a single integer telling the minimum amount of money FJ needs to spend to operate enough air conditioners to satisfy all his cows (with the conditions listed above).
SAMPLE INPUT:
2 4
1 5 2
7 9 3
2 9 2 3
1 6 2 8
1 2 4 2
6 9 1 5
SAMPLE OUTPUT:
10
One possible solution that results in the least amount of money spent is to select those that cool the intervals [2,9], [1,2], and [6,9], for a cost of 3+2+5=10.
Problem credits: Aryansh Shrivastava and Eric Hsu

大概意思是有空调,数据提供发动成本和效果,但是是一次性的,请你算出最优方案(既满足奶牛需求,又要使花费降到最低)
这题数据量非常小,就是题目有些绕人,可以直接爆搜。

#include <bits/stdc++.h>
using namespace std;

int a[20],b[20],vhash[20],ans=1e9,cl[30],s[30],t[30],n,m,c[20],p[20],vh[110];

void dfs(int s){
	if(s>ans)return;
	bool f=1;
	for(int i=1;i<=100;++i)
		if(vh[i]>0){
			f=0;
			break;
		}
	if(f){
		ans=s;
		return;
	}
	for(int i=1;i<=m;++i){
		if(vhash[i])continue;//只能使用一次
		bool t=1;
		for(int j=a[i];j<=b[i];++j)
			if(vh[j]>0){
				t=0;
				break;
			}
		if(!t){
			for(int j=a[i];j<=b[i];++j)
				vh[j]-=p[i];//用vh数组标记温度
			vhash[i]=1;
			dfs(s+c[i]);
			for(int j=a[i];j<=b[i];++j)
				vh[j]+=p[i];
			vhash[i]=0;
		}
	}
}

int main(){
	cin>>n>>m;
	for(int i=1;i<=n;++i){
		cin>>s[i]>>t[i]>>cl[i];
		for(int j=s[i];j<=t[i];++j)
			vh[j]=cl[i];
	}
	for(int i=1;i<=m;++i){
		cin>>a[i]>>b[i]>>p[i]>>c[i];
	}	
	dfs(0);
	cout<<ans<<endl;
	return 0;
}

T3.Moo Operations

Because Bessie is bored of playing with her usual text string where the only characters are ‘C,’ ‘O,’ and ‘W,’ Farmer John gave her Q new strings (1≤Q≤100), where the only characters are ‘M’ and ‘O.’ Bessie’s favorite word out of the characters ‘M’ and ‘O’ is obviously “MOO,” so she wants to turn each of the Q strings into “MOO” using the following operations:
Replace either the first or last character with its opposite (so that ‘M’ becomes ‘O’ and ‘O’ becomes ‘M’).
Delete either the first or last character.
Unfortunately, Bessie is lazy and does not want to perform more operations than absolutely necessary. For each string, please help her determine the minimum number of operations necessary to form “MOO” or output −1 if this is impossible.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line of input contains the value of Q.
The next Q lines of input each consist of a string, each of its characters either ‘M’ or ‘O’. Each string has at least 1 and at most 100 characters.
OUTPUT FORMAT (print output to the terminal / stdout):
Output the answer for each input string on a separate line.
SAMPLE INPUT:
3
MOMMOM
MMO
MOO
SAMPLE OUTPUT:
4
-1
0
A sequence of 4 operations transforming the first string into “MOO” is as follows:
Replace the last character with O (operation 1)
Delete the first character (operation 2)
Delete the first character (operation 2)
Delete the first character (operation 2)
The second string cannot be transformed into “MOO.” The third string is already “MOO,” so no operations need to be performed.
SCORING:
Inputs 2-4: Every string has length at most 3.
Inputs 5-11: No additional constraints.
Problem credits: Aryansh Shrivastava

因为他只能删除和修改两端,所以我们必须在整个字符串中找到一个长度为3的字符串来修改并且尽可能找修改次数少的。然后自己模拟一下就可以了。

#include <bits/stdc++.h>
using namespace std;

string s;
int n;

void work(string s){
	if(s.find("MOO")!=-1)cout<<s.size()-3<<endl;
	else if(s.find("MOM")!=-1)cout<<s.size()-2<<endl;
	else if(s.find("OOO")!=-1)cout<<s.size()-2<<endl;
	else if(s.find("OOM")!=-1)cout<<s.size()-1<<endl;
	else cout<<-1<<endl;
}

int main(){
	cin>>n;
	for(int i=1;i<=n;++i){
		cin>>s;
		work(s);
	}
	return 0;
}
posted @ 2023-02-12 09:35  whznfy  阅读(12)  评论(0)    收藏  举报  来源