USACO 2023 February Contest, Bronze

USACO 2023 February Contest, Bronze

比赛链接

1 Hungry Cow

Bessie is a hungry cow. Each day, for dinner, if there is a haybale in the barn, she will eat one haybale. Farmer John does not want Bessie to starve, so some days he sends a delivery of haybales, which arrive in the morning (before dinner). In particular, on day di, Farmer John sends a delivery of bi haybales (1≤di≤1014, 1≤bi≤109).
Compute the total number of haybales Bessie will eat during the first T days.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains N and T (1≤N≤105, 1≤T≤1014).
The next N lines each contain di and bi. It is additionally guaranteed that 1≤d1<d2<⋯<dN≤T.
OUTPUT FORMAT (print output to the terminal / stdout):
Output the number of haybales that Bessie will eat during the first T days.
Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a “long long” in C/C++).
SAMPLE INPUT:
1 5
1 2
SAMPLE OUTPUT:
2
Two haybales arrive on the morning of day 1. Bessie eats one haybale for dinner on day 1 and another haybale on day 2. On days 3…5, there are no more haybales for Bessie to eat. In total, Bessie eats 2 haybales during the first 5 days.
SAMPLE INPUT:
2 5
1 2
5 10
SAMPLE OUTPUT:
3
Two haybales arrive on the morning of day 1. Bessie eats one haybale on days 1 and 2. There are no haybales for Bessie to eat on days 3 and 4. On the morning of day 5, 10 haybales arrive. Bessie eats one haybale for dinner on day 5. In total, Bessie eats 3 haybales during the first 5 days.
SAMPLE INPUT:
2 5
1 10
5 10
SAMPLE OUTPUT:
5
10 haybales arrive on the morning of day 1. Bessie eats one haybale on days 1…4. On the morning of day 5, another 10 haybales arrive, meaning there are 16 haybales in the barn. For dinner on day 5, Bessie eats another haybale. In total, Bessie eats 5 haybales during the first 5 days.
SCORING:
Inputs 4-7: T≤105
Inputs 8-13: No additional constraints.
Problem credits: Brandon Wang

实际上,针对于每一天,只有2种可能:
1.草不够吃,Bessie挨饿,那么此时吃草的天数就是草的份数。
2.否则(也就是草够吃),那么此时吃草的天数就是两次送草间隔天数,同时也可能余下一些草。

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

int n,d[100010],b[100010],t,s;

signed main(){
	scanf("%lld%lld",&n,&t);
	for(int i=1;i<=n;++i)
		scanf("%lld%lld",&d[i],&b[i]);
	d[n+1]=t+1;//设置边界----最后一天,可以避免特判处理
	for(int i=1;d[i]<=t;++i)
		if(d[i+1]-d[i]>b[i])s+=b[i];
		else b[i+1]+=b[i]-(d[i+1]-d[i]),s+=d[i+1]-d[i];
	printf("%lld\n",s);
	return 0;
}

2 Stamp Grid

A stamp painting is a black and white painting on an N×N canvas, where certain cells are inked while others are blank. It can be described by an N×N array of characters (1≤N≤20). The ith entry of the jth column of the array is equal to * if the canvas contains ink at that square and . otherwise.
Bessie has a stamp painting that she would like to create, so Farmer John has lent her a single K×K (1≤K≤N) stamp to do this and an empty N×N canvas. Bessie can repeatedly rotate the stamp clockwise by 90∘ and stamp anywhere on the grid as long as the stamp is entirely inside the grid. Formally, to stamp, Bessie chooses integers i,j such that i∈[1,N−K+1] and j∈[1,N−K+1]; for each (i′,j′) such that 1≤i′,j′≤K, canvas cell (i+i′−1,j+j′−1) is painted black if the stamp has ink at (i′,j′). Bessie can rotate the stamp at any time between stampings. Once a canvas cell is painted black, it remains black.
Farmer John is wondering whether it’s possible for Bessie to create her desired stamp painting with his stamp. For each of T (1≤T≤100) test cases, help Farmer John answer this question.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line of input contains T, the number of test cases.
Each test case starts with an integer N followed by N lines, each containing a string of *s and .s, representing Bessie’s desired stamp painting. The next line contains K and is followed by K lines, each containing a string of *s and .s, representing Farmer John’s stamp.
Consecutive test cases are separated by newlines.
OUTPUT FORMAT (print output to the terminal / stdout):
For each test case, output “YES” or “NO” on separate lines.
SAMPLE INPUT:

4

2
**
*.
1
*

3
.**
.**
***
2
.*
**

3
...
.*.
...
3
.*.
...
...

3
**.
.**
..*
2
.*
*.

