链接:http://codeforces.com/contest/1070
中文不好,题意省略
这个显然要枚举集合点,枚举之后考虑英雄走的顺序,考虑在集合点左边的英雄,其实如果当前英雄能够直接走到集合点,那么这个英雄右边的数字已经是无用的了。。因此只要从右往左维护安全的位置,判断当前英雄能否到达当前位置即可。。
/**
* ┏┓ ┏┓
* ┏┛┗━━━━━━━┛┗━━━┓
* ┃ ┃
* ┃ ━ ┃
* ┃ > < ┃
* ┃ ┃
* ┃... ⌒ ... ┃
* ┃ ┃
* ┗━┓ ┏━┛
* ┃ ┃ Code is far away from bug with the animal protecting
* ┃ ┃ 神兽保佑,代码无bug
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┃
* ┃ ┗━━━┓
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid ((x+y)/2)
#define NM 105
#define nm 20005
#define pi 3.1415926535897931
using namespace std;
const ll inf=1e9+7;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}
int n,m,_x,tot,c[NM][NM],d[NM];
bool v[NM];
struct tmp{
int x,y,i;
bool operator<(const tmp&o){return x<o.x;}
}a[NM],b[NM];
int main(){
n=read();m=read();
inc(i,1,m)a[i].x=read(),a[i].y=read(),a[i].i=i;
sort(a+1,a+1+m);
inc(i,1,n)d[i]=read();
if(m==1)return 0*printf("%d\n1\n",a[1].x);
inc(i,1,n)inc(j,i+1,n){
int s=0;
inc(k,i+1,j)s+=d[k],c[i][j]=min(c[i][j],s);
c[i][j]=-c[i][j];
}
inc(i,1,n)inc(j,1,i-1){
int s=0;
dec(k,i-1,j)s+=d[k],c[i][j]=min(c[i][j],s);
c[i][j]=-c[i][j];
}
inc(k,a[1].x,a[m].x-1){
mem(v);
tot=0;
inc(i,1,m)if(a[i].x<k)b[++tot]=a[i];
b[++tot]=tmp{k,0};
_x=tot;
dec(i,tot-1,1)inc(j,_x,tot)if(c[b[i].x][b[j].x]<=b[i].y){
inc(k,_x,j-1)v[b[k].i]=false;
v[b[i].i]++;
_x=i;
break;
}
if(_x>1)continue;
tot=0;
dec(i,m,1)if(a[i].x>k+1)b[++tot]=a[i];
b[++tot]=tmp{k+1,0};_x=tot;
dec(i,tot-1,1)inc(j,_x,tot)if(c[b[i].x][b[j].x]<=b[i].y){
inc(k,_x,j-1)v[b[k].i]=false;
v[b[i].i]++;
_x=i;
break;
}
if(_x>1)continue;
printf("%d\n",k);
dec(i,m,1)if(a[i].x<k&&v[a[i].i])printf("%d ",a[i].i);
inc(i,1,m)if(a[i].x>k&&v[a[i].i])printf("%d ",a[i].i);
inc(i,1,m)if(!v[a[i].i])printf("%d ",a[i].i);putchar('\n');
return 0;
}
return 0*printf("-1\n");
}
G. Monsters and Potions
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Polycarp is an introvert person. In fact he is so much of an introvert that he plays "Monsters and Potions" board game alone. The board of the game is a row of n
cells. The cells are numbered from 1 to n
from left to right. There are three types of cells: a cell containing a single monster, a cell containing a single potion or a blank cell (it contains neither a monster nor a potion).
Polycarp has m
tokens representing heroes fighting monsters, which are initially located in the blank cells s1,s2,…,sm
. Polycarp's task is to choose a single cell (rally point) and one by one move all the heroes into this cell. A rally point can be a cell of any of three types.
After Policarp selects a rally point, he picks a hero and orders him to move directly to the point. Once that hero reaches the point, Polycarp picks another hero and orders him also to go to the point. And so forth, until all the heroes reach the rally point cell. While going to the point, a hero can not deviate from the direct route or take a step back. A hero just moves cell by cell in the direction of the point until he reaches it. It is possible that multiple heroes are simultaneously in the same cell.
Initially the i
-th hero has hi
hit points (HP). Monsters also have HP, different monsters might have different HP. And potions also have HP, different potions might have different HP.
If a hero steps into a cell which is blank (i.e. doesn't contain a monster/potion), hero's HP does not change.
If a hero steps into a cell containing a monster, then the hero and the monster fight. If monster's HP is strictly higher than hero's HP, then the monster wins and Polycarp loses the whole game. If hero's HP is greater or equal to monster's HP, then the hero wins and monster's HP is subtracted from hero's HP. I.e. the hero survives if his HP drops to zero, but dies (and Polycarp looses) if his HP becomes negative due to a fight. If a hero wins a fight with a monster, then the monster disappears, and the cell becomes blank.
If a hero steps into a cell containing a potion, then the hero drinks the potion immediately. As a result, potion's HP is added to hero's HP, the potion disappears, and the cell becomes blank.
Obviously, Polycarp wants to win the game. It means that he must choose such rally point and the order in which heroes move, that every hero reaches the rally point and survives. I.e. Polycarp loses if a hero reaches rally point but is killed by a monster at the same time. Polycarp can use any of n
cells as a rally point — initially it can contain a monster, a potion, or be a blank cell with or without a hero in it.
Help Polycarp write a program to choose a rally point and the order in which heroes move.
Input
The first line of the input contains two integers n
and m (1≤n≤100; 1≤m≤n
) — length of the game board and the number of heroes on it.
The following m
lines describe heroes. Each line contains two integers si and hi (1≤si≤n; 1≤hi≤106), where si is the initial position and hi is the initial HP of the i-th hero. It is guaranteed that each cell si is blank. It is also guaranteed that all si
are different.
The following line contains n
integers a1,a2,…,an (−106≤aj≤106), where aj describes the i
-th cell of the game board:
- aj=0
means that the i
- -th cell is blank,
- aj<0
- means that the i-th cell contains monster with positive HP of −aj
- ,
- aj>0
- means that the i-th cell contains potion with aj
- HP.
Output
On the first line of the output print the index of the rally point cell.
On the second line print m
integers — the order in which heroes should move to the rally point. Heroes are numbered from 1 to min the order they are given in the input.
If there are multiple solutions, print any of them.
If it is impossible to find a rally point which can be reached by all heroes, print a single integer -1 in the output.
Examples
InputCopy
8 3 8 2 1 3 4 9 0 3 -5 0 -5 -4 -1 0
OutputCopy
6 3 1 2
InputCopy
1 1 1 1 0
OutputCopy
1 1
InputCopy
3 2 1 1 3 1 0 -5000 0
OutputCopy
-1
InputCopy
8 3 1 15 5 10 8 1 0 -5 -5 -5 0 -5 -5 0
OutputCopy
7 2 1 3
Note
The picture illustrates the first example: