机房水题欢乐赛 2016-04-22 下午

水炸了。。。

T1: Word

给出一个原串和n个字符串。
判断原串是否存在两个不相交子串按顺序连接起来在n个字符串中存在一样的。
输出一样的个数。

【输入样例】

ABCBABA
2
BAAB
ABBA

【输出样例】

1

【样例解释】

BAAB这个单词是无法出现在句子中的。
ABBA这个单词则可以出现在句子中,当a=1,b=2,c=4,d=5或者a=1,b=2,c=6,d=7时即可。

【数据约定】

40%数据:m<=1000,每个单词长度小于等于10
100%数据:2<=m<=10^5,1<=n<=100,每个单词长度在1到1000之间

Solution

枚举两个子串中间点,那么一个在左边一个在右边。
正反做两次KMP,算出原串在某个位置能匹配到的最远长度,然后对前缀求最大值即可。

T2: 水题

长度为N的区间内只有7和4两种数字,
2个操作:
switch l r:把[l,r]的4换成7,7换成4.
count 询问n个数的最长不下降子序列长度.

【输入样例1】

2 3
47
count
switch 1 2
count

【输出样例1】

2
1

【输入样例2】

3 5
747
count
switch 1 1
count
switch 1 3
count

【输出样例2】

2
3
2

【数据约定】

20%数据:n<=100,m<=100
100%数据:1<=n<=10^6,1<=m<=3*10^5

Solution

线段树维护。每个节点保存47,74,44,77的最长长度。那么答案就显而易见了。

一个伪代码

struct Node {
    int a44, a47, a74, a77, tag;
} tree[N * 4];
void pushdown(int x) {
    if (tree[x].tag) {
        swap(tree[x].a44, tree[x].a77);
        swap(tree[x].a47, tree[x].a74);
        tree[x*2].tag ^= 1;
        tree[x*2+1].tag ^= 1;
        tree[t].tag = 0;
    }
}
void update(int x) {
    pushdown(x * 2);
    pushdown(x * 2 + 1);
    Node l = tree[t * 2], r = tree[t * 2 + 1];
    tree[x].a44 = l.a44 + r.a44;;
    tree[x].a77 = l.a77 + r.a77;
    tree[x].a47 = max(max(l.a44+r.a47,l,a47,r.a77),l.a44,r.a77);
    tree[x].a74 = max(max(l.a77+r.a74,l,a74,r.a44),l.a77,r.a44);
}
void build(int x, int l, int r) {
    if (l == r) {
        if (a[l] == 4) tree[x].a44 = 1;
        else tree[x].a77 = 1;
        return;
    }
    int mid = l + r >> 1;
    build(x * 2, l, mid);
    build(x * 2 + 1, mid + 1, r);
    update(x);
}
void revert(int x, int l, int r, int ql, int qr) {
    if (ql <= l && r <= qr) {
        tree[x].tag ^= 1;
        return;
    }
    pushdown(x);
    int mid = l + r >> 1;
    if (ql <= mid) revert(x * 2, l, mid, ql, qr);
    if (mid < qr) revert(x * 2 + 1, mid + 1, r, ql, qr);
    update(x);
}
int query() {
    pushdown(1);
    Node r = tree[1].
    return max(max(r.a44, r.a77), r.a47);
}
int main() {
    ...
    return 0;
}

T3: 超级大水题

存在数列 {wi}
对于每个询问 (a,b) ,有个计算程序:

for(sum=0,k=0;a+k*b<=n;++k)
    sum+=w[a+k*b];
print(sum)

【输入描述】

第一行n
第二行n个数,为wi
第三行m
接下来m行每行2个数a,b(1<=a,b<=n)

【输出描述】

输出m行,每行求出询问(a,b)所对应的sum

【输入样例】

4
2 3 5 7
3
1 3
2 3
2 2

【输出样例】

9
3
10

【数据约定】

30%数据,n,m<=1000
100%数据: 1<=n,m<=3*10^5,1<=wi<=10^9,

Solution

根号算法
如果 bn ,那么sum不会算超过 n 次,暴力。
否则,一个简单dp。

这个在2015-12-20做过。。

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 300001, M = N;
typedef unsigned long long ll;

int read() {
    int s = 0; char ch = getchar();
    for (; ch < '0' || ch > '9'; ch = getchar());
    for (; '0' <= ch && ch <= '9'; ch = getchar()) s = s * 10 + ch - '0';
    return s;
}

struct Query {
    ll a, b, i;
    friend bool operator <(const Query &a, const Query &b) {
        return a.b < b.b;
    }
} queries[M];
ll sum[N + 500], ans[M];
int n, w[N];

inline ll brute_force(const Query &q) {
    ll s = 0;
    for (ll k = q.a; k <= n; k += q.b) s += w[k];
    return s;
}

int main() {
    int i, j, m;
    n = read();
    for (i = 1; i <= n; i++) w[i] = read();
    m = read();
    for (i = 1; i <= n; i++) queries[i].a = read(), queries[i].b = read(), queries[i].i = i;
    sort(queries + 1, queries + 1 + m);
    for (i = 1; i <= m; i++) {
        if (queries[i].b * queries[i].b > n) ans[queries[i].i] = brute_force(queries[i]);
        else {
            if (queries[i].b != queries[i - 1].b)
                for (j = n; j > 0; j--)
                    sum[j] = w[j] + sum[j + queries[i].b];
            ans[queries[i].i] = sum[queries[i].a];
        }
    }
    for (i = 1; i <= m; i++)
        printf("%I64d\n", ans[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值