第十三届蓝桥杯国赛C++B组【个人代码】

代码修改过。仅作个人记录,代码仅供参考。


试题 C: 卡牌

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

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;


const int N = 2e5 + 10;
LL n, m, a[N], b[N];

priority_queue<PLL, vector<PLL>, greater<PLL> > pq;

int main()
{
	scanf("%lld%lld", &n, &m);
	for(int i = 1; i <= n; i ++ ) scanf("%lld", a + i), pq.push({a[i], i});
	for(int i = 1; i <= n; i ++ ) scanf("%lld", b + i), b[i] += a[i];
	
	while(m > 0){
		LL id = pq.top().second; pq.pop();
		if(a[id] == b[id]) break;
		a[id] ++ , m -- ;
		pq.push({a[id], id});
	}
	
	LL ans = 9e18;
	for(int i = 1; i <= n; i ++ ) ans = min(ans, a[i]);
	printf("%lld", ans);
	
	
	return 0;
}

试题 D: 最大数字

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

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 2e5 + 10;

LL n, A, B, ans;
int num[20], res[20];

int main()
{
	scanf("%lld%lld%lld", &n, &A, &B);
	int siz = 0;
	if(n == 0) siz = 1, num[0] = 0;
	else{
		LL t = n;
		while(t) num[siz] = t % 10, siz ++ , t /= 10;
	}
	
	for(int i = 0; i < (1 << siz); i ++ ){
		memcpy(res, num, sizeof res);
		LL a = A, b = B, t = 0;
		
		for(int j = siz - 1; j >= 0; j -- ){
			if(a == 0 && b == 0) break;
			if(i >> j & 1){ // 1 add 0 sub
				while(a && res[j] != 9) a -- , res[j] ++ ;
			}else{
				while(b && res[j] != 9){
					b -- , res[j] -- ;
					if(res[j] < 0) res[j] = 9;
				}
			}
		}
		
		for(int j = siz - 1; j >= 0; j -- ) t = t * 10 + res[j];
		ans = max(ans, t);
	}
	
	printf("%lld", ans);

	return 0;
}

试题 E: 出差

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

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 2e4 + 10, M = 2e4 + 10;

int n, m;
int he[N], ne[M], v[M], w[M], idx;
int pw[N];
bool st[N]; int dis[N];

void add(int a, int b, int c){
	v[idx] = b, w[idx] = c;
	ne[idx] = he[a], he[a] = idx ++ ;
}

void dij(){
	for(int i = 2; i <= n; i ++ ) dis[i] = 0x3f3f3f3f;
	priority_queue<PII, vector<PII>, greater<PII> > pq;
	pq.push({0, 1});
	
	while(pq.size()){
		int now = pq.top().second; pq.pop();
		if(st[now]) continue;
		st[now] = 1;
		for(int i = he[now]; ~i; i = ne[i]){
			if(dis[v[i]] > dis[now] + w[i] + pw[v[i]]){
				dis[v[i]] = dis[now] + w[i] + pw[v[i]];
				pq.push({dis[v[i]], v[i]});
			}
		}
	}
}

int main()
{
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i ++ ) he[i] = -1;
	for(int i = 1; i <= n; i ++ ) scanf("%d", pw + i);
	pw[1] = pw[n] = 0;
	
	while(m -- ){
		int a, b, c; scanf("%d%d%d", &a, &b, &c);
		add(a, b, c), add(b, a, c);
	}
	
	dij();
	
	printf("%d", dis[n]);

	return 0;
}

试题 F: 费用报销

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

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 1e3 + 10, M = 5e3 + 10;

int n, m, k;
vector<int> w[N];
int day[20] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dp[N][M], ans;

int cal(int m, int d){
	int s = d;
	for(int i = 1; i < m; i ++ ) s += day[i];
	return s;
}

int main()
{
	scanf("%d%d%d", &n, &m, &k);
	for(int i = 1, mo, d, v, t; i <= n; i ++ ){
		scanf("%d%d%d", &mo, &d, &v);
		if(v > m) continue;
		t = cal(mo, d);
		w[t].push_back(v);
	}
	
	for(int i = 1; i <= 365; i ++ ){
		for(int j = 0; j <= m; j ++ ) dp[i][j] = dp[i - 1][j];
		if(w[i].size()){
			for(int ww : w[i]){
				if(i <= k){
					for(int j = ww; j <= m; j ++ )
						dp[i][j] = max(dp[i][j], ww);
				}else{
					for(int j = ww; j <= m; j ++ )
						dp[i][j] = max(dp[i][j], dp[i - k][j - ww] + ww);
				}
			}
		}
		for(int j = 0; j <= m; j ++ ) ans = max(ans, dp[i][j]);
	}
	printf("%d", max(ans, dp[365][m]));

	return 0;
}

试题 G: 故障

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

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 110, M = 5e3 + 10;

int n, m, p[N], a[N][N];

bool cmp(pair<double, int> a, pair<double, int> b){
	if(a.first != b.first) return a.first > b.first;
	return a.second < b.second;
}

