TC SRM 671 Div2 500 BearDartsDiv2
2015-10-17 03:32
344 查看
Problem Statement
Limak is an old brown bear who likes to play darts.
Limak just picked up an empty scorecard. He then threw a sequence of darts into a dartboard and for each dart he recorded the point value of the area it hit. You are given a int[] w containing the contents of Limak’s scorecard: a sequence of point values.
Today there is a special jackpot. In order to win the jackpot, the player must present a scorecard with exactly four scores: (a, b, c, d). Additionally, these scores must be such that a*b*c = d. Note that order matters: the scores a, b, c, d must have been obtained in this particular order.
Limak wants to erase all but four scores from his scorecard in such a way that he will win the jackpot. Compute and return the number of different ways in which he can do that.
Definition
Class BearDartsDiv2
Method count
Parameters vector
Returns long long
Method signature long long count(vector w)
(be sure your method is public)
Limits
Time limit (s) 1.000
Memory limit (MB) 256
Notes
Pay attention to the unusual time limit.
Constraints
w will contain between 4 and 500 elements, inclusive.
Each element in w will be between 1 and 10^6, inclusive.
Test cases
w { 10, 2, 2, 7, 40, 160 }
Returns 2
Limak must erase two of the six scores on his scorecard. There are two ways to do that and win the jackpot:
Erase the elements on 0-based indices 3 and 5. You are left with (a,b,c,d) = (10,2,2,40), and 10*2*2 = 40.
Erase the elements on 0-based indices 0 and 3. You are left with (a,b,c,d) = (2,2,40,160), and 2*2*40 = 160.
w { 128, 64, 32, 16, 8, 4, 2, 1 }
Returns 0
Regardless of which four scores Limak erases, the remaining four will always have a > b > c > d and therefore it cannot be the case that a*b*c = d.
w { 2, 3, 4, 5, 6, 8, 12, 16, 20, 24, 40, 24, 20, 16, 12, 8, 6, 5, 4, 3, 2 }
Returns 3
w { 100, 100, 100, 1000000, 1000000, 1000000, 1000000, 1000000 }
Returns 5
w { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
Returns 2573031125
解析
meet in mid
把等式改写成a*b=d/c就变成O(N^2)的枚举啦!感谢@好地方bug的指导。可以写出如下代码(A就是题上的w)
比赛的时候O(N^3)暴力乱搞,连最后一个样例都跑不过TAT。长者教导我还是要提高姿势水平……
而且我发现TC的时间好像测不准……上面的代码第一次T了,后来交又A了。
Limak is an old brown bear who likes to play darts.
Limak just picked up an empty scorecard. He then threw a sequence of darts into a dartboard and for each dart he recorded the point value of the area it hit. You are given a int[] w containing the contents of Limak’s scorecard: a sequence of point values.
Today there is a special jackpot. In order to win the jackpot, the player must present a scorecard with exactly four scores: (a, b, c, d). Additionally, these scores must be such that a*b*c = d. Note that order matters: the scores a, b, c, d must have been obtained in this particular order.
Limak wants to erase all but four scores from his scorecard in such a way that he will win the jackpot. Compute and return the number of different ways in which he can do that.
Definition
Class BearDartsDiv2
Method count
Parameters vector
Returns long long
Method signature long long count(vector w)
(be sure your method is public)
Limits
Time limit (s) 1.000
Memory limit (MB) 256
Notes
Pay attention to the unusual time limit.
Constraints
w will contain between 4 and 500 elements, inclusive.
Each element in w will be between 1 and 10^6, inclusive.
Test cases
w { 10, 2, 2, 7, 40, 160 }
Returns 2
Limak must erase two of the six scores on his scorecard. There are two ways to do that and win the jackpot:
Erase the elements on 0-based indices 3 and 5. You are left with (a,b,c,d) = (10,2,2,40), and 10*2*2 = 40.
Erase the elements on 0-based indices 0 and 3. You are left with (a,b,c,d) = (2,2,40,160), and 2*2*40 = 160.
w { 128, 64, 32, 16, 8, 4, 2, 1 }
Returns 0
Regardless of which four scores Limak erases, the remaining four will always have a > b > c > d and therefore it cannot be the case that a*b*c = d.
w { 2, 3, 4, 5, 6, 8, 12, 16, 20, 24, 40, 24, 20, 16, 12, 8, 6, 5, 4, 3, 2 }
Returns 3
w { 100, 100, 100, 1000000, 1000000, 1000000, 1000000, 1000000 }
Returns 5
w { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
Returns 2573031125
解析
meet in mid
把等式改写成a*b=d/c就变成O(N^2)的枚举啦!感谢@好地方bug的指导。可以写出如下代码(A就是题上的w)
map<LL,LL> cnt; LL ans=0; cnt[A[0]*A[1]]++; for(int i=2;i<N-1;i++)//i枚举题中的c { for(int j=i+1;j<N;j++) if(A[j]%A[i]==0) ans+=cnt[A[j]/A[i]]; for(int j=0;j<i;j++) cnt[A[j]*A[i]]++; }
比赛的时候O(N^3)暴力乱搞,连最后一个样例都跑不过TAT。长者教导我还是要提高姿势水平……
而且我发现TC的时间好像测不准……上面的代码第一次T了,后来交又A了。
相关文章推荐
- android 26 设置项目有多个入口Activity。
- Leetcode 3Sum
- Amazon OA2准备——循环链表插入
- android 25 跨进程启动activity
- Linux下php安装Redis扩展
- android 24 设置与桌面相同的action和category
- Leetcode Two Sum II - Input array is sorted
- powershell 调用winform dll
- android 23 启动带2个Categories值的预定义acticity和桌面activity
- [前言]
- 进程和线程的区别?
- 第五次作业——团队项目——需求规格说明书
- 我体验过的可以用的XCode插件
- 第五次作业——团队项目——需求规格说明书
- 第五次作业——团队项目——需求规格说明书
- Modern Operating System --- Power Management (OS Issues)
- Android日志收集
- android 22 启动带2个action值的预定义acticity
- UNITY 5.2 GameObject类
- 团队项目的整体计划安排