Problem Statement

    

You are given the ints perimeter and area. Your task is to find a triangle with the following properties:

  • The coordinates of each vertex are integers between 0 and 3000, inclusive.
  • The perimeter of the triangle must be exactly perimeter, and its area must be exactly area.

If there are multiple solutions, you may choose any of them. Return a vector <int> with six elements: {x1, y1, x2, y2, x3, y3}, where (x1, y1), (x2, y2), and (x3, y3) are the coordinates of the vertices of your triangle. If there is no solution, return an empty vector <int> instead.

Definition

    
Class: FindThePerfectTriangle
Method: constructTriangle
Parameters: int, int
Returns: vector <int>
Method signature: vector <int> constructTriangle(int area, int perimeter)
(be sure your method is public)

Limits

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

Constraints

- area will be between 1 and 1,000,000, inclusive.
- perimeter will be between 1 and 1000, inclusive.

Examples

0)  
    
6
11
Returns: { }
There are no valid triangles with area 6 and perimeter 11.
1)  
    
6
12
Returns: {1, 1, 1, 4, 5, 4 }
The example output describes a right triangle with vertices at (1, 1), (1, 4) and (5, 4). Its sides have lengths 3, 4, and 5, hence its perimeter is 12. The area of the triangle is (3*4)/2 = 6.
2)  
    
37128
882
Returns: {137, 137, 273, 410, 1, 410 }
 
3)  
    
12
18
Returns: {1, 1, 4, 5, 1, 9 }
In this test case our solution constructed an isosceles triangle with vertices at (1, 1), (4, 5) and (1, 9).
4)  
    
18096
928
Returns: {1, 1, 1, 88, 417, 88 }
 
5)  
    
1000000
1000
Returns: { }
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

题意
给出三角形的面积和周长,找到三个坐标为整数的点,使得以它们为顶点的三角形符合条件,并满足$$$1\le x_i\le 3000, 1\le y_i\le 3000, (i=1,2,3)$$$
分析
假设三条边的长度从大到小依次为$$$a, b, c$$$,通过枚举$$$a$$$和$$$b$$$,可以找到所有面积大小正确的组合。接下来的问题就是如何把这样的三角形放到网格上面。首先,$$$a^2$$$,$$$b^2$$$,$$$c^2$$$必须拆成两个平方数的和,因为周长≤$$$1000$$$,所以$$$a,b,c\lt 1000$$$,只需要对$$$1^2, 2^2,...,1000^2$$$进行预处理,记录它们所有拆成平方数的和的形式。对于一组$$$a, b, c$$$,遍历$$$a$$$和$$$b$$$的拆分方式的组合,假设把$$$a$$$这条边放到($$$0$$$,$$$0$$$)-($$$x_1$$$,$$$y_1$$$),$$$b$$$这条边放到($$$0$$$,$$$0$$$)-($$$x_2$$$, $$$y_2$$$),那么如果满足$$$c^2=(x_1-x_2)^2+(y_1-y_2)^2$$$,就找到了一组坐标。然后只需要把坐标平移到规定的区域内就行了。我选择的是所有坐标平移$$$x+1500,y+1500$$$
总结
第一场topcoder,从将近1点打到2点,熬夜场永远思路不清晰,暴力题都敲不出来(代码该怎么贴呀)
代码
#include<bits/stdc++.h>
using namespace std;
vector<pair<int, int>> cnt[];
class FindThePerfectTriangle
{
public:
vector <int> constructTriangle(int area, int perimeter) {
int c;
for (int i = ; i <= ; ++i)cnt[i*i].emplace_back(i, );
for (int i = ; i <= ; ++i) {
for (int j = ; j<i&&j*j+i*i<=; ++j) {
if (cnt[i*i + j*j].empty())continue;
cnt[i*i + j*j].emplace_back(i, j);
}
}
for (int a = ; a <= && a + < perimeter; a++) {
for (int b = ; b <= && a + b < perimeter&&b <= a; b++) {
c = perimeter - a - b;
if (a < c || b < c || a >= (b + c))continue;
//用海拉公式的变形来验证面积正确
long long p = perimeter;
long long s = p*(p - * a)*(p - * b)*(p - * c);
if (s == (long long)() * area*area) {
int A = a*a, B = b*b;
//枚举A和B的拆分方式
for (int i = ; i<cnt[A].size(); i++)
for (int j = ; j<cnt[B].size(); j++) {
int x1 = cnt[A][i].first, y1 = cnt[A][i].second;
int x2 = cnt[B][j].first, y2 = cnt[B][j].second;
int xx, yy;
for(int s1=-;s1<=;s1+=)
for(int s2=-;s2<=;s2+=){
xx = x1 - s1*x2;
yy = y1 - s2*y2;
if(xx*xx+yy*yy==c*c){
vector<int>res;
res.push_back(); res.push_back();
res.push_back(+x1); res.push_back(+y1);
res.push_back(+s1*x2); res.push_back(+s2*y2);
return res;
}
xx = x1 - s1*y2;
yy = y1 - s2*x2;
if (xx*xx + yy*yy == c*c){
vector<int>res;
res.push_back(); res.push_back();
res.push_back( + x1); res.push_back( + y1);
res.push_back( + s1*y2); res.push_back( + s2*x2);
return res;
}
}
} }
}
}
vector<int>res;
return res;
}
};

