【HihoCoder1391】【排序】【堆】Countries 题解

Countries

There are two antagonistic countries, country A and country B. They are in a war, and keep launching missiles towards each other.

It is known that country A will launch N missiles. The i-th missile will be launched at time Tai. It flies uniformly and take time Taci from one country to the other. Its damage capability is Dai.

It is known that country B will launch M missiles. The i-th missile will be launched at time Tbi.

It flies uniformly and takes time Tbci from one country to the other. Its damage capability is Dbi.

Both of the countries can activate their own defending system.

The defending system of country A can last for time TA, while The defending system of country B can last for time TB.

When the defending system is activated, all missiles reaching the country will turn around and fly back at the same speed as they come.

At other time, the missiles reaching the country will do damages to the country.
(Note that the defending system is still considered active at the exact moment it fails)

Country B will activate its defending system at time X.

When is the best time for country A to activate its defending system? Please calculate the minimal damage country A will suffer.

Input
There are no more than 50 test cases.

For each test case:

The first line contains two integers TA and TB, indicating the lasting time of the defending system of two countries.

The second line contains one integer X, indicating the time that country B will active its defending system.

The third line contains two integers N and M, indicating the number of missiles country A and country B will launch.

Then N lines follow. Each line contains three integers Tai, Taci and Dai, indicating the launching time, flying time and damage capability of the i-th missiles country A launches.

Then M lines follow. Each line contains three integers Tbi, Tbci and Dbi, indicating the launching time, flying time and damage capability of the i-th missiles country B launches.

0 <= TA, TB, X, Tai, Tbi<= 100000000

1 <= Taci, Tbci <= 100000000

0 <= N, M <= 10000

1 <= Dai, Dbi <= 10000

Output
For each test case, output the minimal damage country A will suffer.

O(N)预处理,求出每枚导弹不会打到A需要A国防御系统开启的时间段[st,et],只有A开启防御的时间段[Y,Y+Ta]包含[st,et]那么这枚导弹不会打到A。将et从小到大排序。
从1到n+m枚导弹,对于当前这枚导弹,如果需要被防御,那么A防御系统的结束时间就为et,那么开启时间就为et-Ta
那么将之前已经被防御的导弹中st

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define rez(i,x,y) for(int i=x;i>=y;i--)
#define res(i,x,y) for(int i=x;i<=y;i++)
#define INF 2100000000
#define LL long long
#define clr(x)  memset(x,0,sizeof(x))
#define MAX 0x3f3f3f3f
#define N 20004
using namespace std;
template<class T>inline void readin(T &res){
    static char ch;
    while((ch=getchar())>'9'||ch<'0');
    res=ch-48;
    while((ch=getchar())<='9'&&ch>='0')
    res=ch-48+res*10;
}
int n,m,cnt,ans;
int Ta,Tb,X,sum;
struct node{
    LL t,c,e,d;
}a[N],b[N];
struct cmpf{
    bool operator ()(const int &lhs,const int &rhs){
        return a[lhs].t>a[rhs].t;
    }
};
bool cmp(node lhs,node rhs){
    return lhs.e<rhs.e;
}
int main(){
    int x,y,i,j;
    while(scanf("%d%d",&Ta,&Tb)!=EOF){
        cnt=0,ans=MAX,sum=0;
        readin(X),readin(n),readin(m);
        for(i=1;i<=n;i++){
            readin(b[i].t),readin(b[i].c),readin(b[i].d);
            b[i].e=b[i].t+b[i].c;
            if(b[i].e>=X&&b[i].e<=X+Tb){
                sum+=b[i].d;
                a[++cnt].t=b[i].e+b[i].c;
                j=Tb+X-a[cnt].t;
                j=j%(2*b[i].c);
                a[cnt].e=Tb+X-j;
                a[cnt].d=b[i].d;
                if(j>=b[i].c)a[cnt].e+=b[i].c+b[i].c;
                if(a[cnt].t+b[i].c<X||a[cnt].t>Tb+X)a[cnt].e=a[cnt].t;
            }
        }
        for(i=1;i<=m;i++){
            readin(b[i].t),readin(b[i].c),readin(b[i].d);
            b[i].e=b[i].t+b[i].c;
            sum+=b[i].d;
            a[++cnt].t=b[i].e;
            j=Tb+X-a[cnt].t;
            j=j%(2*b[i].c);
            a[cnt].e=Tb+X-j;
            a[cnt].d=b[i].d;
            if(j>=b[i].c)a[cnt].e+=b[i].c+b[i].c;
            if(a[cnt].t+b[i].c<X||a[cnt].t>Tb+X)a[cnt].e=a[cnt].t;
        }
        sort(a+1,a+cnt+1,cmp);
        priority_queue<int,vector<int>,cmpf>q;
        for(i=1;i<=cnt;i++){
            q.push(i);y=a[i].e;
            sum-=a[i].d;
            x=q.top();
            while(y-a[x].t>Ta&&!q.empty()){
                sum+=a[x].d;
                q.pop();x=q.top();
            }
            ans=min(ans,sum);
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值