(AtCoder Beginner Contest 341)(A - D)

比赛地址 :

Tasks - Toyota Programming Contest 2024#2(AtCoder Beginner Contest 341)

A . Print 341

模拟就好了 , 先放一个 1 , 然后放 n 个 01 ;

复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
#define int long long
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;

using namespace std;

inline void solve(){
	int n ; cin >> n ;
	cout << 1 ;
	 for(int i=0;i<n;i++){
	 	cout << "01";
	 }
}
 
signed main()
{
    IOS
    int _ = 1;
    // cin >> _;
    while(_ --) solve();
    return 0;
}

B . Foreign Exchange

贪心, 因为后面操作不会影响前面的,前面的会使后面的变大,而题目要求使最后一个最大,那么 , 直接从前往后遍历就好了 ;

复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
#define int long long
typedef long long LL;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;

using namespace std;

inline void solve() {
	int n ; cin >> n ;
	vector<int> a(n+1) ,s(n) , t(n) ;
	for(int i=1;i<=n;i++) cin >> a[i] ;
	for(int i=1;i<n;i++) cin >> s[i] >> t[i] ;
	// 第i个-si ,i+1就+ti
	for(int i=1;i<n;i++){
		if(a[i] >= s[i]){
			int k = a[i] / s[i] ;
			a[i] -= k * s[i] ;
			a[i+1] += k * t[i] ; 
		}
	}
	cout << a[n] << endl;
}	

signed main()
{
    IOS
        int _ = 1;
    // cin >> _;
    while (_--) solve();
    return 0;
}

C . TaKahashi Gets Lost

暴力 , 对于每一个点,找它是否满足题意,如果满足,则ans++;

复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define lowbit(x) (x&(-x))
#define sz(a) (int)a.size()
#define pb push_back
#define all(a) a.begin(), a.end()
#define int long long
typedef long long LL;
const int mod = 1e9+7;
const int N = 510;

char c[N][N] ;

using namespace std;

inline void solve(){
	int h,w,n;cin>>h>>w>>n ;
	string t ; cin >> t ;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			cin >> c[i][j] ;
		}
	}
	int ans = 0 ;
	for(int i=1;i<=h;i++){
		for(int j=1;j<=w;j++){
			if(c[i][j]=='#') continue ;
			int a = i , b = j ;
			bool tag = true;
			for(int k=0;k<n;k++){
				if(t[k]=='L'){
					if(b==1){
						tag = false;
						break ; 
					}
					b-=1;
					if(c[a][b]=='#'){
						tag = false;
						break;
					}
				}
				else if(t[k]=='U'){
					if(a==1){
						tag = false;
						break ; 
					}
					a-=1;
					if(c[a][b]=='#'){
						tag = false;
						break;
					}
				}
				else if(t[k]=='R'){
					if(b==w){
						tag = false;
						break ; 
					}
					b+=1;
					if(c[a][b]=='#'){
						tag = false;
						break;
					}
				}
				else{// "D"
					if(a==h){
						tag = false;
						break ; 
					}
					a+=1;
					if(c[a][b]=='#'){
						tag = false;
						break;
					}
				}
			}
			if(tag){
				ans ++ ;
			}
		}
	}
	cout << ans << endl ;
}
 
signed main()
{
    IOS
    int _ = 1;
    // cin >> _;
    while(_ --) solve();
    return 0;
}

D . Only one of two

先找到n,m的最小公倍数l,那么对于一个数x,能被n整除且<=x的数的个数就是x/n,所以可以得到下面式子(因为可能同时能被n,m整除,要删掉能被l整除的数字个数):

x/n + x/m - 2 * x/l >= k;

这样就可以使用二分来进行查找 ;

复制代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
LL gcd(LL a , LL b){
	return b ? gcd(b, a % b) : a;
}

// 设 l 是 m,n的最小公倍数 

int main() {
	long long n,m,x,k;
	cin>>n>>m>>k;
	x=(n*m)/gcd(n,m);// 求出最小公倍数 
	long long l=0,r=(long long)2e+18,mid,y;
	while((l+1)<r){// 二分查找 t/n+t/m-2*t/x>=k的最佳答案  
		mid=(l+r)/2;
		y=(mid/n)+(mid/m)-2*(mid/x);
		if(y<k)l=mid;
		else r=mid;
	}
	cout<<r<<endl;
	return 0;
}
相关推荐
lisw052 小时前
稀疏算法概述!
算法·机器人·无人机
思想觉悟3 小时前
Aosp实战之自定义开机启动进程
c++·面试
YLCHUP4 小时前
【括号匹配+dp优化】题解:双端括号(brackets)_动态规划_状态优化_数学建模_贪心_性质挖掘_STLmap_C++算法竞赛
数据结构·c++·笔记·算法·数学建模·贪心算法·动态规划
郝学胜-神的一滴5 小时前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl
geovindu9 小时前
python:Logistic Regression
开发语言·后端·python·算法
geovindu10 小时前
python: Random Forest
开发语言·后端·python·算法·随机森林
derek199711 小时前
软考软件设计师:主键、传递依赖和冗余,三问其实是一件事
c语言·c++·计算机网络·软件工程·学习方法
凌冰_11 小时前
数据结构 - 线性结构(三)
java·数据结构·算法·软件工程
拂拉氏11 小时前
【知识讲解】 数据结构与算法-树状数组的认识
数据结构·算法·树状数组
艾莉丝努力练剑11 小时前
【AI大模型接入SDK】解决Unicode码转化以及界面变形问题
前端·c++·人工智能·学习·安全·架构