upc 組隊賽18 STRENGTH【貪心模擬】

STRENGTH

題目鏈接

題目描述

Strength gives you the confidence within yourself to overcome any fears, challenges or doubts. Feel the fear and do it anyway! If you have been going through a rough time and feel burnt out or stressed, the Strength card encourages you to find the strength within yourself and keep going. You have got what it takes to see this situation through to its eventual end. You might also feel compelled to hold space for someone else who is going through a difficult period and needs your strength and support.
Alice and Bob are playing ``Yu-Gi-Oh!'', a famous turn-based trading card game, in which two players perform their turns alternatively. After several turns, Alice and Bob have many monsters respectively. Alice has n and Bob has m monsters under their own control. Each monster's strength is measured by a non-negative integer si. To be specific, the larger si is, the more power the monster has.
During each turn, for every single monster under control, the player can give a command to it at most once, driving it to battle with an enemy monster (given that opposite player has no monsters as a shield, the monster can directly attack him).
Additionally, the process of the battle is also quite simple. When two monsters battle with each other, the stronger one (i.e. the one with larger si) will overwhelm the other and destroy it and the winner's strength will remain unchanged. Meanwhile, the difference of their strength will produce equivalent damage to the player who loses the battle. If the player is directly attacked by a monster, he will suffer from the damage equal to the monster's strength. Notice that when two monsters have the same strength, both of them will vanish and no damage will be dealt.
Right now it is Alice's turn to play, having known the strength of all monsters, she wants to calculate the maximal damage she can deal towards Bob in one turn. Unfortunately, Bob has great foresight and is well-prepared for the upcoming attack. Bob has converted several of his monsters into defense position, in which even if the monster is destroyed, he wouldn't get any damage.
Now you are informed of the strength of all the monsters and whether it is in defense position for each Bob's monster, you are expected to figure out the maximal damage that could be dealt in this turn.

輸入

The first line contains a single integer T≤20 indicating the number of test cases.
For each test case, the first line includes two integer O≤n,m≤100000, representing the number of monsters owned by Alice and Bob.
In next three lines, the first two lines include n and m integers O≤si≤109 indicating the strength of the i-th monster, separated by spaces. The last line contains m integers 0 or 1 indicating the position of Bob’Si-th monsters. In other words, 0 represents the normal position and 1 represents the defense position.

輸出

For the ith test, output a single line in beginning of ``Case i:'', followed by an integer indicating the answer, separated by a single space.

樣例輸入

2
4 2
10 10 10 20
5 15
0 1
4 2
10 10 10 20
5 25
0 1

樣例輸出

Case 1: 25
Case 2: 15

題意

玩游戲王牌,我方場上n只怪獸,都是攻擊形態,戰斗力為a[i];
敵方m只怪獸,戰斗力為b[i],如果下一行是0表示攻擊形態,1表示防御形態
如果我方怪獸的戰斗力攻擊對方攻擊形態的怪獸,就會消滅對方的怪獸,并造成超殺(對敵人本體造成戰斗力的差值a[i] - b[i]的血量),如果攻擊防御形態的怪獸獸,則會消滅怪獸而不會扣對方本體的血量
如果敵人沒有怪獸了,就可以對敵人本體造成直接傷害a[i];
每只怪獸只能攻擊一次
問怎么才能扣敵人最多的血

題解

有兩種貪心策略
一種是 直接拿戰斗力高的去懟敵人戰斗力低的攻擊形態怪,這樣造成的超殺值高
第二種 用戰斗力高的去消滅所有的防御怪和攻擊怪,然后直接對本體進行攻擊
分別求出兩種貪心策略的結果求max
代碼寫的相當繁瑣