topcoder srm 738 div1 FindThePerfectTriangle(枚举)的更多相关文章

  1. Topcoder Srm 726 Div1 Hard

    Topcoder Srm 726 Div1 Hard 解题思路: 问题可以看做一个二分图,左边一个点向右边一段区间连边,匹配了左边一个点就能获得对应的权值,最大化所得到的权值的和. 然后可以证明一个结 ...

  2. Topcoder SRM 643 Div1 250<peter_pan>

    Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...

  3. Topcoder SRM 602 div1题解

    打卡- Easy(250pts): 题目大意:rating2200及以上和2200以下的颜色是不一样的(我就是属于那个颜色比较菜的),有个人初始rating为X,然后每一场比赛他的rating如果增加 ...

  4. TopCoder SRM 605 DIV1

    604的题解还没有写出来呢.先上605的. 代码去practice房间找. 说思路. A: 贪心,对于每个类型的正值求和,如果没有正值就取最大值,按着求出的值排序,枚举选多少个类型. B: 很明显是d ...

  5. topcoder srm 640 div1

    problem1 link 首先使用两个端点颜色不同的边进行连通.答案是$n-1-m$.其中$m$是联通分量的个数. problem2 link 首先构造一个最小割的模型.左边的$n_{1}$个点与源 ...

  6. topcoder srm 635 div1

    problem1 link 首先枚举长度$L$.然后计算每一段长度$L$的差值最大公约数,然后差值除以最大公约数的结果可以作为当前段的关键字.然后不同段就可以比较他们的关键字,一样就是可以转化的. p ...

  7. topcoder srm 630 div1 (2-SAT and SCC template)

    problem1 link 首先计算任意两点的距离.然后枚举选出的集合中的两个点,判断其他点是否可以即可. problem2 link 假设字符串为$s$,长度为$n$.那么对于$SA$中的两个排名$ ...

  8. topcoder srm 600 div1

    problem1 link 首先,如果一个数字的某一位是1但是$goal$的这一位不是1,那么这个数字是不用管它的.那么对于剩下的数字,只需要统计在$goal$为1的位上,这些数字对应位上也是1的数字 ...

  9. topcoder srm 585 div1

    problem1 link 最优的策略就是从最低下一层开始,每两层的三个节点的子树都可以用一次遍历覆盖. problem2 link 从大到小依次放置每一种数字,并记录已经放置的数字一共有多少个$m$ ...

随机推荐

  1. 神器 Tmux 的超绝便利

    服务器的任务不间断运行,就是利用了 tmux 的特性.就是说,一般 ssh 是断开就会停止所有之前连接 ssh 期间运行的所有 processes,而 tmux 的核心业务不在于把屏幕分成几块好看,而 ...

  2. apache-日志-记录post数据

    apache access.log日志只能打印出相关的头部信息,例如:Referer, User-agent.但是我希望看到body中的data. 目前找到解决方案是使用apache的扩展module ...

  3. ThreeJS实现波纹粒子效果

    今天我们来用ThreeJS的库实现一个波纹粒子效果,我们用到的ThreeJS的库有CanvasRenderer.js,OrbitControls.js,Projector.js,stats.min.j ...

  4. Hbase RESTFul API创建namespace返回500

    1.使用官方提供的/namespaces/namespace创建namespace失败,返回500,官方提供示例:/namespaces/namespace POST 创建一个新的namespace. ...

  5. windows下在idea用maven导入spark2.3.1源码并编译并运行示例

    一.前提 1.配置好maven:intellij idea maven配置及maven项目创建 2.下载好spark源码: 二.导入源码: 1.将下载的源码包spark-2.3.1.tgz解压(E:\ ...

  6. 【LDAP安装】在已编译安装的PHP环境下安装LDAP模块

    在已编译安装的PHP环境下安装LDAP模块 (乐维温馨提示:其他模块也能以这个方式安装) 1.在PHP源码包内找到ldap模块文件 cd php-5.6.37 cd ext/ldap/ 2.phpiz ...

  7. 1分钟入门接口自动化框架Karate

    介绍 在这篇文章中,我们将介绍一下开源的Web-API自动化测试框架——Karate Karate是基于另一个BDD测试框架Cucumber来建立的,并且共用了一些相同的思想.其中之一就是使用Gher ...

  8. 温习DL之一:梯度的概念

    1.梯度的概念 梯度是一个矢量,表示某一函数在该点处的方向导数沿着该方向取得最大值,即函数在该点处沿着该方向变化最快. 在微积分里面,对多元函数的参数求∂偏导数,把求得的各个参数的偏导数以向量的形式写 ...

  9. 在虚拟机上搭建物理机可访问的web服务(IIS)

    0x0 前言 安装webug4.0的时候突发奇想,想学下如何在虚拟机里搭建网站,然后让主机像访问互联网的网站一样访问虚拟机的网站,为以后渗透测试搭建环境做准备 0x1 虚拟机安装win2003[以防万 ...

  10. Windows 本地文件搜索神器

    Wox: Windows 本地文件搜索神器 下载地址: https://github.com/Wox-launcher/Wox 注: Wox只能搜索C盘下的文件,所以需要结合everything 如果 ...