Google Code Jam Qualification Round 2014 Problem D. Deceitful War 题解
2014-04-13 10:53
567 查看
本文同时发布在我的个人网站,欢迎访问:http://www.lilongdream.com/2014/04/13/129.html
Naomi and Ken sometimes play games together. Before they play, each of them gets Nidentical-looking blocks of wood with masses between 0.0kg and 1.0kg (exclusive). All of the blocks have
different weights. There are lots of games they could play with those blocks, but they usually play something they call War. Here is how War works:
Each player weighs each of his or her own blocks, so each player knows the weights of all of his or her own blocks, but not the weights of the other player's blocks.
They repeat the following process N times:
Naomi chooses one of her own blocks, with mass
Naomi tells Ken the mass of the block she chose.
Ken chooses one of his own blocks, with mass
They each put their block on one side of a balance scale, and the person whose block is heavier
gets one point.
Both blocks are destroyed in a fire.
Naomi has realized three things about War. First, she has realized that she loses a lot. Second, she has realized that there is a unique strategy that Ken can follow to maximize his points without assuming
anything about Naomi's strategy, and that Ken always uses it. Third, she has realized that she hates to lose. Naomi has decided that instead of playing War, she will play a game she calls Deceitful War. The great thing about Deceitful War is that Ken will
think they're playing War!
Here is how Deceitful War works, with differences between Deceitful War and War in bold:
Each player weighs each of his or her own blocks. Naomi also weighs Ken's blocks while he isn't looking, so Naomi knows the weights of all blocks and Ken only knows the weights of his own blocks.
They repeat the following process N times:
Naomi chooses one of her own blocks, with mass
Naomi tells Ken a number,
number Naomi just told him is
Ken chooses one of his own blocks, with mass
They each put their block on one side of a balance scale, and the person whose block is heavier
gets one point.
Both blocks are destroyed in a fire.
Naomi doesn't want Ken to know that she isn't playing War; so when she is choosing which block to play, and what mass to tell Ken, she must make sure that the balance scale won't reveal that
It might seem like Naomi won't win any extra points by being deceitful, because Ken might discover that she wasn't playing War; but Naomi knows Ken thinks both players are playing War, and she knows what
he knows, and she knows Ken will always follow his unique optimal strategy for War, so she can always predict what he will play.
You'll be given the masses of the blocks Naomi and Ken started with. Naomi will play Deceitful War optimally to gain the maximum number of points. Ken will play War optimally to gain the maximum number
of points assuming that both players are playing War. What will Naomi's score be? What would it have been if she had played War optimally instead?
If each player has a single block left, where Naomi has 0.5kg and Ken has 0.6kg, then Ken is guaranteed to score the point. Naomi can't say her number is ≥ 0.6kg, or Ken will know she isn't playing War
when the balance scale shows his block was heavier.
If each player has two blocks left, where Naomi has [0.7kg, 0.2kg] and Ken has [0.8kg, 0.3kg], then Naomi could choose her 0.2kg block, and deceive Ken by telling him that she chose a block that was 0.6kg.
Ken assumes Naomi is telling the truth (as in how the War game works) and will play his 0.8kg block to score a point. Ken was just deceived, but he will never realize it because the balance scale shows that his 0.8kg block is, like he expected, heavier than
the block Naomi played. Now Naomi can play her 0.7kg block, tell Ken it is 0.7kg, and score a point. If Naomi had played War instead of Deceitful War, then Ken would have scored two points and Naomi would have scored zero.
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line containing a single integer N, the
number of blocks each player has. Next follows a line containing N space-separated real numbers: the masses of Naomi's blocks, in kg. Finally there will be a line containing N space-separated real numbers: the masses of Ken's
blocks, in kg.
Each of the masses given to Ken and Naomi will be represented as a 0, followed by a decimal point, followed by 1-5 digits. Even though all the numbers in the input have 1-5 digits after the decimal point,
Ken and Naomi don't know that; so Naomi can still tell Ken that she played a block with mass 0.5000001kg, and Ken has no reason not to believe her.
For each test case, output one line containing "Case #x: y z", where x is the test case number (starting from 1), y is the number of
points Naomi will score if she plays Deceitful War optimally, and z is the number of points Naomi will score if she plays War optimally.
1 ≤ T ≤ 50.
All the masses given to Ken and Naomi are distinct, and between 0.0 and 1.0 exclusive.
1 ≤ N ≤ 10.
1 ≤ N ≤ 1000.
分析:
如果为War规则,则Ken的对策如下:当Naomi取的木块重量大于自己当前的最大值时,则出自己当前最轻的木块,否则出自己所有大于Naomi木块中的最轻者。
对于Deceitful War,Naomi的对策是,如何当前木块小于Ken的最小值,则谎称自己的木块在Ken最大值和第二大值之间,这样Ken会出最大值,否则谎称自己的木块大于Ken最大值,这时Ken会出最小值。即如果必输,则输的最有价值,否则尽可能以最小的代价赢,并且不能被Ken发现。
为方便处理,对数据进行排序,不影响结果。
参考代码:
Problem
Naomi and Ken sometimes play games together. Before they play, each of them gets Nidentical-looking blocks of wood with masses between 0.0kg and 1.0kg (exclusive). All of the blocks havedifferent weights. There are lots of games they could play with those blocks, but they usually play something they call War. Here is how War works:
Each player weighs each of his or her own blocks, so each player knows the weights of all of his or her own blocks, but not the weights of the other player's blocks.
They repeat the following process N times:
Naomi chooses one of her own blocks, with mass
ChosenNaomi.
Naomi tells Ken the mass of the block she chose.
Ken chooses one of his own blocks, with mass
ChosenKen.
They each put their block on one side of a balance scale, and the person whose block is heavier
gets one point.
Both blocks are destroyed in a fire.
Naomi has realized three things about War. First, she has realized that she loses a lot. Second, she has realized that there is a unique strategy that Ken can follow to maximize his points without assuming
anything about Naomi's strategy, and that Ken always uses it. Third, she has realized that she hates to lose. Naomi has decided that instead of playing War, she will play a game she calls Deceitful War. The great thing about Deceitful War is that Ken will
think they're playing War!
Here is how Deceitful War works, with differences between Deceitful War and War in bold:
Each player weighs each of his or her own blocks. Naomi also weighs Ken's blocks while he isn't looking, so Naomi knows the weights of all blocks and Ken only knows the weights of his own blocks.
They repeat the following process N times:
Naomi chooses one of her own blocks, with mass
ChosenNaomi.
Naomi tells Ken a number,
ToldNaomi, between 0.0kg and 1.0kg exclusive.Ken, who thinks they're playing War, thinks the
number Naomi just told him is
ChosenNaomi.
Ken chooses one of his own blocks, with mass
ChosenKen.
They each put their block on one side of a balance scale, and the person whose block is heavier
gets one point.
Both blocks are destroyed in a fire.
Naomi doesn't want Ken to know that she isn't playing War; so when she is choosing which block to play, and what mass to tell Ken, she must make sure that the balance scale won't reveal that
ChosenNaomi ≠ ToldNaomi. In other words, she must make decisions so that:
ChosenNaomi > ChosenKenif, and only if,
ToldNaomi > ChosenKen, and
ToldNaomiis not equal to the mass of any of Ken's blocks, because he knows that isn't possible.
It might seem like Naomi won't win any extra points by being deceitful, because Ken might discover that she wasn't playing War; but Naomi knows Ken thinks both players are playing War, and she knows what
he knows, and she knows Ken will always follow his unique optimal strategy for War, so she can always predict what he will play.
You'll be given the masses of the blocks Naomi and Ken started with. Naomi will play Deceitful War optimally to gain the maximum number of points. Ken will play War optimally to gain the maximum number
of points assuming that both players are playing War. What will Naomi's score be? What would it have been if she had played War optimally instead?
Examples
If each player has a single block left, where Naomi has 0.5kg and Ken has 0.6kg, then Ken is guaranteed to score the point. Naomi can't say her number is ≥ 0.6kg, or Ken will know she isn't playing Warwhen the balance scale shows his block was heavier.
If each player has two blocks left, where Naomi has [0.7kg, 0.2kg] and Ken has [0.8kg, 0.3kg], then Naomi could choose her 0.2kg block, and deceive Ken by telling him that she chose a block that was 0.6kg.
Ken assumes Naomi is telling the truth (as in how the War game works) and will play his 0.8kg block to score a point. Ken was just deceived, but he will never realize it because the balance scale shows that his 0.8kg block is, like he expected, heavier than
the block Naomi played. Now Naomi can play her 0.7kg block, tell Ken it is 0.7kg, and score a point. If Naomi had played War instead of Deceitful War, then Ken would have scored two points and Naomi would have scored zero.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with a line containing a single integer N, thenumber of blocks each player has. Next follows a line containing N space-separated real numbers: the masses of Naomi's blocks, in kg. Finally there will be a line containing N space-separated real numbers: the masses of Ken's
blocks, in kg.
Each of the masses given to Ken and Naomi will be represented as a 0, followed by a decimal point, followed by 1-5 digits. Even though all the numbers in the input have 1-5 digits after the decimal point,
Ken and Naomi don't know that; so Naomi can still tell Ken that she played a block with mass 0.5000001kg, and Ken has no reason not to believe her.
Output
For each test case, output one line containing "Case #x: y z", where x is the test case number (starting from 1), y is the number ofpoints Naomi will score if she plays Deceitful War optimally, and z is the number of points Naomi will score if she plays War optimally.
Limits
1 ≤ T ≤ 50.All the masses given to Ken and Naomi are distinct, and between 0.0 and 1.0 exclusive.
Small dataset
1 ≤ N ≤ 10.
Large dataset
1 ≤ N ≤ 1000.
Sample
Input | |
4 1 0.5 0.6 2 0.7 0.2 0.8 0.3 3 0.5 0.1 0.9 0.6 0.4 0.3 9 0.186 0.389 0.907 0.832 0.959 0.557 0.300 0.992 0.899 0.916 0.728 0.271 0.520 0.700 0.521 0.215 0.341 0.458 | |
Output | |
Case #1: 0 0 Case #2: 1 0 Case #3: 2 1 Case #4: 8 4 |
如果为War规则,则Ken的对策如下:当Naomi取的木块重量大于自己当前的最大值时,则出自己当前最轻的木块,否则出自己所有大于Naomi木块中的最轻者。
对于Deceitful War,Naomi的对策是,如何当前木块小于Ken的最小值,则谎称自己的木块在Ken最大值和第二大值之间,这样Ken会出最大值,否则谎称自己的木块大于Ken最大值,这时Ken会出最小值。即如果必输,则输的最有价值,否则尽可能以最小的代价赢,并且不能被Ken发现。
为方便处理,对数据进行排序,不影响结果。
参考代码:
#include <iostream> #include <vector> #include <algorithm> int main() { int T; std::cin >> T; std::vector<double> naomi1; std::vector<double> ken1; std::vector<double> naomi2; std::vector<double> ken2; int y, z; int N; for(int round = 1; round <= T; ++round) { std::cin >> N; double weight; naomi1.clear(); ken1.clear(); naomi2.clear(); ken2.clear(); for(int i = 0; i < N; ++i) { std::cin >> weight; naomi1.push_back(weight); } for(int i = 0; i < N; ++i) { std::cin >> weight; ken1.push_back(weight); } std::sort(naomi1.begin(), naomi1.end()); std::sort(ken1.begin(), ken1.end()); naomi2.assign(naomi1.begin(), naomi1.end()); ken2.assign(ken1.begin(), ken1.end()); y = 0; for(int i = 0; i < N; ++i) { if(naomi1[0] < ken1[0]) { naomi1.erase(naomi1.begin()); ken1.pop_back(); } else { naomi1.erase(naomi1.begin()); ken1.erase(ken1.begin()); y++; } } z = 0; for(int i = 0; i < N; ++i) { if(naomi2[0] > ken2[ken2.size() - 1]) { z = naomi2.size(); break; } else { for(int j = 0; j < ken2.size(); ++j) { if(ken2[j] < naomi2[0]) { continue; } else { naomi2.erase(naomi2.begin()); ken2.erase(ken2.begin() + j); break; } } } } std::cout << "Case #" << round << ": " << y << " " << z << std::endl; } return 0; }感谢阅读!
相关文章推荐
- Google Code Jam 2014 Problem D. Deceitful War
- Google Code Jam Qualification Round 2014 Problem B. Cookie Clicker Alpha 题解
- Google Code Jam Qualification Round 2014 Problem A. Magic Trick 题解
- Google Code Jam 2014 资格赛:Problem D. Deceitful War
- Google Code Jam 2012 Qualification Round. Problem D. Hall of Mirrors
- Google Code Jam Africa 2010 Qualification Round Problem A. Store Credit
- Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words
- Google Code Jam Qualification Round 2015 Problem B. Infinite House of Pancakes
- Google Code Jam 2009 Qualification Round Problem A. Alien Language
- Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation
- Google Code Jam 2014 Qualification Round C.Minesweeper Master 和 Microsoft某实习机试试题(快速求组合)
- dp - Google Code jam Qualification Round 2015 --- Problem B. Infinite House of Pancakes
- Google Code Jam 2009 Qualification Round Problem B. Watersheds
- Google Code Jam 2009 Qualification Round Problem C. Welcome to Code Jam
- Google Code jam Qualification Round 2015 --- Problem A. Standing Ovation
- dp - Google Code jam Qualification Round 2015 --- Problem B. Infinite House of Pancakes
- Google CodeJam Qualification Round 2014
- Google Code Jam 2014 Round 1 A:Problem A Charging Chaos
- Google Code Jam 2014 Round 1B Problem B
- GoogleCodeJam Qualification Round 2014 ABD