代碼

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define scac(x) scanf("%c",&x)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pri3(x,y,z) printf("%d %d %d\n",x,y,z)
#define prl(x) printf("%lld\n",x)
#define prl2(x,y) printf("%lld %lld\n",x,y)
#define prl3(x,y,z) printf("%lld %lld %lld\n",x,y,z)
#define ll long long
#define LL long long
#define read read()
#define pb push_back
#define mp make_pair
#define P pair<int,int>
#define PLL pair<ll,ll>
#define PI acos(1.0)
#define eps 1e-6
#define inf 1e17
#define INF 0x3f3f3f3f
#define N 205
const int maxn = 1e5+5;
ll a[maxn],b[maxn];
ll g[maxn];//gong
ll s[maxn];//shou
int vis[maxn];
int op;
bool cmp(ll x,ll y)
{return x > y;
}
int main()
{int t;sca(t);int kase = 0;while(t--){memset(vis,0,sizeof(vis));int n,m;sca2(n,m);rep(i,0,n)  scl(a[i]);rep(i,0,m)  scl(b[i]);int cntg = 0;int cnts = 0;rep(i,0,m){sca(op);if(op) s[cnts++] = b[i];else   g[cntg++] = b[i];}sort(a,a+n,cmp); // da -> xiaosort(g,g+cntg); // xiao -> dasort(s,s+cnts,cmp); // da -> xiaoint posa = 0;int posg = 0;int poss = cnts-1;ll ans = -1;ll temp = 0;while(posa < n && posg < cntg) //plan1 先打攻擊怪{if(a[posa] >= g[posg]){temp += a[posa] - g[posg]; posa++;posg++;}elsebreak;}ll sum = 0;if(posg != cntg) //我方怪打完了,對方攻擊怪還有剩余{ans = max(ans, temp);}else{int i = n-1;while(i >= posa && poss >= 0)//開始打防守怪{if(a[i] >= s[poss]) {i--;poss--;}else{sum += a[i];i--;}}if(poss == -1)  {temp += sum;if(i != posa){while(i>=posa){temp += a[i];i--;}}}ans = max(ans,temp);}posa = n-1;poss = cnts - 1;while(posa >= 0 && poss >= 0) //plan2 先打防守怪{if(a[posa] >= s[poss]){vis[posa] = 1;posa--;poss--;}else{posa--;}}if(poss == -1) {posa = 0;posg = cntg - 1;temp = 0;while(posa < n && posg >= 0) {if(vis[posa]) //前面拿來打防守怪了{posa++;continue;}if(a[posa] >= g[posg]) {temp += a[posa] - g[posg];posa++;posg--;}else{break;}}if(posg == -1) {while(posa < n){if(vis[posa]){posa++;continue;}temp += a[posa];posa++;}ans = max(ans,temp);}}printf("Case %d: %lld\n",++kase,ans);}return 0;}

轉載于:https://www.cnblogs.com/llke/p/10822416.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/275185.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/275185.shtml
英文地址,請注明出處:http://en.pswp.cn/news/275185.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

JSONNull

最近用JSONObject&#xff0c;感覺比xml好用一些&#xff0c;json的打包和解包都比較清晰和容易&#xff0c;最近遇到一個問題&#xff0c;將一個JSON對象解析&#xff0c;存到hashmap中去&#xff0c;然后再從hashmap取出數據&#xff0c;遇到jsonnull的問題&#xff0c;本以為…

“這張圖告訴你什么?”

For data to be impactful, it must be understood.為了使數據具有影響力&#xff0c;必須理解它。 I’ve happily spent hundreds and hundreds of hours of my life watching users misunderstand data visualizations. I’m strangely hooked on it.我快樂地度過了數百個小…

我們從 UmiJS 遷移到了 Vite

大家好&#xff0c;我是若川。最近組織了源碼共讀活動&#xff0c;感興趣的可以點此加我微信ruochuan12 進群參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。已進行三個月了&#xff0c;很多小伙伴表示收獲頗豐。我們從 UmiJS遷移到 Vite 已經上線半年…

將DataTable的內容以EXCEl的形式導出到本地

1.在搞項目的時候一般會遇到&#xff0c;將GridView或者Repeater的內容以Excel的形式保存到本地&#xff0c;即導出功能。我總結了兩個方法。 方法一&#xff1a; 1 DataTable dt query.GetItems().GetDataTable();2 if (dt ! null)3 {4 …

智能家居數據庫設計_設計更智能的數據表

智能家居數據庫設計重點 (Top highlight)Data tables are hard. There are many different ways to think about them. So, naturally, the first step would be to figure out what your users need.數據表很難。 有許多不同的方式來考慮它們。 因此&#xff0c;自然地&#x…

可能是全網首個前端源碼共讀活動,誠邀你加入一起學習

大家好&#xff0c;我是若川。眾所周知&#xff0c;從8月份開始&#xff0c;我組織了源碼共讀活動&#xff0c;每周學習200行左右的源碼&#xff0c;到現在持續了3個多月&#xff0c;堅持答疑解惑。幫助了不少人&#xff0c;還是挺開心的。另外&#xff0c;涌現了很多優秀的讀者…

vsftpd 的配置項目

基本配置說明&#xff1a; 1&#xff09;local_root/ftpfile(當本地用戶登入時&#xff0c;將被更換到定義的目錄下&#xff0c;默認值為各用戶的家目錄) 2&#xff09;anon_root/ftpfile(使用匿名登入時&#xff0c;所登入的目錄) 3&#xff09;use_localtimeYES(默認是GMT時…

線段樹專輯——pku 3667 Hotel

http://poj.org/problem?id3667 哈哈&#xff0c;經典中的經典題啊。利用線段樹求最大連續空閑區間&#xff0c;并返回空閑區間的起點坐標。 View Code 1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 using namespace std; 5 6 …

houseparty不流暢_重新設計Houseparty –用戶體驗案例研究

houseparty不流暢Houseparty has become very popular during the COVID-19 period because it helps you connect with others in a fun way. The concept is simple, you open the app and jump on a video call with your friends. You can even play online games with the…

你不知道的 Node.js 工具函數

從類型判斷說起在 JavaScript 中&#xff0c;進行變量的類型校驗是一個非常令人頭疼的事&#xff0c;如果只是簡單的使用 typeof 會到各種各樣的問題。舉幾個簡單的&#x1f330;&#xff1a;console.log(typeof null) // object console.log(typeof new Array) // object cons…

Java應用集群下的定時任務處理方案(mysql)

今天來說一個Java多機部署下定時任務的處理方案。 需求: 有兩臺服務器同時部署了同一套代碼&#xff0c; 代碼中寫有spring自帶的定時任務&#xff0c;但是每次執行定時任務時只需要一臺機器去執行。 當拿到這個需求時我腦子中立馬出現了兩個簡單的解決方案&#xff1a; 利用ip…

概念驗證_設置成功的UX概念驗證

概念驗證用戶體驗/概念證明/第1部分 (USER EXPERIENCE / PROOF OF CONCEPT / PART 1) This is the first article of a four-part series. Please read Part 2 and Part 3.這是由四個部分組成的系列文章的第一篇。 請閱讀 第2 部分 和 第3部分 。 How do today’s top UX desi…

從 vue3 和 vite 源碼中,我學到了一行代碼統一規范團隊包管理器的神器

1. 前言大家好&#xff0c;我是若川。最近組織了源碼共讀活動&#xff0c;感興趣的可以加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。已進行四個月了&#xff0c;很多小伙伴表示收獲頗豐。想學源碼&#xff0c;極力推薦之前我寫…

什么事接口

假設你設計一個和人交流的程序。 先建立一個接口 interface 人 //定義接口&#xff0c;它代表一個人&#xff0c; {void Hello(); }//接口虛函數&#xff0c;用來跟這個人說話 但不同的人有不用的交流方式&#xff0c;具體方式用類來實現&#xff0c;比如。 class 美國人&#…

6個高效辦公的Excel小技巧,學會讓你高效辦公

很多人在做Excel表格的時候&#xff0c;會出現下面這種情況&#xff1a;好不容易把內容都輸入好了&#xff0c;才發現文字或是數字的排列組合需要重新調整&#xff0c;這個時候頭就大了&#xff0c;到底是要一個個復制黏貼&#xff0c;還是要刪除后再添加&#xff1f;不管哪種方…

unity 完美像素_像素完美

unity 完美像素從Kidpix到設計系統 (From Kidpix to design systems) Did you ever create stamps in KidPix? Kidpix is bitmap drawing software that’s been around since the nineties, and I remember many happy — more like maddening — hours creating tiny pixela…

整整4個月了,盡全力組織了源碼共讀活動~

大家好&#xff0c;我是若川。從8月份到現在11月結束了。每周一期&#xff0c;一起讀200行左右的源碼&#xff0c;撰寫輔助文章&#xff0c;截止到現在整整4個月了。由寫有《學習源碼整體架構系列》20余篇的若川【若川視野公眾號號主】傾力組織&#xff0c;召集了各大廠對于源碼…

kvm 學習(二)

Linux下 如何通過命令行使用現有的鏡像創建、啟動kvm虛擬機 這里假定已經創建好了相應的鏡像&#xff1a; eg&#xff1a;我這里制作的鏡像名稱為zu1-centos7.img # lszu1-centos7.img 1、拷貝這個鏡像到某一個目錄 cp zu1-centos7.img /data2/ 2、編寫鏡像的配置文件&#x…

字節內部前端開發手冊(完整版)開放下載!

備戰2022&#xff0c;準備好了嗎&#xff1f;據字節HR部門發布的最新信息&#xff0c;2019年以來字節連續3年擴招&#xff0c;而即將到來的2022年春招前端崗位數不低于3000&#xff0c;雖連年擴招&#xff0c;但是報錄比卻從2019年的3%下降到今年的1%。BAT等一線大廠同樣有類似…

EBS中Java并發程序筆記(1)

在Oracle EBS中的Java并發程序&#xff08;Java Concurrent Program&#xff09;是系統功能中的一個亮點&#xff0c;它的出現使得用戶可以在ERP系統中運行自己定義的Java程序。本文為學習筆記&#xff0c;所以不會介紹太多背景知識。 使用Java并發程序的好處&#xff1a; 當遇…