解题报告 『[USACO07JAN]Tallest Cow(差分)』

原题地址

建立一个数组high,若一对关系指明Ai与Bi可以互相看见(Ai < Bi),则把数组high中下标为Ai + 1到Bi - 1的数都减去1,意为Ai到Bi之间的牛的身高至少比它们少1。

再加一个辅助数组sup可以将时间复杂度由O(NM)降到O(N + M)。

代码实现如下:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (register int i = a; i <= b; i++)

const int maxn = 1e4 + 5;

int n, p, h, r;
int sup[maxn], high[maxn];
bool vis[maxn][maxn];

int read() {
    int x = 0, flag = 0;
    char ch = ‘ ‘;
    while (ch != ‘-‘ && (ch < ‘0‘ || ch > ‘9‘)) ch = getchar();
    if (ch == ‘-‘) {
        flag = 1;
        ch = getchar();
    }
    while (ch >= ‘0‘ && ch <= ‘9‘) {
        x = (x << 1) + (x << 3) + ch - ‘0‘;
        ch = getchar();
    }
    return flag ? -x : x;
}

void write(int x) {
    if (x < 0) {
        putchar(‘-‘);
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + ‘0‘);
}

int main() {
    n = read(), p = read(), h = read(), r = read();
    rep(i, 1, r) {
        int u, v;
        u = read(), v = read();
        if (u > v) swap(u, v);
        if (vis[u][v]) continue;
        sup[u + 1]--;
        sup[v]++;
        vis[u][v] = 1;
    }
    rep (i, 1, n) {
        high[i] = high[i - 1] + sup[i];
        write(h + high[i]);
        printf("\n");
    }
    return 0;
}

值得注意的是,本题关系对是有可能重复给出的,因此我用了bool数组vis来判断,然而这是一个二维数组,很有可能爆空间,此题只是勉强卡过,更好的做法是使用map。

代码实现如下:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (register int i = a; i <= b; i++)

const int maxn = 1e4 + 5;

int n, p, h, m;
int sup[maxn], high[maxn];

map<pair<int, int>, bool> existed;

int read() {
    int x = 0, flag = 0;
    char ch = ‘ ‘;
    while (ch != ‘-‘ && (ch < ‘0‘ || ch > ‘9‘)) ch = getchar();
    if (ch == ‘-‘) {
        flag = 1;
        ch = getchar();
    }
    while (ch >= ‘0‘ && ch <= ‘9‘) {
        x = (x << 1) + (x << 3) + ch - ‘0‘;
        ch = getchar();
    }
    return flag ? -x : x;
}

void write(int x) {
    if (x < 0) {
        putchar(‘-‘);
        x = -x;
    }
    if (x > 9) write(x / 10);
    putchar(x % 10 + ‘0‘);
}

int main() {
    n = read(), p = read(), h = read(), m = read();
    rep(i, 1, m) {
        int u, v;
        u = read(), v = read();
        if (u > v) swap(u, v);
        if (existed[make_pair(u, v)]) continue;
        sup[u + 1]--;
        sup[v]++;
        existed[make_pair(u, v)] = 1;
    }
    rep (i, 1, n) {
        high[i] = high[i - 1] + sup[i];
        write(h + high[i]);
        printf("\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Kirisame-Marisa/p/10803325.html

时间: 2024-08-30 14:39:24

解题报告 『[USACO07JAN]Tallest Cow(差分)』的相关文章

解题报告 『Raid(分治)』

原题地址 大概翻译一下题意: 在与联邦的战争接连失败之后,帝国方面撤退到了最后的据点.凭借着强大的防御系统,帝国军击退了联邦的六波进攻.经过几天不眠不休的思考,Arthur,联邦统帅,注意到防御系统的唯一弱点是它的能源供应.该系统由n个核电站进行充电,任何一个核电站的故障都会导致系统失效. 将军很快就发动了一次突袭,突袭者是准备潜行进入要塞的N名特工.不幸的是,由于帝国空军的攻击,他们未能按预期的位置着陆.作为一名经验丰富的将军,Arthur很快意识到他需要重新安排计划.他现在想知道的第一件事是

解题报告 『统计单词数(字符串)』

原题地址 字符串入门题,因为完全不擅长所以还是WA了好几次才过. 还犯了一个⑨错误,果然做题时不应该听<チルノのパーフェクトさんすう教室>…… 代码如下: #include <bits/stdc++.h> using namespace std; void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } int m

解题报告 『酒店之王(网络最大流 + 拆点)』

原题地址 网络流板子题 + 拆点,个人觉得蓝题比较合适. 尽管我一开始只得了10分. 具体还是看代码吧. 代码实现如下: #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (register int i = a; i <= b; i++) const int inf = 0x3f3f3f3f, maxn = 1e4 + 5; int n, p, q, S, T, ans = 0, num_edge

解题报告 『占卜DIY(模拟)』

原题地址 水题,纯模拟. 代码实现如下: #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (register int i = a; i <= b; i++) #define per(i, a, b) for (register int i = a; i >= b; i--) const int maxn = 15; int ans = 0; int num[maxn], mat[maxn]

解题报告 『不要62(数位动规)』

原题地址 又学会了骂人的新词语. 代码实现如下: #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (register int i = (a); i <= (b); i++) const int maxn = 10; int l, r, ans, len; int num[maxn], dp[maxn][maxn]; void origin() {memset(dp, -1, sizeof(dp)

解题报告 『HISTOGRA - Largest Rectangle in a Histogram(单调栈)』

原题地址 单调栈板子题,代码很简单. 注意将a[n + 1]赋值为0,防止栈中矩形未弹完. 代码实现如下: #include <bits/stdc++.h> using namespace std; #define LL long long #define rep(i, a, b) for (register int i = a; i <= b; i++) const int maxn = 1e5 + 5; int n; int a[maxn], sta[maxn], wid[maxn]

解题报告 『矿洞:坍塌(ODT)』

原题地址 退役第二天也要坚持用珂朵莉树水题. 代码实现如下: #include <bits/stdc++.h> using namespace std; #define IT set<node>::iterator #define rep(i, a, b) for (register int i = (a); i <= (b); i++) const int maxn = 5e5 + 5; int n, m, cnt = 1; char str[maxn]; struct n

洛谷 P2879 [USACO07JAN]区间统计Tallest Cow 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:https://www.luogu.org/problem/show?pid=2879 题目描述 FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told on

BZOJ1635 [Usaco2007 Jan]Tallest Cow 最高的牛 数列差分

Description FJ's N (1 <= N <= 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 <= H <= 1,000,000) of the tallest cow along with