给出N个人在0时刻的财富值M[i](所有人在0时刻的财富互不相等),以及财富增长速度S[i],随着时间的推移,某些人的财富值会超越另外一些人。如果时间足够长,对于财富增长最快的人来说,他的财富将超越所有其他对手。
求发生的前10000次超越,分别是谁超过了谁?如果总的超越次数不足10000,则输出所有超越,如果1次超越都不会发生,则输出No Solution。
输出按照超越发生的时间排序,同一时刻发生的超越,按照超越者的编号排序,如果编号也相同,则按照被超越者的编号排序。所有排序均为递增序。
Input
第1行:N,表示人的数量。(1 <= N <= 10000) 第2 - N + 1行:每行2个数,分别是初始的财富值M[i],和财富增长速度S[i]。(0 <= M[i] <= 10^5, 1 <= S[i] <= 100)
Output
输出前10000次超越,超越者和被超越者的编号。如果总的超越次数不足10000,则输出所有。如果1次超越都不会发生,则输出No Solution。 输出按照超越发生的时间排序,同一时刻发生的超越,按照超越者的编号排序,如果超越者编号也相同,则按照被超越者的编号排序。所有排序均为递增序。
Input示例
4 1 100 2 100 3 1 4 50
Output示例
2 3 1 3 2 4 1 4
追击问题,给出N个人的初始距离及速度,求前10000次超越;
优先队列:首先按照初始距离排序,逐个遍历,二分搜索初始距离比他大的人,如果速度比他小就计算时间差并记录,如果大于10000,比较队首元素判断是否更新
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 5e4+10;
const int Maxn=1e4;
int m[N], s[N];
struct node
{
int m, s, id;
} p[N];
bool operator <(const node&a,const node&b)
{
if(a.m==b.m) return a.s<b.s;
return a.m<b.m;
}
struct node1
{
int a, b;
double t;
} ans[N];
bool operator<(const node1&x,const node1&y)
{
if(x.t==y.t)
{
if(x.a==y.a) return x.b<y.b;
else return x.a<y.a;
}
else return x.t<y.t;
}
int n;
void solve()
{
sort(p,p+n);
priority_queue<node1>q;
for(int i=0; i<n; i++)
{
int k=lower_bound(p+i+1,p+n,p[i])-p;
for(int j=k; j<n; j++)
{
if(p[i].s>p[j].s)
{
node1 tmp;
tmp.t=(double)(p[j].m-p[i].m)/(p[i].s-p[j].s);
tmp.a=p[i].id, tmp.b=p[j].id;
if(q.size()==Maxn)
{
if(tmp<q.top())
{
q.pop();
q.push(tmp);
}
}
else q.push(tmp);
}
}
}
int k=0;
while(!q.empty())
{
ans[k++]=q.top();
q.pop();
}
for(int i=k-1; i>=0; i--)
printf("%d %d\n",ans[i].a,ans[i].b);
}
int main()
{
while(scanf("%d", &n)!=EOF)
{
for(int i=0; i<n; i++)
{
scanf("%d %d",&p[i].m, &p[i].s);
p[i].id=i+1;
}
solve();
}
return 0;
}