HDU 5808 Price List Strike Back

Problem Description
There are  n  shops numbered with successive integers from  1  to  n  in Byteland. Every shop sells only one kind of goods, and the price of the  i -th shop's goods is  vi . The distance between the  i -th shop and Byteasar's home is  di .

Every day, Byteasar will purchase some goods. On the  i -th day, he will choose an interval  [li,ri]  and an upper limit  ci . Then, he will visit each shop with distance at most  ci  away from home in  [li,ri] , buy at most one piece of goods from each shop and go back home. Of course, he can also choose to buy nothing. Back home, Byteasar will calculate the total amount of money he has costed that day and write it down on his account book, denoted as  sumi .

However, due to Byteasar's poor math, he may calculate it wrong. 

Please write a program to help Byteasar judge whether each number is sure to be calculated wrong.
 

Input
The first line of the input contains an integer  T   (1T10) , denoting the number of test cases.

In each test case, the first line of the input contains two integers  n,m   (1n20000,1m100000) , denoting the number of shops and the number of records on Byteasar's account book.

The second line of the input contains  n  integers  v1,v2,...,vn   (1vi100) , denoting the price of the  i -th shop's goods.

The third line of the input contains  n  integers  d1,d2,...,dn   (1di109) , denoting the distance between the  i -th shop and Byteasar's home.

Each of the next  m  lines contains four integers  li,ri,ci,sumi   (1lirin,1ci109,1sumi100) , denoting a record on Byteasar's account book.
 

Output
For each test case, print a line with  m  characters. If the  i -th number is sure to be calculated wrong, then the  i -th character should be '1'. Otherwise, it should be '0'.
 

Sample Input
  
  
2 3 3 3 3 3 2 4 3 3 3 5 3 3 3 3 1 2 3 1 3 5 4 5 1 2 4 2 1 8 9 2 1 1 5 1 3 4 4 1 5 1 5 3 5 1 3 5 1
 

Sample Output
  
  
011

1101

标算的做法,分治,复杂度100(nlogn+m)+mlogn

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef __int64 LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 998244353;
const int N = 2e4 + 10;
const int M = 1e5 + 10;
int T, n, m, x;
int v[N], d[N], f[N][101];
int l[M], r[M], c[M], s[M], ans[M], a[M], b[M];

void solve(int L, int R, int ll, int rr)
{
    if (ll > rr) return;

    if (L == R)
    {
        rep(i, ll, rr) ans[a[i]] = v[R] == s[a[i]] && d[R] <= c[a[i]];
    }
    else
    {
        int mid = L + R >> 1, w1 = ll, w2 = rr, w3 = 0;
        rep(i, ll, rr)
        {
            if (r[a[i]] <= mid) a[w1++] = a[i];
            else if (l[a[i]] > mid) b[w2--] = a[i];
            else b[w3++] = a[i];
        }
        rep(i, w2 + 1, rr) a[i] = b[i];

        if (w3)
        {
            rep(i, 1, 100) f[mid][i] = f[mid + 1][i] = INF;
            f[mid][0] = f[mid + 1][0] = 0;
            f[mid][v[mid]] = d[mid];    f[mid + 1][v[mid + 1]] = d[mid + 1];
            per(i, mid - 1, L)
            {
                per(j, 100, v[i]) f[i][j] = min(f[i + 1][j], max(f[i + 1][j - v[i]], d[i]));
                per(j, v[i] - 1, 0) f[i][j] = f[i + 1][j];
            }
            rep(i, mid + 2, R)
            {
                rep(j, v[i], 100) f[i][j] = min(f[i - 1][j], max(f[i - 1][j - v[i]], d[i]));
                rep(j, 0, v[i] - 1) f[i][j] = f[i - 1][j];
            }
        }
        rep(i, 0, w3 - 1)
        {
            rep(j, 0, s[b[i]]) ans[b[i]] |= max(f[l[b[i]]][j], f[r[b[i]]][s[b[i]] - j]) <= c[b[i]];
        }
        solve(L, mid, ll, w1 - 1);
        solve(mid + 1, R, w2 + 1, rr);
    }
}

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &m);
        rep(i, 1, n) scanf("%d", &v[i]);
        rep(i, 1, n) scanf("%d", &d[i]);
        rep(i, 1, m) a[i] = i, ans[i] = 0;
        rep(i, 1, m) scanf("%d%d%d%d", &l[i], &r[i], &c[i], &s[i]);
        solve(1, n, 1, m);
        rep(i, 1, m) printf("%d", ans[i] ^ 1);
        putchar(10);
    }
    return 0;
}

树状数组+多重背包二进制优化,按道理来说复杂度要炸,然而实际上跑的比分治还快。

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef __int64 LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 998244353;
const int N = 2e4 + 10;
const int M = 1e5 + 10;
int T, n, m, x, y, q, p;
int v[N], d[N], f[101][N];
int l[M], r[M], c[M], s[M], ans[M];
int a[N], b[M], dp[101];

bool cmp1(const int &x, const int &y)
{
    return d[x] < d[y];
}

bool cmp2(const int &x, const int &y)
{
    return c[x] < c[y];
}

void insert(int x, int y)
{
    if (x > p) return;
    for (int i = y; i <= q; i += low(i)) f[x][i]++;
}

int get(int x, int y)
{
    if (x > p) return 0;
    int res = 0;
    for (int i = y; i; i -= low(i)) res += f[x][i];
    return res;
}

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d", &n, &m);
        x = y = q = 0;
        rep(i, 1, n) scanf("%d", &v[i]), x = max(x, v[i]);
        rep(i, 1, n) scanf("%d", &d[i]), a[i] = i;
        rep(i, 1, m)
        {
            scanf("%d%d%d%d", &l[i], &r[i], &c[i], &s[i]);
            b[i] = i; y = max(y, s[i]); q = max(q, r[i]);
        }
        p = min(x, y);
        rep(i, 1, p) rep(j, 1, q) f[i][j] = 0;
        sort(a + 1, a + n + 1, cmp1);
        sort(b + 1, b + m + 1, cmp2);
        for (int i = 1, j = 1; i <= m; i++)
        {
            for (; j <= n&&d[a[j]] <= c[b[i]]; j++) insert(v[a[j]], a[j]);
            int S = s[b[i]], L = l[b[i]], R = r[b[i]];

            if (ans[b[i]] = get(S, R) - get(S, L - 1)) continue;

            rep(k, 1, S) dp[k] = 0;    dp[0] = 1;
            for (int k = 1; !dp[S] && k < S; k++)
            {
                x = min(S / k, get(k, R) - get(k, L - 1));
                for (y = 1; !dp[S] && x; x -= min(x, y), y <<= 1)
                {
                    int cost = min(x, y)*k;
                    for (int w = S; !dp[S] && w >= cost; w--) dp[w] |= dp[w - cost];
                }
            }
            ans[b[i]] = dp[S];
        }
        rep(i, 1, m) printf("%d", ans[i] ? 0 : 1);
        putchar(10);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值