2024暑假集训第四次考试(终极测试)

作者的话

        虽然这是最后一次考试,也是10天暑假集训的终极测试,但是题目难度反而没那么高,这里的难度是思考深度,但是主要是广范围的考所学知识的简单应用(也就是基本都是模版题的应用,只不过知识面广,难度不大),并且也取得了不错的成绩,废话不多说,look look题吧!!

3007. Buying Feed, II

思路分析

        模拟+一点点贪心的思维

        这道题需要模拟+贪心的思维,但是不难,可以这样理解就是把每个商店的食物单价+运费,就是实际的一份食物的价格,应为题目说“如果他的车上有 X 份食物。每走一里就花费 X 元。”

所以大可以看成每份食物的运费是每里1元钱,那么对于某一家商店的运费便是(终点总距离-商店的位置)*1,加上食物单价,便是实际价格,价格从小到大排序就好了,模拟一下就可以算出最小总价了。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

long long sum = 0;
const int N = 1e5+10;
struct Node{
	long long x,f, c; 
} a[N];


bool cmp(Node a,Node b){
	return a.c < b.c;
}

int main(){
	long long k,e,n; cin >> k >> e >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i].x  >> a[i].f >> a[i].c;
		a[i].c += (e-a[i].x);
	}
	sort(a+1,a+n+1,cmp);
	for(int i = 1; i <= n; i++){
		if(k == 0) break;
		if(a[i].f >= k){
			sum += k*a[i].c;
			k = 0;
		}
		else{
			sum += a[i].f*a[i].c;
			k -= a[i].f;
		}
	}
	cout << sum << endl;
	return 0;
} 

3006. Tea Time

思路分析

        并查集模版题。

        这个就很像那个并查集的模版题ybt上的亲戚那道题。就不多说了,如果你会并查集的查询和合并操作就一定会做这道题。

代码

#include<iostream>
using namespace std;

const int N = 1e4+10;
int a[N];

int find(int x){
	if(a[x] != x) a[x] = find(a[x]);
	return a[x];
}

void mer(int x,int y){
	a[find(x)] = find(y);
}

int main(){
	int n,m,q; cin >> n >> m >> q;
	for(int i = 1; i <= n; i++){
		a[i] = i;
	}
	for(int i = 1; i <= m; i++){
		int x,y; cin >> x >> y;
		mer(x,y);
	}
	for(int i = 1;  i <= q; i++){
		int x,y; cin >> x >> y;
		if(find(x) == find(y)) cout << "Y" << endl;
		else cout << "N" << endl;
	}
	return 0;
}

3009. Hide and Seek

思路分析

        图论最短路径题。

        模版的Dijkstra最短路径算法的题,按照题意输出即可。

代码

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

typedef pair<int,int> PII;
const int N = 1e5*2+10;
int h[N],e[N],ne[N],idx,w[N],dist[N],n,m,s[N];

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

void dijkstra(int st){
	memset(dist,0x3f,sizeof dist);
	dist[st] = 0;
	priority_queue<PII,vector<PII>,greater<PII> > q;
	q.push({dist[st],st});
	while(!q.empty()){
		auto t = q.top();
		q.pop();
		int k = t.second,dis = t.first;
		if(s[k]) continue;
		s[k] = 1;
		for(int i = h[k]; i != -1; i = ne[i]){
			int j = e[i];
			if(!s[j]){
				if(dis+w[i] < dist[j]){
					dist[j] = dis+w[i];
					q.push({dist[j],j});
				}
			}
		}
	}
}

int main(){
	memset(h,-1,sizeof h);
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		int a,b; cin >> a >> b;
		add(a,b,1); add(b,a,1);
	} 
	dijkstra(1);
	int id = 1e9,mn = 1e9,mx = -1e9,cnt = 0;
	for(int i = 2; i <= n; i++){
		if(mx < dist[i]){
			mx = dist[i];
			id = i;
		}
	}
	for(int i = 1; i <= n; i++){
		if(dist[i] == mx) cnt++;
	}
	cout << id << " " << mx << " " << cnt << endl;
	return 0;
}

3010. Cow Frisbee Team

代码思路

        这道题是一道类似01背包的DP题。也是最难的一道题。

        这道题我并没写出正解,而是直接打的dfs暴力,可惜TLE只能拿道60分,不过也也让我发现了“暴力果然是万能钥匙”  不会的题打暴力居然可以拿到超过一半分。展示一下我的暴力代码,暴力太6了,如果会正解也肯定写正解呀!!

#include<iostream>
#include<cstdio>
using namespace std;

int a[2010],b[2010];
const int mod = 1e8;
int n,f; 
long long cnt = 0;

void dfs(int x,int t,int s,int idx){
	if(s%f == 0 && s != 0){
		cnt++;
		cnt %= mod;
//		for(int i = 0; i < x; i++) cout << b[i] << " ";
//		cout << endl; 
	}
	if(x == n){
		return ;
	}
	for(int i = idx+1; i <= n; i++){
		b[x] = a[i];
		dfs(x+1,t,s+a[i],i);
//		b[i] = 0;
	}
}

int main(){
	cin >> n >> f;
	for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
	dfs(0,0,0,0);
	cout << cnt << endl;
	return 0;
}

正解的话直接看题解吧:  登录 - Luogu Spilopelia

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值