UVa 1583

自己写的,利用find函数每次查找到第一个就停止,这也是最小元来做的,就是= =刚开始把十万的数据量看成一万了。

#pragma warning(disable:4996)
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iostream>
#include<time.h>
#include<map> 
#include<set>
#include<sstream>
#include<functional>
#include<cassert>
#include<list>
#include<iterator>
#include<utility>
#include <stdexcept>  
#include <sstream>
#include <fstream> 
#include<unordered_map>
#include<unordered_set>
#include<ctype.h>


using namespace std;
using namespace std::placeholders;

int main()
{
    int n, T;
    cin >> T;
    vector<int> a;
    a.reserve(110000);
    for (int i = 0; i <= 100000; i++)
    {
        int e, b, c, d,f;
        f = i / 10000;
        e = i / 1000 - f * 10;
        b = i / 100 - e * 10 - f * 100;
        c = i / 10 - e * 100 - b * 10 - f * 1000;
        d = i % 10;
        a.push_back(i + e + b + c + d+f);
    }
    while (T--)
    {
        cin >> n;
        auto x = find(a.begin(), a.end(), n);
        if (x == a.end())
            cout << 0 << endl;
        else
            cout << x - a.begin() << endl;
    }
    return 0;
}

打完表加写数据到txt文件总共Time:15.356;检查发现大约14.5s的时间是find函数的原因。
可能是uva后台数据不是100000组,最终耗时2170ms,所以估计后台是两万组数据,= =好险
差点超了3000ms的上限。

#pragma warning(disable:4996)
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iostream>
#include<time.h>
#include<map> 
#include<set>
#include<sstream>
#include<functional>
#include<cassert>
#include<list>
#include<iterator>
#include<utility>
#include <stdexcept>  
#include <sstream>
#include <fstream> 
#include<unordered_map>
#include<unordered_set>
#include<ctype.h>


using namespace std;
using namespace std::placeholders;

int ans[100005];

int main()
{
    int n, T;
    memset(ans, 0, sizeof(ans));
    for (int m = 1; m < 100005; m++)
    {
        int x = m, y = m;
        while (x > 0)
        {
            y += x % 10;
            x /= 10;
        }
        if (ans[y] == 0 || m < ans[y])
            ans[y] = m;
    }
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        printf("%d\n", ans[n]);
    }
    return 0;
}

白书的Time:0.135= =;因为直接通过下标访问不需要查找,因而非常快。
白书后台10ms= =比我优化200倍。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值