int main()
{
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i ++ ) scanf("%d", p + i);
	
	for(int i = 1; i <= n; i ++ )
		for(int j = 1; j <= m; j ++ ) scanf("%d", &a[i][j]);
	
	set<int> id;
	
	int num; scanf("%d", &num);
	while(num -- ){
		int x; scanf("%d", &x);
		id.insert(x);
	}
	
	vector<pair<double, int> > all;
	
	for(int i = 1; i <= n; i ++ ){
		bool f = 1;
		for(int j = 1; j <= m; j ++ )
			if(!a[i][j] && id.count(j)) f = 0;
		
		if(!f) all.push_back({0, i});
		else{
			double res = 1;
			for(int j = 1; j <= m; j ++ )
				if(a[i][j]){
					if(id.count(j)) res = res * (a[i][j] / 100.0);
					else res = res * (1 - a[i][j] / 100.0);
				}
			res = res * p[i];
			all.push_back({res, i});
		}
	}
	
	sort(all.begin(), all.end(), cmp);
	double sum = 0;
	for(auto it : all) sum += it.first;
	if(sum == 0){
		for(auto it : all) printf("%d 0.00\n", it.second);
	}else{
		for(auto it : all) printf("%d %.2f\n", it.second, it.first * 100 / sum);
	}

	return 0;
}

试题 H: 机房

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

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 1e5 + 10, M = 2e5 + 10, LG = 20;

int n, T;
int he[N], ne[M], v[M], idx;
int dep[N], fa[N][LG];
LL siz[N];

void add(int a, int b){
	v[idx] = b;
	ne[idx] = he[a], he[a] = idx ++ ;
}

void dfs(int now, int pre){
	dep[now] = dep[pre] + 1, fa[now][0] = pre;
	siz[now] += siz[pre];
	for(int i = 1; i < LG; i ++ ) fa[now][i] = fa[fa[now][i - 1]][i - 1];
	for(int i = he[now]; ~i; i = ne[i]){
		if(v[i] == pre) continue; 
		dfs(v[i], now);
	}
}

int get_lca(int x, int y){
	if(x == y) return x;
	if(dep[x] < dep[y]) swap(x, y);
	for(int i = LG - 1; i >= 0; i -- )
		if(dep[fa[x][i]] >= dep[y]) x = fa[x][i];
	
	if(x == y) return x;
	
	for(int i = LG - 1; i >= 0; i -- )
		if(fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
	return fa[x][0];
}

int main()
{
	scanf("%d %d", &n, &T);
	for(int i = 1; i <= n; i ++ ) he[i] = -1;
	for(int i = 1, a, b; i < n; i ++ ){
		scanf("%d%d", &a, &b);
		add(a, b), add(b, a);
		siz[a] ++ , siz[b] ++ ;
	}
	
	dfs(1, 0);
	
	int a, b;
	LL ans;
	while(T -- ){
		scanf("%d%d", &a, &b);
		int lca = get_lca(a, b);
		if(a == b) ans = siz[a] - siz[fa[a][0]];
		else ans = siz[a] + siz[b] - siz[lca] - siz[fa[lca][0]];
		printf("%lld\n", ans);
	}

	return 0;
}

试题 I: 齿轮

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

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 2e5 + 10, M = 5e3 + 10;

int n, r[N], T;
bool st[N], ans[N];
set<int> uni;

int main()
{
	scanf("%d %d", &n, &T);
	for(int i = 1, x; i <= n; i ++ ){
		scanf("%d", &x);
		if(uni.count(x)) ans[1] = 1;
		uni.insert(x);
		st[x] = 1;
	}
	
	for(int i = 1; i < N; i ++ ) if(st[i]){
		for(int k = 2; i * k < N; k ++ )
			if(st[i * k]) ans[k] = 1;
	}
	
	int q;
	while(T -- ){
		scanf("%d", &q);
		if(ans[q]) printf("YES\n");
		else printf("NO\n");
	}

	return 0;
}

试题 J: 搬砖

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

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int N = 110, M = 5e3 + 10;

LL n, w[N], v[N];
LL ans;

struct node {
	LL w, v, id;
	bool operator < (const node x) const{
		return id < x.id;
	}
};

void update(vector<node> & vec){
	sort(vec.begin(), vec.end());
	do{
		bool f = 1;
		LL sum = 0;
		for(int i = 0; i < vec.size(); i ++ ){
			if(i == 1) sum += vec[i].w;
			else{
				if(sum > vec[i].v){
					f = 0;
					break;
				}
				sum += vec[i].w;
			}
		}
		if(f){
			sum = 0;
			for(int i = 0; i < vec.size(); i ++ ) sum += vec[i].v;
			ans = max(ans, sum);
		}
	}while(next_permutation(vec.begin(), vec.end()));
}

int main()
{
	cin >> n;
	for(int i = 0; i < n; i ++ ) cin >> w[i] >> v[i];
	
	for(int i = 0; i < 1 << n; i ++ ){
		vector<node> vec;
		for(int j = 0; j < n; j ++ ) if(i >> j & 1) vec.push_back({w[j], v[j], j});
		update(vec);
	}
	
	cout << ans;

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值