【比赛笔记】AtCoder Beginner Contest 265笔记
目录
D - Iroha and Haiku (New ABC Edition)
A - Apple
A - Apple https://atcoder.jp/contests/abc265/tasks/abc265_a
https://atcoder.jp/contests/abc265/tasks/abc265_a
题目描述
高桥去买苹果,已知买一个苹果需x元,买三个苹果需y元,问你买n个苹果至少需要多少钱
题目分析
这道题只需分类讨论,选择最优策略即可
题目代码
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/(gcd(a,b))*b;}
int x,y,n;
 
int main(){
	cin>>x>>y>>n;
	if(3*x>y){
		cout<<n/3*y+n%3*x<<endl;
	}	
	else{
		cout<<n*x<<endl;
	}
	return 0;
}
//ACplease!!!
B - Explore
B - Explore https://atcoder.jp/contests/abc265/tasks/abc265_b
https://atcoder.jp/contests/abc265/tasks/abc265_b
题目描述
有n个房间排成一列,起初高桥在第一个房间,且时间限额为t单位时间,从第i个房间到下一个房间需
单位时间。在这些房间内,共有m个奖励,每个奖励是在到达特定房间后获得若干单位时间限额。剩余时间限额在任何时刻都必须大于0单位时间。问你能否顺利到达最后一个房间
题目分析
这道题只需要根据题目要求进行模拟即可,并不需要太多编程基础
题目代码
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/(gcd(a,b))*b;}
int n,m;
long long t;
long long a[100005],b[100005];
int main(){
	cin>>n>>m>>t;
	for(int i=2;i<=n;i++)cin>>a[i];
	for(int i=1;i<=m;i++){
		long long x,y;
		cin>>x>>y;
		b[x]=y;
	}
	for(int i=2;i<=n;i++){
		t-=a[i];
//		cout<<t<<endl;
		if(t<=0){
			cout<<"No"<<endl;
			return 0;
		}
		t+=b[i];
	}
	cout<<"Yes"<<endl;
	return 0;
}
//ACplease!!!
C - Belt Conveyor
C - Belt Conveyor https://atcoder.jp/contests/abc265/tasks/abc265_c
https://atcoder.jp/contests/abc265/tasks/abc265_c
题目描述
给你一个h行w列的迷宫,迷宫的每一位都标注着U,D,L或者R,你一开始在第1行第1列,重复执行以下步骤:
- 如果当前位置标注的是U,位置向上移动一格
- 如果当前位置标注的是D,位置向下移动一格
- 如果当前位置标注的是L,位置向左移动一格
- 如果当前位置标注的是R,位置向右移动一格
- 若执行以上步骤后位置出界,则直接打印之前位置
如果陷入死循环,即移动次数无限,则输出-1
题目分析
这道题移动和判断出界并不难,关键是如何判断是否陷入死循环。所谓死循环,其实就是在一个区域内重复移动,因此我们只需判断每一步到达的位置是否已经来过即可,如果已经来过,那么就说明陷入死循环,直接输出-1
题目代码
#include<bits/stdc++.h>
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/(gcd(a,b))*b;}
int h,w;
char g[505][505];
bool visited[505][505];
int main(){
	cin>>h>>w;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			cin>>g[i][j];
		}
	}
	int x=1,y=1;
	while(1){
		visited[x][y]=1;
		if(g[x][y]=='U'){
			if(x==1){
				cout<<x<<' '<<y;
				return 0;
			}
			x--;
		}
		else if(g[x][y]=='D'){
			if(x==h){
				cout<<x<<' '<<y;
				return 0;
			}
			x++;
		}
		else if(g[x][y]=='L'){
			if(y==1){
				cout<<x<<' '<<y;
				return 0;
			}
			y--;
		}
		else{
			if(y==w){
				cout<<x<<' '<<y;
				return 0;
			}
			y++;
		}
		if(visited[x][y]==1){
			cout<<-1<<endl;
			return 0;
		}
	}
	return 0;
}
//ACplease!!!
D - Iroha and Haiku (New ABC Edition)
D - Iroha and Haiku (New ABC Edition) https://atcoder.jp/contests/abc265/tasks/abc265_d
https://atcoder.jp/contests/abc265/tasks/abc265_d
题目描述
给你一个长度为n的数组,问你是否存在符合以下要求的整形元组(x,y,z,w):
题目分析
这道题其实只需要用前缀和配合上二分查找便能解决,不论是用递归还是用set容器,代码量都并不大
题目代码
赛时代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/(gcd(a,b))*b;}
int n;
int res[4],a[200005],s[200005];
bool solve(int pos,int k){
	if(k==4)return 1;
	while(pos<=n){
		if(binary_search(s+pos,s+n+1,res[k]+s[pos])){
			int *p=lower_bound(s+pos,s+n+1,res[k]+s[pos]);
			if(solve(p-s,k+1)){
				return 1;
			}			
		}
		pos++;
	}
//	cout<<k<<endl;
	return 0;
}
signed main(){
	cin>>n>>res[1]>>res[2]>>res[3];
	for(int i=1;i<=n;i++){
		cin>>a[i];
		s[i]=s[i-1]+a[i];
	}
	if(solve(0,1)){
		cout<<"Yes"<<endl;
	}
	else{
		cout<<"No"<<endl;
	}
	return 0;
}
//ACplease!!!
赛后代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/(gcd(a,b))*b;}
int n,p,q,r,sum;
set <int> s({0});
signed main(){
	cin>>n>>p>>q>>r;
	for(int i=1;i<=n;i++){
		int a;
		cin>>a;
		sum+=a;
		s.insert(sum);
	}
	for(int x:s){
		if(s.find(x+p)!=s.end()&&s.find(x+p+q)!=s.end()&&s.find(x+p+q+r)!=s.end()){
			cout<<"Yes"<<endl;
			return 0;
		}
	}
	cout<<"No"<<endl;
	return 0;
}
//ACplease!!!
 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号