SAMPLE OUTPUT:
YES
YES
NO
YES
In the first test case, Bessie can perform the following sequence of stampings:
Stamp at (1,1)
Stamp at (1,2)
Stamp at (2,1)
In the second test case, Bessie can perform the following sequence of stampings:
Stamp at (2,2)
Stamp at (2,1)
Rotate 90∘
Rotate 90∘
Stamp at (1,2)
In the third test case, it is impossible to paint the middle cell.
In the fourth test case, Bessie can perform the following sequence of stampings:
Rotate 90∘
Stamp at (1,1)
Stamp at (1,2)
Stamp at (2,2)
Problem credits: Benjamin Qi and Claire Zhang

首先明确一点:逆时针顺时针根本不需要管。
接下来就是要明白:先印后印都是一样的,搞清楚这一点就可以枚举了。
还有:因为这里问的步数最小步数而是可不可以得到,所以只要可以印(可以印----是指不能把应该印“ . ”的地方印“ * ”)我们就尽量印。

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

int t,n,k;
char a[30][30],b[30][30],cop[30][30];

void check(int i,int j){
	for(int qwer=1;qwer<=k;++qwer)
		for(int qwe=1;qwe<=k;++qwe)
			if(a[i+qwer-1][j+qwe-1]=='.'&&b[qwer][qwe]=='*')return;
	for(int qwer=1;qwer<=k;++qwer)
		for(int qwe=1;qwe<=k;++qwe)
			if(b[qwer][qwe]=='*')cop[i+qwer-1][j+qwe-1]='*';
}//判断是否可以印并执行

void turn(){
	char ans[30][30];
	for(int i=1;i<=k;++i)
		for(int j=1;j<=k;++j)
			ans[i][j]=b[k-j+1][i];
	for(int i=1;i<=k;++i)
		for(int j=1;j<=k;++j)
			b[i][j]=ans[i][j];
}//字符数组翻转

void work(){
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			cop[i][j]='.';//经过印刷能得到的数组
	turn();
	for(int i=1;i<=n-k+1;++i)
		for(int j=1;j<=n-k+1;++j)
			check(i,j);
	turn();
	for(int i=1;i<=n-k+1;++i)
		for(int j=1;j<=n-k+1;++j)
			check(i,j);
	turn();
	for(int i=1;i<=n-k+1;++i)
		for(int j=1;j<=n-k+1;++j)
			check(i,j);
	turn();
	for(int i=1;i<=n-k+1;++i)
		for(int j=1;j<=n-k+1;++j)
			check(i,j);
	bool f=1;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			if(a[i][j]!=cop[i][j]){
				f=0;
				break;
			}
	if(f)cout<<"YES\n";
	else cout<<"NO\n";
}

signed main(){
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=1;i<=n;++i)
			for(int j=1;j<=n;++j)
				cin>>a[i][j];
		cin>>k;
		for(int i=1;i<=k;++i)
			for(int j=1;j<=k;++j)
				cin>>b[i][j];
		work();
	}
	return 0;
}

3 Watching Mooloo

Bessie likes to watch shows on Mooloo. Because Bessie is a busy cow, she has planned a schedule for the next N (1≤N≤105) days that she will watch Mooloo. Because Mooloo is a paid subscription service, she now needs to decide how to minimize the amount of money she needs to pay.
Mooloo has an interesting subscription system: it costs d+K (1≤K≤109) moonies to subscribe to Mooloo for d consecutive days. You can start a subscription at any time, and you can start a new subscription as many times as you desire if your current subscription expires. Given this, figure out the minimum amount of moonies Bessie needs to pay to fulfill her schedule.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains integers N and K.
The second line contains N integers describing the days Bessie will watch Mooloo: 1≤d1<d2<⋯<dN≤1014.
OUTPUT FORMAT (print output to the terminal / stdout):
Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a “long long” in C/C++).
SAMPLE INPUT:
2 4
7 9
SAMPLE OUTPUT:
7
Bessie buys a three-day subscription on day 7, spending d+K=3+4=7 moonies.
SAMPLE INPUT:
2 3
1 10
SAMPLE OUTPUT:
8
Bessie first buys a one-day subscription on day 1, spending d+K=1+3=4 moonies. Bessie also buys a one-day subscription on day 10, spending d+K=1+3=4 moonies. In total, Bessie spends 8 moonies.
SCORING:
Inputs 3-5: N≤10
Inputs 6-12: No additional constraints.
Problem credits: Danny Mittal

每一次都面临着两种选择:
1.与上一次连续订阅,共花费k+1+d[i]-d[i-1],相当于这一次订阅花了d[i]-d[i-1]
2.这次订阅单独算,共花k+1
比较d[i]-d[i-1]与k+1大小取最小值(贪心。局部最优就能保证全局最优,因为无论选1还是2对下一次操作都没有影响)成为这一次订阅花费。

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

int n,k,d[100010];

signed main(){
	scanf("%lld%lld",&n,&k);
	for(int i=1;i<=n;++i)
		scanf("%lld",&d[i]);
	int ans=1+k;
	for(int i=2;i<=n;++i)
		ans+=min(d[i]-d[i-1],1+k);
	printf("%lld\n",ans);
	return 0;
}
posted @ 2023-03-18 21:49  whznfy  阅读(5)  评论(0)    收藏  举报  来源