最新文章

[SCOI2005]扫雷 题解

[SCOI2005]扫雷 题解

题目地址:洛谷:【P2327】[SCOI2005]扫雷 – 洛谷、BZOJ:P 

[CQOI2007]余数求和 题解

[CQOI2007]余数求和 题解

题目地址:洛谷:【P2261】[CQOI2007]余数求和 – 洛谷、BZOJ 

[SDOI2009]虔诚的墓主人 题解

[SDOI2009]虔诚的墓主人 题解

题目地址:洛谷:【P2154】[SDOI2009]虔诚的墓主人 – 洛谷、BZOJ:Problem 1227. — [SDOI2009]虔诚的墓主人

题目描述

小W是一片新造公墓的管理人。公墓可以看成一块N×M的矩形,矩形的每个格点,要么种着一棵常青树,要么是一块还没有归属的墓地。
当地的居民都是非常虔诚的基督徒,他们愿意提前为自己找一块合适墓地。为了体现自己对主的真诚,他们希望自己的墓地拥有着较高的虔诚度。
一块墓地的虔诚度是指以这块墓地为中心的十字架的数目。一个十字架可以看成中间是墓地,墓地的正上、正下、正左、正右都有恰好k棵常青树。
小W希望知道他所管理的这片公墓中所有墓地的虔诚度总和是多少。

输入输出格式

输入格式:
输入文件religious.in的第一行包含两个用空格分隔的正整数N和M,表示公墓的宽和长,因此这个矩形公墓共有(N+1) ×(M+1)个格点,左下角的坐标为(0, 0),右上角的坐标为(N, M)。
第二行包含一个正整数W,表示公墓中常青树的个数。
第三行起共W行,每行包含两个用空格分隔的非负整数xi和yi,表示一棵常青树的坐标。输入保证没有两棵常青树拥有相同的坐标。
最后一行包含一个正整数k,意义如题目所示。

输出格式:
输出文件religious.out仅包含一个非负整数,表示这片公墓中所有墓地的虔诚度总和。为了方便起见,答案对2,147,483,648取模。

输入输出样例

输入样例#1:

5 6
13
0 2
0 3
1 2
1 3
2 0
2 1
2 4
2 5
2 6
3 2
3 3
4 3
5 2
2

输出样例#1:

6

说明

图中,以墓地(2, 2)和(2, 3)为中心的十字架各有3个,即它们的虔诚度均为3。其他墓地的虔诚度为0。
对于30%的数据,满足1 ≤ N, M ≤ 1,000。
对于60%的数据,满足1 ≤ N, M ≤ 1,000,000。
对于100%的数据,满足1 ≤ N, M ≤ 1,000,000,000,0 ≤ xi ≤ N,0 ≤ yi ≤ M,1 ≤ W ≤ 100,000,1 ≤ k ≤ 10。
存在50%的数据,满足1 ≤ k ≤ 2。
存在25%的数据,满足1 ≤ W ≤ 10000。

题解

有一些分可以用[eq]O(n^2)[/eq]乱搞过。具体来说就是预处理每个格子上下左右的常青树数量扫一遍格子。N和M太大的时候可以离散化搞一下,容易发现我们的算法其实是[eq]O(w^2)[/eq]的复杂度。
想把[eq]O(n^2)[/eq]搞成[eq]O(n \log n)[/eq]的算法,需要用到数据结构的辅助。假设每个格子上面、下面、左边、右边的常青树数量分别是u、d、l、r,我们发现每个格子对答案的贡献实际上是\mathrm{C}_u^k \times \mathrm{C}_d^k \times \mathrm{C}_l^k \times \mathrm{C}_r^k。如果我们一个格子一个格子地计算,肯定是不行的,考虑用数据结构维护一个维度的信息,这里我们维护的是区间\mathrm{C}_u^k \times \mathrm{C}_d^k的和。可以先把常青树按照坐标双关键字排序。由于每棵常青树只会对它所在行下面的该列格子产生影响,我们可以边处理边更新这个常青树所在列的值。处理同行的每两棵常青树之间的墓区间的上述和,再乘以左右两边常青树的数量即可。这个求前缀和可以使用常数小的树状数组来做。
模数非常特殊,是[eq]2^{31}[/eq],我们可以用unsigned int自然溢出。

代码

// Code by KSkun, 2018/4
#include <cstdio>
#include <cctype>

#include <vector>
#include <algorithm>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF 
        : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    register char c = fgc();
    while(!isdigit(c)) {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(isdigit(c)) {
        res = (res << 1) + (res << 3) + c - '0';
        c = fgc();
    }
    return res * neg;
}

