Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

如果你 T(n * m) 却 TLE……

Posted by ShenNaizhi at 2024-08-20 02:29:59 on Problem 1742
Data sets 组数不确定,注意常数大小。

排名前几 200ms+ 过题实在是太恐怖了,我自认为 T(n * m) 的写法居然都跑了 2300ms+ ……本质上是对于每种硬币,按该硬币面值的个个余数来做处理,这样就能保证每种硬币的处理控制在 T(m)。

代码:
// #undef LOCAL
// #undef LOCAL_IO

#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <bitset>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <algorithm>
#include <complex>
#include <functional>
#include <initializer_list>
#include <iterator>
#include <limits>
#include <new>
#include <string>
#include <utility>
#include <iostream>
#include <iomanip>
typedef long long i64;
using namespace std;

#ifdef LOCAL
#   define debug(x) cout << #x << " = " << x << endl
#   define debugv(v) cout << #v << ": ["; for (auto iter = v.begin();\
    iter != v.end(); ) { cout << *iter << (++iter == v.end() ? "" : ", "); }\
    cout << ']' << endl
#   define debugstmt(s) {s}
#else
#   define debug(...)
#   define debugv(...)
#   define debugstmt(...)
#endif

const int MAXM = 100000;
const int MAXN = 100;
const int INF = 1e8;

bool dp[MAXM + 5];
int a[MAXN + 5], c[MAXN + 5];

void solve(const int n, const int m) {
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    for (int i = 0; i < n; i++) scanf("%d", &c[i]);
    memset(dp, 0, sizeof(dp));
    dp[0] = true;
    for (int i = 0; i < n; i++) {
        if (a[i] > m) continue;
        for (int j = 0; j < a[i] && j + a[i] <= m; j++) {
            int maxPrev = -INF;
            for (int k = j; k <= m; k += a[i]) {
                if (!dp[k]) {
                    dp[k] = (maxPrev >= k - c[i] * a[i]);
                } else {
                    maxPrev = k;
                }
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= m; i++) ans += dp[i];
    printf("%d\n", ans);
}

void solve() {
    while (true) {
        int n, m;
        scanf("%d%d", &n, &m);
        if (n == 0 && m == 0) break;
        solve(n, m);
    }
}

int main() {
    #ifdef LOCAL_IO
        freopen("t.in", "r", stdin);
        freopen("t.out", "w", stdout);
    #endif
    int _ = 1;
    // scanf("%d", &_);

    while (_--) {
        solve();
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator