BNUOJ 26223 CosmoCraft

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   使用   os   io   

CosmoCraftTime Limit: 1000msMemory Limit: 32768KBThis problem will be judged on HDU. Original ID: 4257
64-bit integer IO format: %I64d      Java class name: Main In the two-player game CosmoCraft you manage an economy in the hopes of producing an army capable of defeating your opponent. You manage the construction of workers, production facilities, and army units; the game revolves around balancing the resources you allocate to each. The game progresses in turns.
1. Workers give you income at the rate of 1 dollar per turn. 
2. Production facilities let you produce either an army unit or a worker for the cost of 1 dollar. (only 1 army unit or worker can be produced per turn per facility) 
3. It costs 1 dollar to create a production facility. 
4. Your army, of course, lets you fight against your opponent. 
You start off with n workers and k production facilities. The game progresses in turns – at each turn, you can spend the income you get from your workers on a mixture of workers, army, and creating production facilities. Workers produced this round do not give you income until the next round; likewise, production facilities do not become active until the next round. Any unspent income from the current round carries over to the next. 
At the end of a round, you can take the total army you’ve produced and attack your opponent; if you have strictly more units than your opponent, the opponent loses immediately, and you retain the difference of the army sizes. Otherwise, your army is crushed and your opponent is left with the difference of the army sizes. (it would be wise for him to counter-attack after this, but you don’t lose immediately at least). The game ends after t turns, at which point both players will usually attack with the larger army reigning victorious. 
You’re playing against your friend, and since you’ve played against him so many times you know exactly what he’s going to spend his money on at every turn, and exactly when he’s going to attack. Knowing this, you’ve decided that the best strategy is to play defensively – you just want to survive every attack, and amass as large an army in the meantime so you can counterattack (and hopefully win) at the end of the game. 
What’s the largest army you can have at the end of the game, given that you must survive all your friend’s attacks? InputThere will be several test cases in the input. Each test case will begin with a line with three integers: 
n k t 
where n (1≤n≤100) is the number of workers you start with, k (1≤k≤100) is the number of production facilities you have at the start, and t(1≤t≤10,000) is the number of turns. On the next line will be t-1 integers, ai (0≤ai≤Max signed 64-bit integer), separated by single spaces. The ith integer indicates the strength of the attack (that is, the number of army units your opponent is using in that attack) on turn i. The input will end with a line with three 0s. 
Hint
Huge input, please ues c++. OutputFor each test case output a single integer indicating the maximum number of armies you could have at the end of the game. Output -1 if it is impossible to survive. Output each integer on its own line, with no spaces, and do not print any blank lines between answers. While it is possible for some inputs to generate unreasonably large answers, all judge inputs yield answers which will fit in a signed 64-bit integer. Sample Input
8 4 622 6 10 14 04 3 30 06 9 70 0 11 0 7 00 0 0
Sample Output
-111101
SourceThe University of Chicago Invitational Programming Contest 2012 解題:轉自他人 

題目大意

  介紹一款名叫CosmoCraft的雙人回合制策略遊戲(但網上找不到這種遊戲),每一回合玩家可以:1。花費1元/個購買工人,2。花費1元/個購買士兵,3。花費1元/個購買兵營。每回合1個工人可以掙1元,但錢會在下一回合給你,每個兵營每一回合只能造一個工人或士兵,同樣的每一回合建造的兵營將在下一回合起可用,每一回合造的工人或士兵當前回合起即可使用。遊戲初始給你n個工人和k個兵營以及n元,遊戲共t回合,前t-1回合每一回合將會有a[i]個敵方士兵回來攻擊你,你必須能抵擋住每一回合的進攻,即在每一回合擁有比進攻你的敵方軍隊更多計程車兵,每次戰爭士兵數多的一方取勝,並剩下雙方交戰士兵的差值計程車兵數,而失敗一方則一兵不剩,請你使用最優策略使得你在第t回合的反擊中擁有最多計程車兵,並輸出最大值。

 

貪心策略:

  1。每一回合必須花完所有的錢。

  2。一個士兵不可能連續存在兩回合以上(超過兩回合說明他沒有打仗,可以花1元在第一回合造工人,在第二回合用工人賺的錢造兵營,第三回合再用工人賺的錢和造的兵營造士兵)。

  3。每回合在保證生存的前提下,先造盡量多的工人,能造多少造多少,剩下的錢再造兵營。

  4。根據策略二,第i(1<=i<=t)回合計程車兵只能由第i回合或第i-1回合造,若第i回合可以造出d[i]個士兵,就在第i回合造,否則需要第i-1回合的支援,第i-1回合最多支援第i回合k[i-1]-u[i-1]個士兵(在w[i-1]>k[i-1]的情況下)否則不能抵禦第i回合的進攻。(k[i]代表第i回合的兵營數,n[i]代表第i回合的工人數,u[i]代表第i回合所需士兵數)。

 

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 100100;18 LL d[maxn],n,k,t,u,theMin;19 int main() {20     int i,j;21     bool flag;22     while(scanf("%I64d %I64d %I64d",&n,&k,&t),n||k||t){23         for(i = 1; i < t; i++) scanf("%I64d",d+i);24         d[t] = 0;25         if(min(n,k) < d[1]){puts("-1");continue;}26         u = d[1];27         flag = false;28         for(i = 1; i < t; i++){29             if(n + min(n,k) - u < d[i+1]) {flag = true;break;}//利用上次剩餘的30             //造一些 還不夠去死31             theMin = min(n,k);//利用n個人和k個軍營最多造的人數32             k += n - theMin;//當n > k時,剩餘的錢造軍營33             n += theMin - u;//造了theMin個人,然後去打仗,死了一些,最後剩餘34             if(d[i+1] > k){//下一輪進攻軍營不夠用35                 n -= d[i+1]-k;//用一些工人去換d[i+1]-k個人,36                 u = k;//已經死了這麼多個,還要死k個才行37             }else u = d[i+1];38         }39         if(flag) {puts("-1");continue;}40         if(t == 1) printf("%I64d\n",u < k ? u:k);41         else printf("%I64d\n",n);42     }43     return 0;44 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.