1. 最大波动
#include<iostream>
using namespace std;
const int inf=999999999;
int main(){
int n,max=-inf,pre,now;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&now);
if(i!=0&&max<abs(now-pre))
max=abs(now-pre);
pre=now;
}
printf("%d\n",max);
return 0;
}
2. 火车购票
#include<iostream>
using namespace std;
int a[20],R,t;
void ins(int r){
for(int i=r;t>=1;i++)
while (a[i]<5&&t>=1) {
cout<<r*5+a[i]+1<<" ";
a[i]++;
t--;
}
}
int main(){
int n,flag=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&t);
for(int r=0;r<20;r++){
if(a[r]<5&&flag==0)
R=r;
if(a[r]<=5-t){
R=r; break;
}
}
ins(R); cout<<endl;
}
return 0;
}
4
2 5 4 2
1 2
6 7 8 9 10
11 12 13 14
3 4
3. 炉石传说
#include <iostream>
#include <vector>
#include <string>
#define OPONENT 1-player
using namespace std;
const int RED = 0;
const int BLACK = 1;
struct status {
int life, power;
status(int l, int p){life = l; power = p;}
};
vector<status> v[2];
int main()
{
int n, player=RED;
string action;
int position, attack, health;
int attacker, defender;
v[RED].push_back(status(30, 0));
v[BLACK].push_back(status(30, 0));
cin >> n;
while(n--) {
cin >> action;
if(action == "summon") {
cin >> position >> attack >> health;
v[player].insert(v[player].begin() + position, status(health, attack));
} else if(action == "attack") {
cin >> attacker >> defender;
v[player][attacker].life -= v[OPONENT][defender].power;
v[OPONENT][defender].life -= v[player][attacker].power;
if(v[player][attacker].life <= 0)
v[player].erase(v[player].begin() + attacker);
if(defender != 0 && v[OPONENT][defender].life <= 0)
v[OPONENT].erase(v[OPONENT].begin() + defender);
} else if(action == "end") {
player = OPONENT;
}
}
if(v[RED][0].life <=0) cout << -1 << endl;
else if(v[BLACK][0].life <=0) cout << 1 << endl;
else cout << 0 << endl;
cout << v[RED][0].life << endl;
cout << v[RED].size() - 1;
for(int i=1; i<(int)v[RED].size(); i++)
cout << " " << v[RED][i].life;
cout << endl;
cout << v[BLACK][0].life << endl;
cout << v[BLACK].size() - 1;
for(int i=1; i<(int)v[BLACK].size(); i++)
cout << " " << v[BLACK][i].life;
cout << endl;
return 0;
}
8
summon 1 3 6
summon 2 4 2
end
summon 1 4 5
summon 1 2 1
attack 1 2
end
attack 1 1
0
30
1 2
30
1 2
4. 交通规划
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int INT_MAX2 = ((unsigned int)(-1) >> 1);
const int MAXN = 10000;
struct edge {
int v, cost;
edge(int v2, int c){v=v2; cost=c;}
};
struct node {
int u, cost;
node(){}
node(int u2, int c){u=u2; cost=c;}
bool operator<(const node n) const {
return cost > n.cost;
}
};
vector<edge> g[MAXN+1];
priority_queue<node> q;
int dist[MAXN+1];
int cost[MAXN+1];
bool visited[MAXN+1];
void dijkstra_add(int start, int n)
{
for(int i=0; i<=n; i++) {
dist[i] = INT_MAX2;
cost[i] = INT_MAX2;
visited[i] = false;
}
dist[start] = 0;
cost[start] = 0;
q.push(node(start, 0));
node f;
while(!q.empty()) {
f = q.top();
q.pop();
int u = f.u;
if(!visited[u]) {
visited[u] = true;
int len = (int)g[u].size();
for(int i=0; i<len; i++) {
int v2 = g[u][i].v;
if(visited[v2])
continue;
int tempcost = g[u][i].cost;
int nextdist = dist[u] + tempcost;
if(dist[v2] > nextdist) {
dist[v2] = nextdist;
cost[v2] = tempcost;
q.push(node(v2, dist[v2]));
} else if(dist[v2] == nextdist)
cost[v2] = min(cost[v2], tempcost);
}
}
}
}
int main()
{
int n, m,ans=0;
cin >> n >> m;
for(int i=1; i<=m; i++) {
int u,v,cost;
cin >> u >> v >> cost;
g[u].push_back(edge(v, cost));
g[v].push_back(edge(u, cost));
}
dijkstra_add(1, n);
#ifdef DEBUG
cout << "dist : ";
for(int i=1; i<=n; i++)
cout << dist[i] << " ";
cout << endl;
cout << "cost : ";
for(int i=1; i<=n; i++)
cout << cost[i] << " ";
cout << endl;
#endif
for(int i=2; i<=n; i++)
ans += cost[i];
cout << ans << endl;
return 0;
}
4 5
1 2 4
1 3 5
2 3 2
2 4 3
3 4 2
dist : 0 4 5 7
cost : 0 4 5 2
11