TCO 2013 round 1C TheKnights
来源:互联网 发布:linux 修改帐号密码 编辑:程序博客网 时间:2024/05/22 00:11
绝对是大神级别的题目,第一次做感觉完全没有思路,看解答之后感觉:其实也挺简单的。。。
这题主要是涉及概率论中的期望计算,以及期望的线性性质。
数据类型的转换非常重要,否则有可能会造成溢出。
题目分析在这,非常非常好:http://apps.topcoder.com/wiki/display/tc/TCO+2013+Round+1C
#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>using namespace std;// About probability and expected values// Take advantage of the linearity of expected values// and divide the original problems// Transformation of data type is very important in solving this problemclass TheKnights {public:double find(int, int, int);double single(int n, int a, int b);double inter(int n, int a, int b);};// Only can be attacked by one knightdouble TheKnights::single(int n, int a, int b) {//define directionsconst double dx[] = {a, b, -a, -b, 0, a, b, -a, -b};const double dy[] = {b, -a, b, -a, 0, -b, a, -b, a};double res = 0;double p = 1.0/((double)n*n);int i = 9;if(a == b)i = 5;for(int j=0; j<i; j++){double minx = max(0.0, dx[j]);double maxx = min(n-1+dx[j], (double)n-1);double miny = max(0.0, dy[j]);double maxy = min(n-1+dy[j], (double)n-1);double x = max(0.0, maxx-minx+1);double y = max(0.0, maxy-miny+1);//cout<<"x: "<<x<<endl;//cout<<"y: "<<y<<endl;res += (x*y);}//cout<<"single: "<<res<<endl;return res*p;}// Can be attacked by two knightsdouble TheKnights::inter(int n, int a, int b) {//define directionsconst double dx[] = {a, b, -a, -b, 0, a, b, -a, -b};const double dy[] = {b, -a, b, -a, 0, -b, a, -b, a};double res = 0;double p = 2.0/(((double)n*n)*(((double)n*n)-1));int i = 9;if(a == b)i = 5;for(int j=0; j<i; j++)for(int k=(j+1); k<i; k++){double minx = max(0.0, max(dx[j], dx[k]));double maxx = min((double)n-1, min(n-1+dx[j], n-1+dx[k]));double miny = max(0.0, max(dy[j], dy[k]));double maxy = min((double)n-1, min(n-1+dy[j], n-1+dy[k]));double x = max(0.0, maxx-minx+1);double y = max(0.0, maxy-miny+1);res += (x*y);}//cout<<"inter: "<<res<<endl;return res*p;}double TheKnights::find(int n, int a, int b) {return 2*single(n,a,b)-inter(n,a,b);}//<%:testing-code%>//Powered by [KawigiEdit] 2.0!
- TCO 2013 round 1C TheKnights
- TCO 2013 Round 1A
- 2005 TCO Online Round 1
- 2012 TCO Algorithm - Round 1A
- TCO round 3A
- TCO 2015 Round 1A DIV 1 1000
- 2011TCO Qual Round A
- 2013 TCO round2B EllysFigurines
- 【TCO 2013】Litpanels
- 【TCO 2013 WildCard】SemiMultiple
- 2013 TCO Algorithm Round 2B - Division I, Level Two ScotlandYard
- TCO'09 Elimination Round 2 PlaneFractal recursion
- TCO round 2A level3 EllysReversals
- 【TCO 2013 3A】TrichyInequality
- 推理题 2014 TCO Round 2B 500 SumAndProductPuzzle
- Topcoder 2016 TCO Algorithm Round 3B Hard
- Topcoder 2017 TCO Algorithm Round 3A Hard
- Topcoder 2017 TCO Algorithm Round 3A Hard
- 数据结构面试题:josephus排列,循环链表
- javascript语法
- PHP-数据库类
- 动态规划推荐题目(持续更新)
- Jee - JBoss AS7 JNDI Datasource - JBoss JNDI 数据源配置(MySQL配置)
- TCO 2013 round 1C TheKnights
- 【OpenStack】【转载】OpenStack中的测试
- AJAX - 向服务器发送请求
- windows8环境下卸载Oracle步骤
- JAVA的循环
- 翻转字符串
- 黑马程序员-面向对象概述 构造函数与this关键字
- Libevent调用
- Andriod :ADB server didn't ACK