您的位置:首页 > 理论基础 > 计算机网络

Countries 2016北京网络赛 预处理+排序+优先队列

2016-09-25 15:23 337 查看
Countries

时间限制:1000ms

单点时限:1000ms

内存限制:256MB

描述

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.

输入

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

输出

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

提示

In the first case, country A should active its defending system at time 3.

Time 1: the missile is launched by country A.

Time 2: the missile reaches country B, and country B actives its defending system, then the missile turns around.

Time 3: the missile reaches country A, and country A actives its defending system, then the missile turn around.

Time 4: the missile reaches country B and turns around.

Time 5: the missile reaches country A and turns around.

Time 6: the missile reaches country B, causes damages to country B.

样例输入

2 2

2

1 0

1 1 10

4 5

3

2 2

1 2 10

1 5 7

1 3 2

0 4 8

样例输出

0

17

题目链接:

  http://hihocoder.com/problemset/problem/1391

题目大意:

A和B两个国家互射导弹,每个国家都有一个防御系统,在防御系统开启的时间内可以将到达本国的导弹反弹回去(掉头,防御系统不能开开关关)。

现在已知:Ta、Tb为A、B两国导弹防御能开启的持续时间,X为B国开启导弹防御系统的时刻(持续时间为[X,Tb+X],包含端点)

A向B发射N枚导弹,B向A发射M枚导弹。每枚导弹有3个值:发射时间,从A到B或从B到A的飞行时间,伤害值。

现在A可以在任意时刻开启防御系统,求A所受到的最小伤害值。

分析思路:

拿到题目后不再装状态,居然没有根据导弹的发射时间和B的防御时间想出求出为使每个导弹不大打中A的防御区间(只想到了一个ct一个ct的模拟。。。。对自己无语)。

首先预处理,求出每枚导弹不会打到A需要A国防御系统开启的时间段[st,et],只有A开启防御的时间段[Y,Y+Ta]包含[st,et]那么这枚导弹不会打到A。

  预处理之后将每枚导弹按照et从小到大排序。

  

  从1到n+m枚导弹,对于当前这枚导弹,如果需要被防御,那么A防御系统的结束时间就为et,那么开启时间就为et-Ta

  

  所以可以开个STL的优先队列记录之前被防御的导弹序号,按照st从小到大存,每次比较最小的st和开启时间et-Ta。并在过程中增减伤害值,并记录最小伤害。

AC代码:

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
#define MAX 0x7f7f7f7f
using namespace std;
typedef long long  LL;

const int maxn = 20005;
int Ta,Tb,x,sum;
int n,m,misnum,ans;

struct mis
{
LL st,et,ct,d;
}a[maxn],b[maxn];

struct cmp1
{
bool operator () (const int &aa,const int &bb)
{
return a[aa].st>a[bb].st;
}
};

bool cmp(mis aa,mis bb)
{
return aa.et<bb.et;
}

int main()
{
int i,j,k;
int x,y,z;
while(scanf("%d%d",&Ta,&Tb)!=EOF)
{
misnum=0;ans=MAX;sum=0;
scanf("%d%d%d",&x,&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&b[i].st,&b[i].ct,&b[i].d);
b[i].et=b[i].st+b[i].ct;
if(b[i].et>=x && b[i].et<=x+Tb)
{
sum+=b[i].d;
a[++misnum].st=b[i].et+b[i].ct;
j=x+Tb-a[misnum].st;
j=j%(2*b[i].ct);
a[misnum].et=x+Tb-j;
if(j>=b[i].ct) a[misnum].et+=2*b[i].ct;
a[misnum].d=b[i].d;
if(a[misnum].st+b[i].ct<x || a[misnum].st+b[i].ct>x+Tb) a[misnum].et=a[misnum].st;
}
}
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&b[i].st,&b[i].ct,&b[i].d);
b[i].et=b[i].st+b[i].ct;
sum+=b[i].d;
a[++misnum].st=b[i].et;
j=x+Tb-a[misnum].st;
j=j%(2*b[i].ct);
a[misnum].et=x+Tb-j;
if(j>=b[i].ct) a[misnum].et+=2*b[i].ct;
a[misnum].d=b[i].d;
if(a[misnum].st+b[i].ct<x || a[misnum].st+b[i].ct>x+Tb) a[misnum].et=a[misnum].st;

}
sort(a+1,a+misnum+1,cmp);

priority_queue<int,vector<int>,cmp1> q;

for(i=1;i<=misnum;i++)
{
q.push(i);
y=a[i].et;
sum-=a[i].d;
x=q.top();
while(y-a[x].st>Ta && !q.empty())
{
sum+=a[x].d;
q.pop();
x=q.top();
}
ans=min(ans,sum);
}
printf("%d\n",ans);

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: