2020-02-15 Jedi and the Galactic Empire

Jedi Knights are often tasked with protection. Whether protecting shield generators or important
diplomats, the Jedi will use their lightsabers to deflect blaster shots and keep their asset safe.
Sometimes a Jedi will go on missions alone or will be accompanied by another Jedi. When
protecting an asset, they will stand side by side deflecting shots that would otherwise harm the
asset they wish to protect. Sometimes, even together, they cannot block all the blaster shots.
That is because Jedi are still limited by their physical reaction time. More specifically, when a
Jedi blocks a blaster shot, he has to wait a certain amount of time before he can block another
shot. For example, a Jedi that takes t time units between shots can block a shot arriving at time k
and a shot arriving at time k + t (or later).
So, Jedi will coordinate which shots they each will block and which shots they will allow to pass
through their defense. Either Jedi can independently block each shot as long as there is enough
time between the current shot and his last blocked shot. But determining which shots to block
and which to let by is no easy task if they want to minimize the number of shots that reach their
asset. That is why they seek aid from the other great power in the galaxy, programming.
As the Jedi’s knowledge of programming is not as deep as their knowledge of the force, they
have asked the programmers of the Universal Computational Federation (UCF) to find better
strategies for blocking blaster shots with their lightsabers.

The Problem:
Given the times the blasters reach the Jedi, the number of Jedi, and their reaction time, determine
the number of blaster shots that reach the asset they are trying to protect. Note that each Jedi can
block a blaster shot at the beginning of the mission but after his first block the Jedi is limited by
his reflexes (the time he has to wait before he can block another shot).

The Input:
The first input line contains a positive integer, n, indicating the number of protection missions
the Jedi have been assigned. This is followed by the data for each mission. The first input line
for each mission contains an integer, b (1 ≤ b ≤ 1,000), the number of blaster shots fired at their
asset. This is followed by a line containing b numbers, separated by spaces, which are the times
the blaster shots will reach the Jedi. These numbers can be in any order but will be positive
integers less than 1,000,000. This is followed by an integer j (1 ≤ j ≤ 2) on a line by itself, which
is the number of Jedi on the mission. The last input line for a mission contains j space separated
integers giving the reaction time of each Jedi, the time it takes a Jedi to prepare to block the next
blaster shot. These numbers will be between 1 and 100 inclusive.

The Output:
For each mission, output “Mission #m: a” where m is the mission number (starting with 1)
and a is the minimum number of blaster shots that the Jedi are unable to block and will hit their
asset. Leave a blank line after the output for each mission.
Sample Input:
4
5
10 5 5 10 5
1
100
4
2 4 9 9
2
10 7
5
2 4 8 13 13
2
10 7
5
2 4 6 8 10
1
2
Sample Output:
Mission #1: 4
Mission #2: 1
Mission #3: 1
Mission #4: 0
Explanation of the Sample Input/Output:
In Mission #2, Jedi with speed 7 can block 2 and one 9 and Jedi with speed 10 can block 4,
resulting in one shot remaining unblocked.
In Mission #3, Jedi with speed 7 can block 4 and one 13 and Jedi with speed 10 can block 2 and
the other 13, resulting in one shot remaining unblocked.
大意就是给你若干个时间点作为有射击,会有一个或两个人帮你挡子弹,但是他们的防护有反应时间,求你受到的最少的攻击次数
思路:一个人直接遍历;两个人的时候通过dfs加上数组记录求过的情况(剪枝)来缩短运算时间即可。
题目

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<vector>
#include<algorithm>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
const int N=1000010;
const int inf=0x3f3f3f;
int n,k,m,coun;
int f1,f2;
int f[1000][50][50];
int mk[1010];
void check()
{
    int x=mk[0];
    for(int i=1;i<m;++i)
    {
        if(mk[i]-x>=f1)
            x=mk[i];
        else 
            coun++;
    }
}
int dfs(int x,int u,int fr)
{
    if(fr>=m)
        return 0;
    int d;
    if(fr==0)
        d=mk[0];
    else 
        d=mk[fr]-mk[fr-1];
    x=max(x-d,0);
    u=max(u-d,0);       //将坐标转换成距离当前点的距离,若可以取(小于等于0)则取0。
    if(f[fr][x][u]<inf)
            return f[fr][x][u];
    if(!u&&!x)
    {
        f[fr][x][u]=min(dfs(f1,u,fr+1),dfs(x,f2,fr+1));
        return f[fr][x][u];
    }
    else if(!x)
    {
        f[fr][x][u]=dfs(f1,u,fr+1);
        return f[fr][x][u];
    }
    else if(!u)
    {
        f[fr][x][u]=dfs(x,f2,fr+1);
        return f[fr][x][u];
    }
    else
    {
        f[fr][x][u]=dfs(x,u,fr+1)+1;
        return f[fr][x][u];
    }
}
int main() {
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int t;
        coun=0;
        memset(f,0x3f,sizeof(f));
        printf("Mission #%d: ",i);
        cin>>m;
        for(int j=0;j<m;j++)
        {
            scanf("%d",&mk[j]);
        }
        sort(mk,mk+m);
        scanf("%d%d",&t,&f1);
        if(t==1)
        {
            check();
            printf("%d\n\n",coun);
        }
        else
        {
            scanf("%d",&f2);
            printf("%d\n\n",dfs(0,0,0));
        }
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,616评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,020评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,078评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,040评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,154评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,265评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,298评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,072评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,491评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,795评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,970评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,654评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,272评论 3 318
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,985评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,223评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,815评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,852评论 2 351