const int MAXN = 200005;

int n, m, w, k;

unsigned int C[MAXN][15];

inline void calc(int x) {
    C[0][0] = 1;
    for(int i = 1; i <= x; i++) {
        for(int j = 0; j <= std::min(i, k); j++) {
            C[i][j] += C[i - 1][j];
            if(j > 0) C[i][j] += C[i - 1][j - 1];
        }
    }
}

unsigned int tree[MAXN];

inline int lowbit(int x) {
    return x & -x;
}

inline void add(int x, int v) {
    for(int i = x; i <= w; i += lowbit(i)) {
        tree[i] += v;
    }
}

inline unsigned int query(int x) {
    unsigned int res = 0;
    for(int i = x; i; i -= lowbit(i)) {
        res += tree[i];
    }
    return res;
}

struct Tree {
    int x, y;
} trees[MAXN];

inline bool cmp(Tree a, Tree b) {
    return a.x != b.x ? a.x < b.x : a.y < b.y;
}

std::vector<int> tmpx, tmpy;
int rcnt[MAXN], ccnt[MAXN], nrcnt, nccnt[MAXN];

int main() {
    n = readint(); m = readint(); w = readint();
    for(int i = 1; i <= w; i++) {
        trees[i].x = readint(); trees[i].y = readint();
        tmpx.push_back(trees[i].x); tmpy.push_back(trees[i].y);
    }
    k = readint();
    calc(w);
    tmpx.push_back(-1); tmpy.push_back(-1);
    // 离散化
    std::sort(tmpx.begin(), tmpx.end());
    tmpx.erase(std::unique(tmpx.begin(), tmpx.end()), tmpx.end());
    std::sort(tmpy.begin(), tmpy.end());
    tmpy.erase(std::unique(tmpy.begin(), tmpy.end()), tmpy.end());
    for(int i = 1; i <= w; i++) {
        trees[i].x = std::lower_bound(tmpx.begin(), tmpx.end(), trees[i].x) - tmpx.begin();
        trees[i].y = std::lower_bound(tmpy.begin(), tmpy.end(), trees[i].y) - tmpy.begin();
        rcnt[trees[i].x]++; ccnt[trees[i].y]++;
    }
    std::sort(trees + 1, trees + w + 1, cmp);
    unsigned int ans = 0;
    for(int i = 1; i < w; i++) {
        if(trees[i].x != trees[i - 1].x) nrcnt = 0;
        nccnt[trees[i].y]++; nrcnt++;
        // 更新所在列的值
        unsigned int up = nccnt[trees[i].y], down = ccnt[trees[i].y] - nccnt[trees[i].y],
            now = C[up][k] * C[down][k], old = query(trees[i].y) - query(trees[i].y - 1);
        if(now != old) add(trees[i].y, now - old);
        unsigned int left = nrcnt, right = rcnt[trees[i].x] - nrcnt;
        if(trees[i].x != trees[i + 1].x || trees[i + 1].y - trees[i].y <= 1
            || left < k || right < k) continue;
        // 计算两棵常青树之间区间对答案的贡献
        ans += C[left][k] * C[right][k] * (query(trees[i + 1].y - 1) - query(trees[i].y));
    }
    printf("%u", ans % 2147483648u); // 自然溢出是对2^32取模,这里再取一次模
    return 0;
}
[JLOI2013]卡牌游戏 题解

[JLOI2013]卡牌游戏 题解

题目地址:洛谷:【P2059】[JLOI2013]卡牌游戏 – 洛谷、BZOJ 

[SCOI2010]生成字符串 题解

[SCOI2010]生成字符串 题解

题目地址:洛谷:【P1641】[SCOI2010]生成字符串 – 洛谷、BZO 

[洛谷1357]花园 题解

[洛谷1357]花园 题解

题目地址:洛谷:【P1357】花园 – 洛谷

题目描述

小L有一座环形花园,沿花园的顺时针方向,他把各个花圃编号为1~N(2<=N<=10^15)。他的环形花园每天都会换一个新花样,但他的花园都不外乎一个规则,任意相邻M(2<=M<=5,M<=N)个花圃中有不超过K(1<=K<M)个C形的花圃,其余花圃均为P形的花圃。
例如,N=10,M=5,K=3。则
CCPCPPPPCC 是一种不符合规则的花圃;
CCPPPPCPCP 是一种符合规则的花圃。
请帮小L求出符合规则的花园种数Mod 1000000007

由于请您编写一个程序解决此题。

输入输出格式

输入格式:
一行,三个数N,M,K。

