zoukankan      html  css  js  c++  java
  • Under Attack(zoj)

    Doctor serves at a military air force base. One day, the enemy launch a sudden attack and the base is under heavy fire. The fighters in the airport must take off to intercept enemy bombers. However, the enemies know this clearly and they now focus on destroying the runway. The situation is becoming worse rapidly!

    Every part of the runway has a damage level. On each bombing run, the damage level is increased by the bomb's damage . Fortunately, the enemy bombers has to stop bombing the runway when they run out of ammo. Then the ground crew have time to evaluate the situation of runway so that they can come to repair the runway ASAP after enemy attacks. The most heavily-damaged part on fighters' taking off and landing path should first be repaired. Assume that runway start from north and head to south , and fighters will take off or land only from north to south or vice versa.

    Now that the task is clear, the ground crew need the cooridinates of two points: first that is the most damaged point from north to south, second is the most damaged point from south to north.The base's central mainframe is down under hacker attack. So Doctor could only use his poor little and shabby notebook to fulfill this task. Can you help him?

    Input

    The input consists of multiple cases.
    The first line is the runway length L. L can be up to 15000.
    Next lines will describe enemy bombing runs ,each line describes effect range start end of each bombing run and enemy bomb damage d.if start is -1, this case ends..
    There can be up to 3000 bombing run, each time the damage is up to 100.
    Notice that the bombing range is from north to south, and runway range is [0,len].

    Output

    Output the cooridinates of two points: first that is the most damaged point from north to south, second is the most damaged point from south to north.

    Sample Input
    10            //长0到10的路
    1 5 2    //从1到5被炸弹伤害为2
    6 9 2   //从6到9的路段被炸弹伤害为2(伤害有累加效果)
    -1 -1 -1  //-1代表一组测试数据结束

    Sample Output
    1 9     //分别输出从左边和从右边算起伤害最大的第一个点

    View Code
     1 #include <iostream>
    2 #include<stdio.h>
    3 #define MAX 15010
    4 using namespace std;
    5 struct node
    6 {
    7 int left,right;
    8 int num;
    9 node()
    10 {
    11 num=0;
    12 }
    13 };
    14 node tree[4*MAX];
    15 void build(int left,int right,int i)
    16 {
    17 tree[i].left=left;tree[i].right=right;
    18 if (left<right)
    19 {
    20 int mid=(left+right)>>1;
    21 build(left,mid,2*i);
    22 build(mid+1,right,2*i+1);
    23 }
    24 }
    25 void insert(int i,int left,int right,int num)
    26 {
    27 if(left<=tree[i].left&&tree[i].right<=right)
    28 {
    29 tree[i].num+=num;
    30 return;
    31 }
    32 if (tree[i].left<tree[i].right)
    33 {
    34 int mid=(tree[i].left+tree[i].right)>>1;
    35 if(right<=mid)
    36 insert(2*i,left,right,num);
    37 else if (left>mid)
    38 insert(2*i+1,left,right,num);
    39 else
    40 {
    41 insert(2*i,left,mid,num);
    42 insert(2*i+1,mid+1,right,num);
    43 }
    44 }
    45 }
    46 int sum;
    47 void finds(int k,int i)
    48 {
    49 if (tree[i].left<=k&&k<=tree[i].right)
    50 {
    51 sum+=tree[i].num;
    52 }
    53 if(tree[i].left<tree[i].right)
    54 {
    55 int mid=(tree[i].left+tree[i].right)>>1;
    56 if(k<=mid)
    57 finds(k,2*i);
    58 else
    59 finds(k,2*i+1);
    60 }
    61 }
    62 int main()
    63 {
    64 int n,i,num,left,right,max,north,south;
    65 while(cin>>n)
    66 {
    67 build(1,n+1,1);
    68 for(i=0;i<=4*n;i++)
    69 tree[i].num=0;
    70 while(scanf("%d%d%d",&left,&right,&num)&&left!=-1)
    71 {
    72 insert(1,left+1,right+1,num);
    73 }
    74 max=0;
    75 for(i=1;i<=n+1;i++)
    76 {
    77 sum=0;
    78 finds(i,1);
    79 if(max<sum)
    80 {
    81 north=i;
    82 max=sum;
    83 }
    84 if(max<=sum)
    85 {
    86 south=i;
    87 }
    88 }
    89 printf("%d %d\n",north-1,south-1);
    90 }
    91 return 0;
    92 }
  • 相关阅读:
    PAT 1008--------数组元素的循环右移,你需要记住的
    PAT1049-----枚举法,找规律题,注意降低时间复杂度
    PAT1048----你需要了解并记住的解题思路
    C++中几个输入函数的用法和区别(cin、cin.get()、cin.getline()、getline()、gets()、getchar()))
    PAT1040----关于数学题目的解法新思路值得借鉴,字符的配对
    PAT1029-----介绍字符串的解题思路和部分知识点
    PAT1027-----等差数列的问题或数学问题
    PAT1026----四舍五入的思路,%2d的一些知识
    / 已阅 /PAT1017-------高精度计算,问题的所有可能情况
    LeetCode 无重复字符的最长子串
  • 原文地址:https://www.cnblogs.com/qijinbiao/p/2368066.html
Copyright © 2011-2022 走看看