Problem Statement

    

You are given an int W. There is a rectangle in the XY-plane with corners at (0, 0), (0, 1), (W, 0), and (W, 1). Let T[x] be the triangle with vertices at (0, 1), (W, 1) and (x, 0). (All points that lie inside the triangle are a part of T[x] as well.)

The objective in this problem is to calculate the area of the region (T[0] xor T[1] xor ... xor T[W]). (See Notes for a formal definition.) The figures below show the region (T[0] xor T[1] xor ... xor T[W]) for W=1,2,3,4,5,6.

  

Return the integer part of the area of the region.

Definition

    
Class: TriangleXor
Method: theArea
Parameters: int
Returns: int
Method signature: int theArea(int W)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 64

Notes

- For sets of points A and B in the XY-plane, the set (A xor B) is defined as the set of all points that lie in exactly one of the sets A and B (i.e., points that belong to the union of A and B but don't belong to their intersection).
- If the exact area is A, the correct return value is floor(A), not round(A). In words: you should return the largest integer that is less than or equal to the exact area.
- The format of the return value was chosen to help you in case of small precision errors. The constraints guarantee that computing the correct area with absolute error less than 0.01 is sufficient to determine the correct return value. The author's solution is significantly more precise than that.

Constraints

- W will be between 1 and 70,000, inclusive.
- The difference between the exact area of the region and the nearest integer will be greater than 0.01.

Examples

0)  
    
1
Returns: 0
The exact area is 0.5.
1)  
    
2
Returns: 1
The area is approximately 1.33333.
2)  
    
3
Returns: 1
The exact area is 1.35.
3)  
    
4
Returns: 2
The area is approximately 2.62857. Note that the correct answer is 2, not 3.
4)  
    
5
Returns: 2
The area is approximately 2.13294.
5)  
    
12345
Returns: 4629
 

题意:给你一个1*n的矩形,按图中方法划线、涂色,问多大面积涂为黄色。

题解:

根据题目中的图,可以用两条对角线把涂色区域分为四个部分。

对于上方部分,若n为偶数,全为黄色;若为奇数,全为黑色。

对于左右部分,通过三角形的相似求出各个等高三角形的底之和与对角线长度的比例,计算面积。

对于下方部分,同样通过相似求出各组等高四边形的底之和与高,计算面积。

代码:

 class JumpFurther
{
public:
int furthest(int N, int badStep)
{
//$CARETPOSITION$
int tot=,x=;
for(int i=;i<=N;i++)
{
tot=tot+i; if(tot==badStep)x--;
}
return tot+x;
}
};

TopCoder[SRM587 DIV 1]:TriangleXor(550)的更多相关文章

  1. TopCoder[SRM587 DIV 1]:ThreeColorability(900)

    Problem Statement      There is a H times W rectangle divided into unit cells. The rows of cells are ...

  2. TopCoder[SRM513 DIV 1]:Reflections(1000)

    Problem Statement      Manao is playing a new game called Reflections. The goal of the game is trans ...

  3. Topcoder SRM584 DIV 2 500

    #include <set> #include <iostream> #include <string> #include <vector> using ...

  4. Topcoder SRM583 DIV 2 250

    #include <string> #include <iostream> using namespace std; class SwappingDigits { public ...

  5. 【补解体报告】topcoder 634 DIV 2

    A:应该是道语文题,注意边界就好: B:开始考虑的太复杂,没能够完全提取题目的思维. 但还是A了!我愚蠢的做法:二分答案加暴力枚举, 枚举的时候是完全模拟的,比如每次取得时候都是从大到小的去取,最后统 ...

  6. Topcoder Srm627 DIV 2

    A,B:很水,注意边界,话说HACK都是这些原因. C: R[I][J]:表示反转I-J能改变冒泡排序的次数: DP方程:dp[i][k]=max(dp[j][k],dp[j][k-1]+dp[j][ ...

  7. Topcoder SRM548 Div 1

    1. KingdomAndTrees 给出n个数a[1..n],求一个数组b[1..n]满足b严格递增,且b[1]>=1. 定义代价为W = max{abs(a[i]-b[i])},求代价最小值 ...

  8. TopCoder[SRM513 DIV 1]:PerfectMemory(500)

    Problem Statement      You might have played the game called Memoria. In this game, there is a board ...

  9. [topcoder]BinaryCards

    现在觉得有空时可以刷一下topcoder的DIV 2的Lvl 3的题目.感觉和刷LeetCode和WikiOi都是不一样的. http://community.topcoder.com/stat?c= ...

随机推荐

  1. Python的历史及介绍

    Python的诞生 Python的创始人吉多·范罗苏姆(Guido van Rossum),在1989年12月的圣诞节期间,为了打发时间,决定开发一种新的脚本解释程序,作为ABC语言的继承. 现在,p ...

  2. node快速入门

    nodejs是一个方兴未艾的技术,近几年一直活跃在各大论坛.其实nodejs也只是新瓶装旧酒,只是它的一些激进的特性使她显得很神秘.nodejs使用的是JavaScript的语法,所以在学习nodej ...

  3. 【记录】spring boot 图片上传与显示

    问题:spring boot 使用的是内嵌的tomcat, 文件上传指定目录时不知道文件上传到哪个地方,不知道访问路径. //部署到服务器的tomcat上时通常使用这种方式request.getSer ...

  4. 【JNDI】Java Naming and Directory Interface

    一.数据源的由来 在Java开发中,使用JDBC操作数据库的四个步骤如下:   ①加载数据库驱动程序(Class.forName("数据库驱动类");)   ②连接数据库(Conn ...

  5. Vue 事件相关实例方法---on/emit/off/once

    一.初始位置 平常项目中写逻辑,避免不了注册/触发各种事件 今天来研究下 Vue 中,我们平常用到的关于 on/emit/off/once 的实现原理 关于事件的方法,是在 Vue 项目下面文件中的 ...

  6. Likecloud-吃、吃、吃

    题目背景 问世间,青春期为何物? 答曰:"甲亢,甲亢,再甲亢:挨饿,挨饿,再挨饿!" 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日 ...

  7. NX二次开发-UFUN打开本地文本文档uc4504

    NX9+VS2012 #include <uf.h> #include <uf_cfi.h> #include <uf_ui.h> using std::strin ...

  8. makefile.new(7117) : error U1087: cannot have : and :: dependents for same target

    makefile.new(7117) : fatal error U1087: cannot have : and :: dependents for same target(2012-05-21 2 ...

  9. python re模块使用

    re.findall() 查找字符 从字符串中找出符合模式的字符序列:findall(模式(正则表达式),目标字符串), 返回值为list类型,list元素为匹配出的各个字符串如: import re ...

  10. 021_IO流

    IO流 I——input 输入 O——Output 输出 文件 File 所有文件类的父类 创建方式 File file = new File("文件路径"); 文件对象常用的方法 ...