输出格式:
花园种数Mod 1000000007

输入输出样例

输入样例#1:

10 5 3

输出样例#1:

458

输入样例#2:

6 2 1

输出样例#2:

18

说明

【数据规模】
40%的数据中,N<=20;
60%的数据中,M=2;
80%的数据中,N<=10^5。
100%的数据中,N<=10^15。

题解

我们看一下M=2怎么做。我们枚举后面2个花圃的状态,一共有4种:CC、CP、PC、PP,设计状态dp[i][S]表示到第i个且末尾2个花圃的状态为S的方案数,然后枚举后面接上去哪个花圃来转移,如CC可以转移到CC和CP。如果M更大,到5了,就可以考虑状压这个状态来转移。转移同上,只是要把操作换为位运算。这样直接做的复杂度是[eq]O(n \cdot 2^{m})[/eq]的。
当n变得无法承受的时候,我们需要一个[eq]O(\log n)[/eq]的算法。这个时候我们想到了快速幂。我们知道,从一个状态向下一个状态的转移是一个固定不变的线性变换,既然如此,我们构建转移矩阵,进行矩阵快速幂计算转移矩阵的n次方即可。复杂度是[eq]O(\log n)[/eq]的(忽略矩阵乘法带来的开销)。
对于构造转移矩阵的方法,实际上矩阵的第i行表示下一层的i这个元素由这一层的元素乘上怎么样的系数加和得来的,因此对于i \rightarrow i'这个转移,我们直接令trans_{i', i} = 1即可。由于初始的时候是一个单位矩阵,也可以不乘这个初始矩阵。
为什么不是n-m次幂?注意我们DP状态的定义,答案在dp[n][S]中,至于当i小于m的时候,实际上S多出来那部分的意义是在枚举这个序列末尾的情况,因为花园是个环嘛。答案计算的是[eq]\sum_S dp[n][S] = trans^n_{S, S}[/eq],是因为初始的时候是个列向量,是一排1,扩展成方阵以后就是单位矩阵了,最后肯定要按照列向量的意义计算,就取主对角线上的数值。

代码

// Code by KSkun, 2018/4
#include <cstdio>
#include <cstring>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    register char c = fgc();
    while(c < '0' || c > '9') {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = fgc();
    }
    return res * neg;
}

const int MAXN = 50, MO = 1e9 + 7;

LL n, m, k, cnt[MAXN];

struct Matrix {
    LL a[MAXN][MAXN];
    Matrix() {
        memset(a, 0, sizeof(a));
    }
    inline Matrix operator*(const Matrix &rhs) const {
        Matrix res;
        for(int i = 0; i < 1 << m; i++) {
            for(int j = 0; j < 1 << m; j++) {
                for(int k = 0; k < 1 << m; k++) {
                    res.a[i][j] = (res.a[i][j] + a[i][k] * rhs.a[k][j] % MO) % MO;
                }
            }
        }
        return res;
    }
    inline Matrix& operator*=(const Matrix &x) {
        return *this = *this * x;
    }
};

inline Matrix fpow(Matrix mat, LL k) {
    Matrix res;
    for(int i = 0; i < 1 << m; i++) {
        res.a[i][i] = 1;
    }
    while(k) {
        if(k & 1) res *= mat;
        mat *= mat;
        k >>= 1;
    }
    return res;
}

int main() {
    n = readint(); m = readint(); k = readint();
    for(int i = 0; i < 1 << m; i++) {
        cnt[i] = cnt[i >> 1] + (i & 1);
    }
    Matrix trans;
    for(int i = 0; i < 1 << m; i++) {
        if(cnt[i] > k) continue;
        trans.a[i >> 1][i] = 1;
        trans.a[i >> 1 | (1 << (m - 1))][i] = 1;
    }
    trans = fpow(trans, n);
    LL res = 0;
    for(int i = 0; i < 1 << m; i++) {
        res = (res + trans.a[i][i]) % MO;
    }
    printf("%lld", res);
    return 0;
}
[SDOI2010]地精部落 题解

[SDOI2010]地精部落 题解

题目地址:洛谷:【P2467】[SDOI2010]地精部落 – 洛谷、BZOJ 

[SCOI2005]最大子矩阵 题解

[SCOI2005]最大子矩阵 题解

题目地址:洛谷:【P2331】[SCOI2005]最大子矩阵 – 洛谷、BZO 

Codeforces Round #476 (Div. 2) [Thanks, Telegram!] 赛后总结

Codeforces Round #476 (Div. 2) [Thanks, Telegram!] 赛后总结

965A Paper Airplanes

