BestCoder Round #42

2024-06-12 17:38
文章标签 round 42 bestcoder

本文主要是介绍BestCoder Round #42,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

第一次打bc,也是我第一次打这种比赛,做出来三道不过system test时挂掉一道。原来比赛是这么的好玩。。。总的来说,还是有很多收获的

首先我感觉,我的手速还是有了那么一点提升的

A题水题代码略

B题变形二分

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAX 100010
using namespace std;int n,m,shoot;struct node{int id,value,shooted;bool operator<(node other)const{if(value<other.value)return 1;else if(value>other.value)return 0;else{if(id<other.id)return 1;elsereturn 0;}}
}bird[MAX];int bsearch(int high){int front=0,rear=n-1;if(high<bird[0].value||high>bird[n-1].value)return -1;int mid;while(front<rear){mid=(front+rear)/2;if(bird[mid].value<high)front=mid+1;else if(bird[mid].value>high)rear=mid;else if(bird[mid].value==high){if(bird[mid].shooted==0)rear=mid;else if(bird[mid].shooted==1)front=mid+1;}}if(bird[rear].value==high&&bird[rear].shooted==0){bird[rear].shooted=1;return bird[rear].id;}elsereturn -1;
}int main(){while(scanf("%d %d",&n,&m)!=EOF){for(int i=0;i<n;i++){scanf("%d",&bird[i].value);bird[i].id=i+1,bird[i].shooted=0;}sort(bird,bird+n);//for(int i=0;i<n;i++)//	cout<<bird[i].id<<' '<<bird[i].value<<endl;for(int i=0;i<m;i++){scanf("%d",&shoot);printf("%d\n",bsearch(shoot));;}}return 0;
}
C题让我收获最大,首先第一遍写的时候忽略了or just leave it alone. 这一句,导致一直wa,后来发现了这点,交上去tle,仔细检查后发现,dfs的时候,存在多余遍历,改了之后又是wa,再仔细检查,发现原来是受最开始的思路影响,初始化的时候有问题,这样才暂时ac,可最后system test的时候我傻眼了,又是tle,最后百思不得其解,发现只剩下一种可能性:记忆化搜索比递推慢,于是赛后又写了发递推才最终ac,才发现递推能比记忆化快这么多,以后还是能递推就递推吧。真是一波三折锕。。。

记忆化 tle代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define MAX 101
using namespace std;int n,m,k,vectors[2][2]={{0,1},{1,0}};
int grid[MAX][MAX],dp[MAX][MAX][MAX];void init(){memset(dp,0,sizeof(dp));dp[n][m][0]=1;if(grid[n][m]<=k)dp[n][m][grid[n][m]]=1;
}void dfs(int a,int b,int c){if(a==n&&b==m){return ;}int x,y;for(int i=0;i<2;i++){x=a+vectors[i][0],y=b+vectors[i][1];if(x>=1&&x<=n&&y>=1&&y<=m){if(dp[x][y][c]==0)dfs(x,y,c);if(dp[x][y][c]==1){dp[a][b][c]=1;goto a;}if(c-grid[a][b]>=0){if(dp[x][y][c-grid[a][b]]==0)dfs(x,y,c-grid[a][b]);if(dp[x][y][c-grid[a][b]]==1){dp[a][b][c]=1;goto a;}}}}
a:{}
}int main(){while(cin>>n>>m>>k){for(int i=1;i<=n;i++){			for(int j=1;j<=m;j++){scanf("%d",&grid[i][j]);}}init();for(int i=k;i>=0;i--){dfs(1,1,i);if(dp[1][1][i]==1){cout<<i<<endl;goto next;		}}
next:{}	}return 0;
}


递推ac代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAX 102
using namespace std;int n,m,k,dp[MAX][MAX][MAX];
int s[MAX][MAX];void init(){memset(dp,0,sizeof(dp));
}int main(){while(scanf("%d %d %d",&n,&m,&k)!=EOF){for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)scanf("%d",&s[i][j]);}init();for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){for(int t=0;t<=k;t++){dp[i][j][t]=max(dp[i][j-1][t],dp[i-1][j][t]);if(t-s[i][j]>=0)dp[i][j][t]=max(dp[i][j-1][t-s[i][j]]+s[i][j],max(dp[i-1][j][t-s[i][j]]+s[i][j],dp[i][j][t]));}}}int ans=0;for(int i=k;i>=0;i--)ans=max(ans,dp[n][m][i]);printf("%d\n",ans);}return 0;
}


这篇关于BestCoder Round #42的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1054885

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

数据库系统 第42节 数据库索引简介

数据库索引是数据库表中一个或多个列的数据结构,用于加快数据检索速度。除了基础的B-Tree索引,其他类型的索引针对特定的数据类型和查询模式提供了优化。以下是几种不同类型的索引及其使用场景的详细说明和示例代码。 1. 位图索引 (Bitmap Index) 位图索引适用于具有少量不同值的列(例如性别、国家代码等),它使用位图来表示数据,从而提高查询效率。 适用场景:当列中的值域较小,且数据分布

【代码随想录训练营第42期 续Day52打卡 - 图论Part3 - 卡码网 103. 水流问题 104. 建造最大岛屿

目录 一、做题心得 二、题目与题解 题目一:卡码网 103. 水流问题 题目链接 题解:DFS 题目二:卡码网 104. 建造最大岛屿 题目链接 题解:DFS  三、小结 一、做题心得 也是成功补上昨天的打卡了。 这里继续图论章节,还是选择使用 DFS 来解决这类搜索问题(单纯因为我更熟悉 DFS 一点),今天补卡的是水流问题和岛屿问题。个人感觉这一章节题对于刚

MemSQL Start[c]UP 2.0 - Round 1A(构造)

题目链接:http://codeforces.com/problemset/problem/452/A 解题思路: 打个表暴力查找匹配。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <complex>#include <cstdio>#include <strin

Codeforces Round #281 (Div. 2)A(构造+暴力模拟)

题目链接:http://codeforces.com/problemset/problem/493/A 解题思路: 暴力的判断,分三种情况去判断即可。注意如果之前已经被罚下场后,那么在后面的罚下情况不应该算在输出结果内。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <co

Codeforces Round #182 (Div. 2)A(水题)

题目链接:http://codeforces.com/contest/302/problem/A 解题思路: 只要通过重新排列使区间内和为0即是1,否则是0. 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <complex>#include <cstdio>#inc