UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)

Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others.
One day, there is another tribe become their target. The strong tribe has decide to terminate them!!!
There are m villages in the other tribe. Each village contains a troop with attack power EAttacki
,
and defense power EDefensei
. Our tribe has n troops to attack the enemy. Each troop also has the
attack power Attacki
, and defense power Defensei
. We can use at most one troop to attack one enemy
village and a troop can only be used to attack only one enemy village. Even if a troop survives an
attack, it can’t be used again in another attack.
The battle between 2 troops are really simple. The troops use their attack power to attack against
the other troop simultaneously. If a troop’s defense power is less than or equal to the other troop’s
attack power, it will be destroyed. It’s possible that both troops survive or destroy.
The main target of our tribe is to destroy all the enemy troops. Also, our tribe would like to have
most number of troops survive in this war.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case start
with 2 numbers n and m, the number of our troops and the number of enemy villages. n lines follow,
each with Attacki and Defensei
, the attack power and defense power of our troops. The next m lines
describe the enemy troops. Each line consist of EAttacki and EDefensei
, the attack power and defense
power of enemy troops
Output
For each test ease, output one line containing ‘Case #x: y’, where x is the test case number (starting
from 1) and y is the max number of survive troops of our tribe. If it‘s impossible to destroy all enemy
troops, output ‘-1’ instead.

 

题目大意:每个军队有一个攻击力和防御力,当一个军队的攻击力大于等于对方的防御力,就可以摧毁敌军(可以双方同时阵亡)。现给出我方和敌方所有军队的攻击力和防御力,问要击溃敌方所有军队,最多能保留多少的存活兵力(只能单挑)。

思路:贪心,可参考田忌赛马。

把我方按攻击力从大到小排序,把敌方按防御力从大到小排序。

遍历敌方的军队,设当前敌方军队为enemy。

要击溃enemy,首先我方派出军队的攻击力要大于等于enemy的防御力。如果我方能不损兵干掉对面,就贪心选择一个防御力最小的但又大于enemy的攻击力的军队。如果必须损兵,就贪心地选择一个防御力最小的军队。这个可以用multiset维护。

正确性就不证啦,不服你举个反例。

PS:比赛的时候吧multiset写成了set卡了几个小时呵呵呵呵……

 

代码(0.082S):

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <set>
 6 using namespace std;
 7 
 8 const int MAXN = 100010;
 9 
10 struct Node {
11     int atk, def;
12     void read() {
13         scanf("%d%d", &atk, &def);
14     }
15 };
16 
17 Node a[MAXN], b[MAXN];
18 int n, m, T;
19 
20 int solve() {
21     multiset<int> st;
22     int res = 0;
23     for(int i = 0, j = 0; j < m; ++j) {
24         while(i < n && a[i].atk >= b[j].def)
25             st.insert(a[i++].def);
26         if(st.empty()) return -1;
27         auto it = st.upper_bound(b[j].atk);
28         if(it != st.end()) st.erase(it);
29         else st.erase(st.begin()), res++;
30     }
31     return n - res;
32 }
33 
34 int main() {
35     scanf("%d", &T);
36     for(int t = 1; t <= T; ++t) {
37         scanf("%d%d", &n, &m);
38         for(int i = 0; i < n; ++i) a[i].read();
39         for(int i = 0; i < m; ++i) b[i].read();
40         sort(a, a + n, [](Node x, Node y) {
41             return x.atk > y.atk;
42         });
43         sort(b, b + m, [](Node x, Node y) {
44             return x.def > y.def;
45         });
46         printf("Case #%d: %d\n", t, solve());
47     }
48 }
View Code

 

转载于:https://www.cnblogs.com/oyking/p/4501554.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The Mongol Empire was one of the largest empires in history, stretching from Eastern Europe to Asia. It was founded by Genghis Khan in 1206 and lasted until the mid-14th century. The empire was known for its military prowess, innovative tactics, and the use of terror to intimidate its enemies. Genghis Khan was born in 1162 in the region that is now Mongolia. He was born into a family of nomads and grew up herding sheep and hunting. At the age of 16, he was captured by a rival tribe and enslaved. He escaped and later began to unite the various tribes of the region under his leadership. In 1206, he was declared the supreme ruler of the Mongol Empire. Under Genghis Khan's leadership, the Mongol Empire began to expand rapidly. Genghis Khan was a brilliant military strategist and his army was known for its speed and mobility. The Mongols were also skilled horsemen and archers, and they were able to defeat much larger armies using innovative tactics such as feigned retreats and surprise attacks. After Genghis Khan's death in 1227, his empire was divided among his four sons. Over the next few decades, the Mongol Empire continued to expand under the leadership of Genghis Khan's descendants. They conquered China, Central Asia, and parts of Eastern Europe. The Mongols were known for their brutality and the use of terror to intimidate their enemies. They were also tolerant of other religions and cultures, and they allowed conquered peoples to retain their own customs and traditions. One of the most famous Mongol leaders was Kublai Khan, who ruled China from 1279 to 1294. He founded the Yuan Dynasty, which was the first foreign dynasty to rule China. Kublai Khan was a patron of the arts and literature, and he encouraged the development of new technologies such as paper money and gunpowder. He also sent emissaries to Europe, including Marco Polo, who wrote about his travels in China. The Mongol Empire began to decline in the mid-14th century. The empire had become too large to be effectively governed, and there were internal divisions and conflicts among the ruling families. The Black Death, which swept through Europe and Asia in the mid-14th century, also had a devastating impact on the Mongol Empire. By the end of the 14th century, the Mongol Empire had disintegrated into several smaller states. Despite its reputation for brutality, the Mongol Empire had a significant impact on world history. The Mongols were responsible for the spread of ideas, technologies, and cultures across Eurasia. They also played a role in the development of international trade and commerce. The Mongol Empire was a significant force in world history, and its legacy can still be felt today.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值