题意简述

一张纸可以做s个纸飞机,现在有k个人,每个人要做n个纸飞机,而一包纸有p张,他们想买若干包然后把纸分给每个人让他们做纸飞机,求他们应该买多少包纸才能满足条件。

思路

没什么难度,按照题目的意思求即可。答案是
[eq display=”1″] \lceil \frac{\lceil \frac{n}{s} \rceil k}{p} \rceil [/eq]
打CFR的时候看一眼1A的那种题。

代码

// Code by KSkun, 2018/4
#include <cstdio>
#include <cmath>

int k, n, s, p;

int main() {
    scanf("%d%d%d%d", &k, &n, &s, &p);
    printf("%d", int(ceil(ceil(double(n) / s) * k / p)));
    return 0;
}

965B Battleship

题意简述

给你一个n*n的地图,有的格子可能属于一条船,有的不属于。一条船的长度为k,体现在地图上就是横向或者纵向连续的k个是船的格子。求可能属于的船的数量最多的格子。
数据范围:n 100

思路

枚举每个格子,然后暴力找到它可能属于的船的数量。具体来说,就是上下/左右找一通连通块,注意延伸出去不能超过k-1格不然不能把这个格子包含进去。然后答案就是这个连通块的大小减去k-1。
写了一会,由于当时CFR就写的很丑emm

代码

// Code by KSkun, 2018/4
#include <cstdio>
#include <cmath>

#include <algorithm>

int n, k;
char mmp[105][105];
int ax, ay, ans = -1;

inline int count(int x, int y) {
    if(mmp[x][y] == '#') return 0;
    int cnt = 0, tcnt = 1, bcnt = 0;
    for(int i = x - 1; i >= 1; i--) {
        if(mmp[i][y] == '#') break;
        tcnt++;
    }
    if(tcnt > k) tcnt = k; cnt += tcnt; tcnt = 1;
    for(int i = x + 1; i <= n; i++) {
        if(mmp[i][y] == '#') break;
        tcnt++;
    }
    if(tcnt > k) tcnt = k; cnt += tcnt - 1; tcnt = 1;
    bcnt += std::max(cnt - (k - 1), 0); cnt = 0;
    for(int i = y - 1; i >= 1; i--) {
        if(mmp[x][i] == '#') break;
        tcnt++;
    }
    if(tcnt > k) tcnt = k; cnt += tcnt; tcnt = 1;
    for(int i = y + 1; i <= n; i++) {
        if(mmp[x][i] == '#') break;
        tcnt++;
    }
    if(tcnt > k) tcnt = k; cnt += tcnt - 1; tcnt = 1;
    bcnt += std::max(cnt - (k - 1), 0); cnt = 0;
    return bcnt;
}

int main() {
    scanf("%d%d%d%d", &n, &k);
    for(int i = 1; i <= n; i++) {
        scanf("%s", mmp[i] + 1);
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            int cnt = count(i, j);
            if(cnt > ans) {
                ans = cnt; ax = i; ay = j;
            }
        }
    }
    printf("%d %d", ax, ay);
    return 0;
}

965C Greedy Arkady

题意简述

k个人想分n块糖。分糖的规则是这样的,先确定每个人每次分到的糖x,先给第一个人x个,然后第二个,一轮给完以后再轮到第一个人,直到剩下的糖数不足x,剩下的糖会直接扔掉。但是有限制每个人最多不能分到糖D次,且x也不能大于M。求第一个人分到糖的最大数目。
数据范围:n: 1e18,M: ≤n,D: 1e3

思路

我们看到D的范围很小,因此我们可以枚举每个人分到的糖的最多次数i。我们可以根据i计算出一个x的范围,而实际上我们肯定想要最大的x,因为第一个人分到糖的数量实际上是xi。这个x很好求:
x = \lfloor \frac{n}{k(i-1)+1} \rfloor
求出x以后看一下超没超M,超了就直接设成M检查x是否能满足i的要求。然后对于x直接计算答案即可。对于一个确定的x,它的答案是xi。
CFR的时候,一没看见剩下的糖扔掉,二没看见D的范围小,而且对数据范围并不敏感,直接导致没想到枚举D。想到了以后应该是很好做的吧。
当时打了个表,发现如果不管剩下的糖扔掉这条,答案在n的两个因数之间是单峰的,写了个找因数+三分找峰,复杂度O(\sqrt{n} + \log^2 n),直接TLE。

代码

// Code by KSkun, 2018/4
#include <cstdio>
#include <cmath>

#include <algorithm>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    register char c = fgc();
    while(c < '0' || c > '9') {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = fgc();
    }
    return res * neg;
}

