暑假刷题第21天--8/4

P1902 刺杀大使 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)(优先队列bfs/二分答案+bfs)

#include<iostream>
#include<queue>
using namespace std;
int n,m;
const int N=1005;
int a[N][N];
int cnt[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
bool vis[N][N];
struct node{
	int step,xx,yy;
	bool operator<(const node &xx)const
	{
		return xx.step<step;
	}
};
int in(int x,int y){
	if(x<0||y<0||x>=n||y>=m)return 0;
	return 1;
}
int bfs(){
	priority_queue<node>q;
	node c;
	c.step=0,c.xx=0,c.yy=0;
	q.push(c);
	vis[0][0]=true;
	while(!q.empty()){
		node t=q.top();
		q.pop();
		if(t.xx==n-1&&t.yy==m-1)return t.step;
		for(int i=0;i<4;i++){
			int tx=t.xx+cnt[i][0];
			int ty=t.yy+cnt[i][1];
			if(in(tx,ty)&&!vis[tx][ty]){
				node now;
				now.xx=tx,now.yy=ty,now.step=max(t.step,a[tx][ty]);
				q.push(now);
				vis[tx][ty]=true;
			}
		}
	}
	return 0;
}
int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
		}
	}
	cout<<bfs()<<endl;
} 
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int n,m;
const int N=1005;
int a[N][N];
int cnt[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
bool vis[N][N];
struct node{
	int xx,yy;
};
int in(int x,int y){
	if(x<0||y<0||x>=n||y>=m)return 0;
	return 1;
}
int bfs(int x){
	queue<node>q;
	memset(vis,0,sizeof(vis));
	node c;
	c.xx=0,c.yy=0;
	q.push(c);
	vis[0][0]=true;
	while(!q.empty()){
		node t=q.front();
		q.pop();
		if(t.xx==n-1)return 1;
		for(int i=0;i<4;i++){
			int tx=t.xx+cnt[i][0];
			int ty=t.yy+cnt[i][1];
			if(in(tx,ty)&&!vis[tx][ty]&&a[tx][ty]<=x){
				node now;
				now.xx=tx,now.yy=ty;
				q.push(now);
				vis[tx][ty]=true;
			}
		}
	}
	return 0;
}
int main(){
	cin>>n>>m;
	int l=0,r=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
			r=max(r,a[i][j]);
		}
	}
	while(r>l){
		int mid=(r+l)/2;
		if(bfs(mid)){
			r=mid;
		}
		else l=mid+1;
	}
	cout<<l<<endl;
} 

Problem - A - Codeforces(签到)

#include<iostream>
using namespace std;
void solve(){
	int a,b,c;
	cin>>a>>b>>c;
	if(a>b+c){
		cout<<2*(b+c)+1<<endl;
	}
	else {
		cout<<2*a-1<<endl;
	}
}
int main(){
	int t;
	cin>>t;
	while(t--){
		solve();
	}
}

Problem - B - Codeforces(优先队列--需熟练掌握重载运算)

#include<iostream>
#include<queue>
using namespace std;
struct node{
	int x,y;
	
};
bool operator<(const node &a,const node &b)
{
	if(a.x!=b.x)return a.x<b.x;
	return a.y>b.y;
}
void solve(){
	int n,k;
	cin>>n>>k;
	priority_queue<node>q;
	for(int i=0;i<n;i++){
		node x;
		cin>>x.x;
		x.y=i;
		q.push(x);
	}
	while(!q.empty()){
		node t=q.top();
		q.pop();
		if(t.x<=k){
			cout<<t.y+1<<' ';
		}
		else {
			t.x=t.x%k;
			if(t.x==0)t.x+=k;
			q.push(t);
		}
	}
	cout<<endl;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		solve();
	}
}

Problem - C - Codeforces(思维)

#include<iostream>
#include<queue>
#include<string>
#include<map>
#include<set>
#include<vector>
using namespace std;
const int N=200005;
void solve(){
	int n,m;
	cin>>n>>m;
	string s;
	cin>>s;
	vector<int>q(n,-1),w(n,n);
	for(int i=0;i<n;i++){
		if(i>0)q[i]=q[i-1];
		if(s[i]=='0')q[i]=i;
	}
	for(int i=n-1;i>=0;i--){
		if(i<n-1)w[i]=w[i+1];
		if(s[i]=='1')w[i]=i;
	}
	set<pair<int,int> >ans;
	while(m--){
		int l,r;
		cin>>l>>r;
		l--,r--;
		if(w[l]>q[r]){
			ans.insert({-1,-1});
		}
		else {
			ans.insert({w[l],q[r]});
		}
	}
	cout<<ans.size()<<endl;
}
int main(){
	int t;
	cin>>t;
	while(t--){
		solve();
	}
}

Problem - D - Codeforces(思维)

#include<iostream>
#include<queue>
#include<string>
#include<map>
#include<set>
#include<vector>
using namespace std;
const int N=200005;
int a[N],b[N];
bool vis[N];
void solve(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	int res=0,res2=0;
	for(int i=0;i<n;i++){
		if(a[i]==2&&!vis[i]){
			res++;
			vis[i]=true;
			for(int j=i-1;j>=0;j--){
				vis[j]=true;
				if(a[j]==0){
					break;
				}
			}
			for(int j=i+1;j<n;j++){
				vis[j]=true;
				if(a[j]==0){
					break;
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		if(a[i]==1&&!vis[i]){
			res++;
			int f=1,p=0;
			vis[i]=true;
			if(i>0&&!vis[i-1]){
				vis[i-1]=true;
				p=1;
			}
			for(int j=i+1;j<n;j++){
				if(a[j]==0&&p==1)break;
				vis[j]=true;
				if(a[j]==0)break;
			}
		}
	}
	for(int i=0;i<n;i++){
		if(!vis[i])res++;
	}
	cout<<res<<endl;
}
int main(){
	int t=1;
	//cin>>t;
	while(t--){
		solve();
	}
}

4443. 无限区域 - AcWing题库(模拟)

#include<iostream>
#include<queue>
#include<string>
#include<map>
#include<set>
#include<vector>
using namespace std;
const int N=200005;
double p=3.1415926;
int cnt=0;
void solve(){
	long long r,a,b;
	cnt++;
	cin>>r>>a>>b;
	double ans=0;
	int f=0;
	while(r!=0){
		ans+=r*r*p;
		if(f==0)r=r*a,f=1;
		else r=r/b,f=0;
	}
	printf("Case #%d: %.6lf\n",cnt,ans);
}
int main(){
	int t;
	cin>>t;
	while(t--){
		solve();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值