zoukankan      html  css  js  c++  java
  • 有道破题~~

    很郁闷的一道“有道难题”:

    这个是在线初赛我抽到的500题目,意思很简单,描述如下:

    Problem Statement
       
    双倍超立方数是指一个正整数可以正好被拆分为两种不同的a^3+b^3的方式,其中a,b均为整数且0<a<=b。对于任何一个指定的 int n, 返回所有的小于等于n的双倍超立方数的个数。
    Definition
       
    Class:
    TwiceSuperCubic
    Method:
    count
    Parameters:
    int
    Returns:
    int
    Method signature:
    int count(int n)
    (be sure your method is public)
       

    Constraints
    -
    n取值范围为1到1,000,000,000(含)
    Examples
    0)

       
    1
    Returns: 0

    1)

       
    1729
    Returns: 1
    1729=1^3+12^3 1729=9^3+10^3
    2)

       
    475574
    Returns: 27

    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.

        首先声明,为了不泄题,我等到所有比赛结束后才发,题目意思看上去很简单,不过却是要仔细看要“求什么”再写代码。我开始就把题目意思理解成对于一个数n,返回他有多少种情况(不同a,b)存在a^3+b^3=n了...(topcoder做多了太想当然了...)就是这个破原因费了我好多的时间,后来test之后发现题意理解重大偏差..然后飞速修改代码,可以通过所有test了,可是我发现对于example[2]我就耗时1.8s了,那么对于1,000,000,000的数据量,完全就算不出来,我写的是一个接近O(n^3)的垃圾算法,虽然做了很多优化。。。一般topcoder的题目的数据量是很小的,又想当然了。。
        这该怎么办呢,显然要想个全新的算法,可是时间剩下不到10分钟了(一共60分钟2题),就在这是我肚子好痛,唉!只好先上个厕所想一想了,正蹲着突然想到了一个算法,这种算法是一种逆向思维,先把对于所有数对的立方和小于n的数全部求出来,存在一个map中,然后再针对n求解,可惜正当我调试的时候,唉...时间到了,垃圾算法也没提交,最后就考第一个题目得了229分...太遗憾了!!(祈祷第一题不要有什么差错啊...)

      反正要全心准备考研,诅咒自己不要进复赛!!

     1#include <iostream>
     2#include <sstream>
     3#include <algorithm>
     4#include <string>
     5#include <utility>
     6#include <map>
     7#include <set>
     8#include <list>
     9#include <stack>
    10#include <queue>
    11#include <cctype>
    12#include <vector>
    13#include <bitset>
    14#include <cmath>
    15#include <functional>
    16//#include <hash_map>
    17//#include <fstream>
    18//#include <cstdlib>
    19//#include <ctime>
    20
    21#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
    22#define FORR(i, a, b) for(int i = (a); i <= (b); ++i)
    23#define BE(x) (x).begin(), (x).end()
    24
    25using namespace std;
    26//using namespace stdex;
    27
    28typedef vector<string> VS;
    29typedef vector<int> VI;
    30typedef long long LL;
    31
    32class TwiceSuperCubic
    33{
    34public:
    35    int count(int n)
    36    {
    37        int result = 0, max = (int)pow((double)n, 1.0/3), temp;
    38        VI cubic;
    39        FORR(i, 0, max)
    40        {
    41            temp = i*i*i;
    42            cubic.push_back(temp);
    43        }

    44        //res[i]表示
    45        map<intint> res;
    46        FORR(i, 1, max)
    47        {
    48            FORR(j, i, max)
    49            {
    50                temp = cubic[i] + cubic[j];
    51                if (temp <= n)
    52                    res[temp]++;
    53            }

    54        }

    55        map<intint>::const_iterator it = res.begin();
    56        while(it != res.end())
    57        {
    58            if(it->second == 2)
    59                result++;
    60            it++;
    61        }

    62        return result;
    63    }

    64}
    ;
    65
    66int main()
    67{
    68    TwiceSuperCubic x;
    69    //return 1546 with less than 2s
    70    cout << x.count(1000000000);
    71    return 0;
    72}
  • 相关阅读:
    在类中声明常量
    PHPStudy配置虚拟主机配置域名步骤
    PHPStudy配置虚拟主机步骤
    2019年7月22日星期一,简单的总结一下
    简单的面向对象
    session和cookie
    Jquery 事件绑定 bind 与on 的区别
    php try catch用法
    include,include_once,require,require_once的区别
    require与 include区别
  • 原文地址:https://www.cnblogs.com/CCBB/p/1493212.html
Copyright © 2011-2022 走看看