LL n, k, M, D;

int main() {
    n = readint(); k = readint(); M = readint(); D = readint();
    LL ans = 0;
    for(LL i = 1; i <= D; i++) {
        LL lg = log(i - 1) / log(10) + log(k) / log(10); // 会爆LL,log处理一下
        if(lg > log(n) / log(10)) continue; 
        LL x = std::min(M, n / ((i - 1) * k + 1));
        if(!x) continue;
        LL ri = ceil(n / x / double(k));
        if(ri != i) continue;
        ans = std::max(ans, x * i);
    }
    printf("%I64d", (long long) ans);
    return 0;
}

965D Single-use Stones

题意简述

河宽w,一只青蛙最多能跳l这么远。给你河中每个位置的石头数,每个石头只能用一次,用完了就沉入水中了,求最多能让多少青蛙过河。
数据范围:w, l: 1e5

思路

最开始想了个网络流的模型,拆点限流石头个数,然后从i向[i+1, i+l]中的每个石头连出边,但是这样的边数是O(n^2)的空间开不下。
其实我们来想一下,S的出边到[1, l],而[1, l]的出边到[2, l+1],依次类推,我们可以把这样一个区间内的点合起来看做最大流的限制啊!也就是说,\min_{i=l}^{w-1} \{sum_i - sum_{i-l}\}就是答案。
其实CF官解的解释是这样的:二分答案k,对于确定的k,首先当从近到远第i块石头到第i+k块的距离大于l的时候,k只青蛙是过不去的。因为最左边那只就跳不过去了,所以就可以O(n)验证确定的k。简化这个过程也能得到上面的解法。

代码

// Code by KSkun, 2018/4
#include <cstdio>

#include <algorithm>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    register char c = fgc();
    while(c < '0' || c > '9') {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = fgc();
    }
    return res * neg;
}

int w, l, a[100005];

int main() {
    w = readint(); l = readint();
    for(int i = 1; i < w; i++) {
        a[i] = readint() + a[i - 1];
    }
    int ans = 1e9;
    for(int i = l; i < w; i++) {
        ans = std::min(ans, a[i] - a[i - l]);
    }
    printf("%d", ans);
    return 0;
}

965E Short Code

题意简述

把n个字符串用它的一个前缀不重复地代表,求最小的代表串长度和。
数据范围:字符串总长: 1e5

思路

如果放在Trie树上看这个问题,就是把一些word标记给提到这个节点的某个父亲的位置,且每个节点只能有一个word标记。考虑一个节点的子树内的word标记全都提前过,现在这个节点上本来就有word标记,那么这个节点的子树的提前也已经完成了,而如果没有word标记,就要从子树中选择一个提前,我们显然应该选择子树中深度较大的那个。逐子树递归完成这个操作,每个子树维护一个可并堆,最后把堆里的元素加个和就完事了。
复杂度O(n \log n)

代码

用了下pb_ds的可并堆,其实好像普通堆O(n \log n)合并也没事?

// Code by KSkun, 2018/4
#include <cstdio>

#include <ext/pb_ds/priority_queue.hpp>

typedef __gnu_pbds::priority_queue<int, std::less<int>, 
    __gnu_pbds::pairing_heap_tag> priority_queue;

const int MAXN = 100005;

int ch[MAXN][26], tot = 1;
bool wrd[MAXN];

inline void insert(char *s) {
    int p = 1;
    for(int i = 0; s[i]; i++) {
        int c = s[i] - 'a';
        if(!ch[p][c]) ch[p][c] = ++tot;
        p = ch[p][c]; 
    } 
    wrd[p] = true;
}

inline void dfs(int u, priority_queue &q, int dep) {
    for(int c = 0; c < 26; c++) {
        if(ch[u][c]) {
            priority_queue p;
            dfs(ch[u][c], p, dep + 1);
            q.join(p);
        }
    }
    if(!wrd[u]) q.pop();
    q.push(dep);
}

int n;
char s[MAXN];

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
        scanf("%s", s);
        insert(s);
    }
    int ans = 0;
    for(int c = 0; c < 26; c++) {
        if(ch[1][c]) {
            priority_queue q;
            dfs(ch[1][c], q, 1);
            while(!q.empty()) {
                ans += q.top();
                q.pop();
            }
        }
    }
    printf("%d", ans);
    return 0;
}
[SDOI2009]学校食堂 题解

[SDOI2009]学校食堂 题解

题目地址:洛谷:【P2157】[SDOI2009]学校食堂 – 洛谷、BZOJ