BestCoder #86

BestCoder #86

今年暑假最后一次BC了,结果B题少加了个判断终测WA了,很不爽......

1001 Price List [hdu 5804]签到题

求出所有数的和sumsum,如果q > sumq>sum那么肯定记多了。

时间复杂度O(n)O(n)

以上是引用官方题解...

#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;

//#pragma comment(linker, "/STACK:1024000000,1024000000")

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second
#define lson            l, mid, rt << 1
#define rson            mid + 1, r, rt << 1 | 1

typedef __int64  LL;
//typedef long long LL;
typedef unsigned int uint;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const double PI = 3.1415926536;
const double eps = 1e-6;
const int MAXN = 100000 + 5;

int T, N, M;
int V[MAXN];
LL Q, SUM;

int main() {
#ifndef ONLINE_JUDGE
    FIN;
    // FOUT;
#endif // ONLINE_JUDGE
    scanf ("%d", &T);
    while (T--) {
        scanf ("%d %d", &N, &M);
        SUM = 0LL;
        for (int i = 1; i <= N; i++) {
            scanf ("%d", &V[i]);
            SUM += V[i];
        }
        for (int i = 1; i <= M; i++) {
            scanf ("%I64d", &Q);
            if (Q > SUM) {
                putchar ('1');
                continue;
            } else {
                putchar ('0');
            }
        }
        putchar ('\n');
    }
    return 0;
}

1002 NanoApe Loves Sequence[hdu 5805]

求出前ii个数里相邻差值的最大值f_ifiiinn里相邻差值的最大值g_igi,那么ans=\sum_{i=1}^n \max(|A_{i-1}-A_{i+1}|,f_{i-1},g_{i+1})ans=i=1nmax(Ai1Ai+1,fi1,gi+1)

时间复杂度O(n)O(n)

以上是引用官方题解...

我的做法是求出N个数相邻的最大值,然后将删除第一个节点和删除最后一个节点和删除中间节点分开来算。

#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;

//#pragma comment(linker, "/STACK:1024000000,1024000000")

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second
#define lson            l, mid, rt << 1
#define rson            mid + 1, r, rt << 1 | 1

typedef __int64  LL;
//typedef long long LL;
typedef unsigned int uint;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const double PI = 3.1415926536;
const double eps = 1e-6;
const int MAXN = 100000 + 5;

int T, N, M;
int A[MAXN];
PII F[MAXN];
int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif // ONLINE_JUDGE
    scanf ("%d", &T);
    while (T--) {
        scanf ("%d", &N);
        for (int i = 1; i <= N; i++) {
            scanf ("%d", &A[i]);
        }
        LL res = 0LL;
        for (int i = 1; i <= N - 1; i++) {
            F[i].fst = abs (A[i + 1] - A[i]);
            F[i].snd = i;
        }
        sort (F + 1, F + N);
        reverse (F + 1, F + N);
        /// delete 1
        if (F[1].snd == 1) res += F[2].fst;
        else res += F[1].fst;
        /// delete N
        if (F[1].snd == N - 1) res += F[2].fst;
        else res += F[1].fst;
        /// delete 2 ~ N-1
        for (int i = 2, w; i <= N - 1; i++) {
            w = abs (A[i + 1] - A[i - 1]);
            int p = 1;
            while (F[p].snd == i || F[p].snd == i - 1) p++;
            if(p == N) res += w;    /// 竟然在这里没有加判断, Too Young Too Simple!...
            else res += max (w, F[p].fst);
        }
        printf("%I64d\n", res);
    }
    return 0;
}

1002 NanoApe Loves Sequence[hdu 5806]


将不小于mm的数看作11,剩下的数看作00,那么只要区间内11的个数不小于kk则可行,枚举左端点,右端点可以通过two-pointer求出。

时间复杂度O(n)O(n)

看了题解之后恍然大悟的感觉...

#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;

//#pragma comment(linker, "/STACK:1024000000,1024000000")

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second
#define lson            l, mid, rt << 1
#define rson            mid + 1, r, rt << 1 | 1

typedef __int64  LL;
//typedef long long LL;
typedef unsigned int uint;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

const int MAXN = 200000 + 5;

int T, N, M, K;
int pre[MAXN];

int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif // ONLINE_JUDGE
    scanf ("%d", &T);
    while (T--) {
        scanf ("%d %d %d", &N, &M, &K);
        pre[0] = 0;
        for (int i = 1, temp; i <= N; i++) {
            scanf ("%d", &temp);
            pre[i] = pre[i - 1] + (int) (temp >= M);
        }
        LL res = 0LL;
        int L, R, v, p = 1;
        for (int i = 1; i <= N; i++) {
            L = i, R = K + L - 1;
            if (R > N) break;
            v = pre[L - 1] + K;
            while (p <= N && pre[p] < v) p++;
            if (pre[p] < v) break;
            res += N - p + 1;
        }
        printf ("%I64d\n", res);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值