CSP 2016-9

本文探讨了五种信息技术问题的解决方案:最大波动用O(n)算法、火车购票策略、炉石传说中的随从操作、交通规则最短路树和Dijkstra算法应用。通过代码实例展示了如何优雅地解决这些问题并利用哈希表、数据结构等技术。
摘要由CSDN通过智能技术生成

1.最大波动
o(n) 模拟即可

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 1000 
int a[N];

int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++)    cin>>a[i];
    int ans=0;
    for(int i=1;i<n;i++) ans=max(ans,(int)fabs(a[i]-a[i-1]));
    cout<<ans;
}

2.火车购票
自己写的:思路比较混乱有点拉

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int a[20];
int main(){
	for(int i=0;i<20;i++)
	a[i]=5;
	
    int n;
  	cin>>n;
  	
  	while(n--){
  	int x;
  	cin>>x;
  	int flag=1;
	for(int i=0;i<20;i++){
		if(a[i]>=x){
			flag=0;
			for(int j=0;j<x;j++)
			cout<<i*5+j+5-a[i]+1<<" ";
			cout<<endl;
			a[i]-=x;
			break;
		}
	}
	if(flag){
		for(int i=0;x&&i<20;i++){
			while(x&&a[i]){
				x--;				
				cout<<i*5+5-a[i]+1<<" ";
				a[i]--;
			}	
		}
		cout<<endl;
	}
	}
}

y总用的哈希表,感觉还不如我的呢,就不贴了吧(●’◡’●)
3.炉石传说
锅炉传说,爷青回属于是
用vector的insert和erase来实现随从召唤和死亡操作
多练模拟题,多学着优雅的解决模拟题

#include<iostream>
#include<vector>
using namespace std;
struct node{//随从 
	int a,h;
};
class Person{
	public:
	int h;//玩家当前血量
	vector<node> vec;
	Person(){
		h=30;
		vec.push_back((node){0,0});
	}
	void summon(){
		int pos,hea,atc;
		cin>>pos>>atc>>hea;
		vec.insert(vec.begin()+pos,(node){atc,hea});
	}
	void attack(Person *o){
		int from,to;
		cin>>from>>to;
		if(to==0){
			o->h-=vec[from].a;
		}else{
			o->vec[to].h-=vec[from].a;
			vec[from].h-=o->vec[to].a;
			if(o->vec[to].h<=0) o->vec.erase(o->vec.begin()+to);
			if(vec[from].h<=0) vec.erase(vec.begin()+from);
		}
	}
};
int main(){
	Person a,b;
	Person *p=&a,*q=&b;
	int n;
	cin>>n;
	while(n--){
		string order;
		cin>>order;
		if(order=="summon")
		p->summon();
		else if(order=="attack")
		p->attack(q);
		else swap(p,q);
	}
	
	if(a.h<=0) cout<<-1<<endl;
	if(b.h<=0) cout<<1<<endl;
	if(a.h>0&&b.h>0) cout<<0<<endl;
	
	cout<<a.h<<endl;
	cout<<a.vec.size()-1<<" ";
	for(int i=1;i<a.vec.size();i++){
		cout<<a.vec[i].h<<" ";
	}
	cout<<endl;
	
	cout<<b.h<<endl;
	cout<<b.vec.size()-1<<" ";
	for(int i=1;i<b.vec.size();i++){
		cout<<b.vec[i].h<<" ";
	}
	cout<<endl;
}

4.交通规则
根据Y总的证明以及思路写的代码:
最短路树算法思路总结:
1.先求出源点到其他点的最短路,用dis数组来存
2.依次枚举各点,对于每个点,遍历他的所有边,若该边的终点到源点的最短距离+该边的权值=这个点到源点的最短距离,则将其与一个值取最小值。得到最小值后,则认为该最小值就是最短路树的其中一条边。
3.枚举完所有点后,就得到了最短路树。

#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>

using namespace std;
#define INF 0x3f3f3f3f
#define N 10010
#define M 100010
struct edge{
	int to,v;
	bool operator<(const edge &nd)const{
	return v>nd.v;
	}
};
vector<edge> G[N];
int n,m;
#define ll long long
int dis[N];

void dijistra(){	
	memset(dis,INF,sizeof(dis));
	priority_queue<edge> q;
	q.push((edge){1,0});
	
	while(q.size()){
		edge top=q.top();
		q.pop();
		
		if(dis[top.to]!=INF) continue;
		//cout<<"top "<<top.to<<"  "<<top.v<<endl;
		dis[top.to]=top.v;
		
		for(int i=0;i<G[top.to].size();++i){
			q.push((edge){G[top.to][i].to,G[top.to][i].v+top.v});
		}	
	}
}

int main(){
	cin>>n>>m;
	while(m--){
		int a,b,c;
		cin>>a>>b>>c;
		G[a].push_back((edge){b,c});
		G[b].push_back((edge){a,c});
	}
	
	dijistra();
	
	int res=0;
	for(int a=2;a<=n;a++){
		int minw=INF;
		for(int j=0;j<G[a].size();j++){
			int bv=G[a][j].v,bt=G[a][j].to;
			if(dis[bt]+bv==dis[a]){
				minw=min(minw,bv);
			}
		}
			res+=minw;	
	}		
	cout<<res;

}

5.基本不用听了,反之也不回(●’◡’●)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值