HDU 4070 Phage War

本文解析了PhageWar这款游戏背后的算法实现。该算法通过贪心策略,对病毒传播时间进行排序并计算最少时间感染所有细胞的方法。适用于喜欢算法挑战和技术解析的读者。

Phage War

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 721    Accepted Submission(s): 382


Problem Description
Phage War is a little flash game. In this game, we want infect all cells by the transmission and breed of phages. 
Originally, there is a cell infected by phages and this cell can breed a new phage every second. You should know that only the new born phages can inject other cells.

There are n cells around this cell, numbered from 1 to n. If there are Di phages reaching the i-th cell, the cell would be infected, and the phages journey will cost Ti seconds. To simplify it, we assume these phages will stay in this new cell and they can’t infect other cells. And the new cell cannot breed new phages and infect other cells.
Can you tell me how much time it costs to infect all cells at least? 
 

Input
In the first line there is an integer T (T <= 50), indicates the number of test cases.
In each case, the first line contains a integers N (1 <= N <= 10^5). Then there are N lines, each line contain two integers Di, Ti (1<=Di, Ti<=100).
 

Output
For each case, output the least time needed in one line.(as shown in the sample output)
 

Sample Input
2 2 2 1 5 6 2 1 11 3 10
 

Sample Output
Case 1: 11 Case 2: 14
 


贪心,按时间从大到小排序,然后判断当前病毒的侵入时间加上之前繁殖病毒的时间是否为所有“最短”时间中最大的。

代码如下:

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>

using namespace std;

struct pha
{
    int d, t;
}p[100002];
int cmp(const void *a, const void *b)
{
    pha *aa = (pha*)a;
    pha *bb = (pha*)b;
    return bb->t - aa->t;
}
int main()
{
#ifdef test
    freopen("in.txt", "r", stdin);
#endif
    int n, t;
    scanf("%d", &t);
    for(int k = 1; k <= t; k++)
    {
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
            scanf("%d%d", &p[i].d, &p[i].t);
        qsort(p, n, sizeof(p[0]), cmp);
        int sum = 0 , max = 0;
        for(int i = 0; i < n; i++)
        {
            sum += p[i].d;
            if(sum + p[i].t > max)
                max = sum + p[i].t;
        }
        printf("Case %d: %d\n", k, max);
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值