Java實現 藍橋杯 算法提升 進攻策略增強(暴力)

試題 算法提升 進攻策略增強

問題描述
  植物大戰殭屍這款遊戲中,還有一個特別的玩兒法:玩家操縱殭屍進攻植物。
  首先,殭屍有m種(每種殭屍都是無限多的),玩家能夠選擇合適的殭屍來進攻。使用第i種殭屍須要花費Wi資源,能夠獲得Pi的攻擊效果。在這裏,咱們認爲多個殭屍總的攻擊效果就是他們每一個攻擊效果的代數和。
  地圖共有n行,對於第i行,最左端有若干植物,這些植物須要至少Qi的攻擊才能被所有消滅。若一行上的植物所有被消滅,咱們稱這一行被攻破。
  因爲資源緊張,你只有總量爲K的資源,不必定可以攻破全部行。但統治者但願攻破相鄰的T行,並但願T儘可能的大。你能幫他算出T的值嗎?
輸入格式
  第一行三個非負整數:m、n、K;
  第二行m個正整數,第i個數表示Wi;
  第三行m個正整數,第i個數表示Pi;
  第四行n個非負整數,第i個數表示Qi。
輸出格式
  3 11 39
  5 2 11
  3 1 7
  5 3 6 10 3 2 4 200 1 1 1
樣例輸入
一個知足題目要求的輸入範例。
例:
2 2
1 2
3 4
樣例輸出
4
數據規模和約定
  對於70%的數據:n<=1000java

對於100%的數據:n<=200000,m<=100,K<=1000000,全部Pi、Qi<=100000000web

PS:
感受這個題沒描述清除,很煩
這個題應該能夠算暴力,由於我是暴力過去的,把每一種可能都循環一遍
後面經過map記錄一下,減小循環,相似於剪枝的操做吧算法

package 藍橋杯官網;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class 進攻策略增強 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int m = s.nextInt();
        int n = s.nextInt();
        int k = s.nextInt();
        int []wi = new int[m];
        int []pi = new int[m];
        for (int i = 0; i < pi.length; i++) {
            wi[i] = s.nextInt();
        }
        for (int i = 0; i < pi.length; i++) {
            pi[i] = s.nextInt();
        }
        int []qi = new int[n];
        for (int i = 0; i < qi.length; i++) {
            qi[i] = s.nextInt();
        }
        int T = 0;
        for (int i = n-1; i >=0; i--) {
            for (int j = 0; j+i>=i&&j+i < n; j++) {
                int a = k;
                int count = 0;
                for (int z = j; z <=j+i; z++) {
                    int x = getPrefferred(wi,pi,qi,z);
                    if(a>=x)
                    {
                        count++;
                        a-=x;
                    }else
                    {
                        break;
                    }
                }
                if(count>T)
                {
                    T = count;
                    if(T>=i+1)
                    {
                        System.out.println(T);
                        return;
                    }
                }
            }
        }
        System.out.println(T);
    }
    static Map<Integer,Integer> map = new HashMap<Integer,Integer>();
    public static int getPrefferred(int []wi,int []pi,int []qi,int index)
    {
        Integer dp = map.get(qi[index]);
        if(dp!=null) return dp;
        int price = Integer.MAX_VALUE;
        int minIndex = 0;
        for (int i = 0; i < wi.length; i++) {
            int pc =(int)( Math.ceil(qi[index]*1.0/pi[i])*wi[i]);
            if(pc<price)
            {
                price = pc;
                minIndex = i;
            }
        }
        map.put(qi[index], price);
        return price;
    }
}