#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn=105;
int N,M,T;
struct data{ int x,y; }A[maxn],B[maxn];
void _scanf(int &x)
{
x=0;
char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
}
void data_in()
{
_scanf(T);
_scanf(N);
for(int i=1;i<=N;i++)
{
_scanf(A[i].x);_scanf(A[i].y);
}
_scanf(M);
for(int i=1;i<=M;i++)
{
_scanf(B[i].x);_scanf(B[i].y);
}
}
bool check(int mid)
{
int n1=0,n2=0;
for(int i=1;i<=N;i++)
if(A[i].x<=mid) n1+=1+(mid-A[i].x)/A[i].y;
for(int i=1;i<=M;i++)
if(B[i].x<=T-mid) n2+=1+(T-mid-B[i].x)/B[i].y;
return n1>n2;
}
void work()
{
int L=1,R=T+1,mid,ans=-1;
while(L>1;
if(check(mid)) R=mid;
else L=mid+1,ans=mid;
}
printf("%d
",ans);
}
int main()
{
freopen("monkey.in","r",stdin);
freopen("monkey.out","w",stdout);
data_in();
work();
return 0;
}
T2(避难向导 shelter): #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn=100005;
typedef long long LL;
int N,M,a,b,c;
struct edge{ int to,next,w; }E[maxn<<1];
int first[maxn],np,dep[maxn],fa[maxn][18];
int o1,o2,D[maxn],du[maxn],maxd[maxn][18];
LL d1[maxn],d2[maxn],dist[maxn];
void _scanf(int &x)
{
x=0;
char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
}
void add_edge(int u,int v,int w)
{
E[++np]=(edge){v,first[u],w};
first[u]=np;
}
void data_in()
{
_scanf(N);_scanf(M);_scanf(a);_scanf(b);_scanf(c);
int x,y,z;
for(int i=1;ivoid getd(int i,int f,LL l,int &o)
{
dist[i]=l;
if(du[i]==1&&dist[i]>dist[o]) o=i;
for(int p=first[i];p;p=E[p].next)
{
int j=E[p].to;
if(j==f) continue;
getd(j,i,l+E[p].w,o);
}
}
void DFS(int i,int f,LL l,LL *d)
{
d[i]=l;
for(int p=first[i];p;p=E[p].next)
{
int j=E[p].to;
if(j==f) continue;
DFS(j,i,l+E[p].w,d);
}
}
void _DFS(int i,int f,int d)
{
dep[i]=d;
fa[i][0]=f;
maxd[i][0]=max(D[i],D[f]);
for(int j=1;(1<1]][j-1];
maxd[i][j]=max(maxd[i][j-1],maxd[fa[i][j-1]][j-1]);
}
for(int p=first[i];p;p=E[p].next)
{
int j=E[p].to;
if(j==f) continue;
_DFS(j,i,d+1);
}
}
int LCA(int x,int y)
{
if(dep[x]int len=dep[x]-dep[y];
for(int j=0;(1<if((1<if(x==y) return x;
for(int j=17;j>=0;j--)
if(fa[x][j]!=fa[y][j]) x=fa[x][j],y=fa[y][j];
return fa[x][0];
}
int solve1(int x,int y,int p)
{
if(dep[x]-dep[y]<=1)
{
if(D[x]>=p) return x;
if(D[y]>=p) return y;
return -1;
}
int len=dep[x]-dep[y],maxv=0;
int z=x;
for(int j=0;(1<if((1<if(maxvreturn -1;
len/=2;
z=x;
for(int j=0;(1<if((1<int re=solve1(x,z,p);
if(re!=-1) return re;
return solve1(fa[z][0],y,p);
}
int solve2(int x,int y,int p)
{
if(dep[x]-dep[y]<=1)
{
if(D[y]>=p) return y;
if(D[x]>=p) return x;
return -1;
}
int len=dep[x]-dep[y],maxv=0;
int z=x;
for(int j=0;(1<if((1<if(maxvreturn -1;
len/=2;
z=x;
for(int j=0;(1<if((1<int re=solve2(fa[z][0],y,p);
if(re!=-1) return re;
return solve2(x,z,p);
}
void work()
{
for(int i=1;i<=N;i++)
if(du[i]==1) { o1=i; break; }
getd(o1,0,0,o1);
getd(o1,0,0,o2=o1);
DFS(o1,0,0,d1);
DFS(o2,0,0,d2);
for(int i=1;i<=N;i++)
D[i]=(max(d1[i],d2[i])+a)*b%c;
_DFS(1,0,1);
int x,y,ans,gf,p;
for(int i=1;i<=M;i++)
{
_scanf(x);_scanf(y);_scanf(p);
gf=LCA(x,y);
ans=solve1(x,gf,p);
if(ans==-1) ans=solve2(y,gf,p);
printf("%d
",ans);
}
}
int main()
{
freopen("shelter.in","r",stdin);
freopen("shelter.out","w",stdout);
data_in();
work();
return 0;
}
T3(银河大盗robbers): #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn=205;
int N,T;
int dist[maxn][maxn],sum[maxn],cnt;
double f[maxn],p[maxn][maxn];
struct matrix{
int x,y;
double A[maxn][maxn];
matrix(){
x=y=0;
memset(A,0,sizeof(A));
}
friend matrix operator * (matrix a,matrix b)
{
matrix c;
c.x=a.x,c.y=b.y;
for(int i=1;i<=c.x;i++)
for(int j=1;j<=c.y;j++)
for(int k=1;k<=a.y;k++)
c.A[i][j]+=a.A[i][k]*b.A[k][j];
return c;
}
}ans,pp,re,tmp;
void data_in()
{
scanf("%d%d",&N,&T);
for(int i=1;i<=N;i++)
scanf("%lf",&f[i]);
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
scanf("%d",&dist[i][j]);
}
void ready()
{
for(int k=1;k<=N;k++)
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
sum[i]+=dist[i][j];
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
p[i][j]=1.0*dist[i][j]/sum[i];
}
void work()
{
if(T!=0)
{
pp.x=pp.y=N;
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
pp.A[i][j]=p[j][i];
tmp=re=pp;
for(int j=1;(1<if((1<1;
for(int i=1;i<=N;i++)
ans.A[i][1]=f[i];
ans=re*ans;
for(int i=1;i<=N;i++)
printf("%.6lf
",ans.A[i][1]);
}
else
{
for(int i=1;i<=N;i++)
printf("%.6lf
",f[i]);
}
}
int main()
{
freopen("robbers.in","r",stdin);
freopen("robbers.out","w",stdout);
data_in();
ready();
work();
return 0;
}