zoukankan      html  css  js  c++  java
  • topcoder srm 738 div1 FindThePerfectTriangle(枚举)

    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.

    题意
    给出三角形的面积和周长,找到三个坐标为整数的点,使得以它们为顶点的三角形符合条件,并满足$$$1le x_ile 3000, 1le y_ile 3000, (i=1,2,3)$$$
    分析
    假设三条边的长度从大到小依次为$$$a, b, c$$$,通过枚举$$$a$$$和$$$b$$$,可以找到所有面积大小正确的组合。接下来的问题就是如何把这样的三角形放到网格上面。首先,$$$a^2$$$,$$$b^2$$$,$$$c^2$$$必须拆成两个平方数的和,因为周长≤$$$1000$$$,所以$$$a,b,clt 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[1000006];
    class FindThePerfectTriangle
    {
        public:
        vector <int> constructTriangle(int area, int perimeter) {
            int  c;
            for (int i = 1; i <= 1000; ++i)cnt[i*i].emplace_back(i, 0);
            for (int i = 1; i <= 1000; ++i) {
                for (int j = 1; j<i&&j*j+i*i<=1000000; ++j) {
                    if (cnt[i*i + j*j].empty())continue;
                    cnt[i*i + j*j].emplace_back(i, j);
                }
            }
            for (int a = 1; a <= 1000 && a + 1 < perimeter; a++) {
                for (int b = 1; b <= 1000 && 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 - 2 * a)*(p - 2 * b)*(p - 2 * c);
                    if (s == (long long)(16) * area*area) {
                        int A = a*a, B = b*b;
                        //枚举A和B的拆分方式
                        for (int i = 0; i<cnt[A].size(); i++) 
                            for (int j = 0; 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=-1;s1<=1;s1+=2)
                                    for(int s2=-1;s2<=1;s2+=2){
                                        xx = x1 - s1*x2;
                                        yy = y1 - s2*y2;
                                        if(xx*xx+yy*yy==c*c){
                                            vector<int>res;
                                            res.push_back(1500); res.push_back(1500);
                                            res.push_back(1500+x1); res.push_back(1500+y1);
                                            res.push_back(1500+s1*x2); res.push_back(1500+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(1500); res.push_back(1500);
                                            res.push_back(1500 + x1); res.push_back(1500 + y1);
                                            res.push_back(1500 + s1*y2); res.push_back(1500 + s2*x2);
                                            return res;
                                        }
                                    }    
                            }
                        
                    }
                }
            }
            vector<int>res;
            return res;
        }
    };
  • 相关阅读:
    图像处理之基础---特征向量的 几何意义
    图像处理之基础---仿射变换
    图像处理之基础---周末戏说卷积
    图像处理之基础---内积和外积
    图像处理之基础---最小二乘积
    图像处理之基础---大话小波和卷积
    嵌入式开发之项目---遥控小车课题设计
    ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 04. 中间件
    ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 03. 服务注册和管道
    ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 02. Web Host 的默认配置
  • 原文地址:https://www.cnblogs.com/tobyw/p/9734428.html
Copyright © 2011-2022 走看看