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}
  • 相关阅读:
    设计模式之原型模式
    Mac OSx下的APK反编译
    Android中ListView封装收缩与展开
    带密码登录的密码保险箱应用源码
    HTML 5缓存机制:Cache Manifest配置实例
    PHP开发大型项目的一点经验
    Windows Phone 编程: 摇一摇 效果
    swift皮筋弹动发射飞机
    Win8.1应用开发之适配器模式(C#实现)
    java实现身份证校验
  • 原文地址:https://www.cnblogs.com/CCBB/p/1493212.html
Copyright © 2011-2022 走看看