其他分享
首页 > 其他分享> > 20200718模拟赛4题解

20200718模拟赛4题解

作者:互联网

A. 渔民的烦恼

题目描述

输入格式

输出格式

样例输入

4
20 300
40 400
340 700
360 600

样例输出

415

Solve

Code

#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#define int long long
using namespace std;
const int N = 1e5+5;
int n, a[N], w[N], c[N], l = 1<<30, r;
ll s;
bool judge(int x) {
    memset(c, 0, sizeof(c));
    for (int i = 1; i < n; ++i) {
        if (w[i] + c[i] < x) c[i+1] -= x - (w[i] + c[i]) + a[i+1] - a[i];
        else if (w[i] + c[i] > x && w[i] + c[i] - x > a[i+1] - a[i]) c[i+1] += w[i] + c[i] - x - a[i+1] + a[i];
    }
    return w[n] + c[n] >= x;
}
signed main() {
    scanf("%lld", &n);
    for (int i = 1; i <= n; ++i)
        scanf("%lld%lld", &a[i], &w[i]), 
        l = min(l, w[i]), s += w[i];
    r = s / n + 1;
    while (l < r) {
        int mid = l+r+1 >> 1;
        if (judge(mid)) l = mid;
        else r = mid - 1;
    }
    printf("%lld\n", l);
    return 0;
}

B. 乘车路线

题目描述

输入格式

输出格式

样例输入

5 6 7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2

样例输出

11

Solve1

Code1

#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 105, M = 1005, MN = 0x3f3f3f3f;
struct Side {
    int t, d, w, next;
}e[10005];
int head[N], tot;
void Add(int x, int y, int z, int w) {
    e[++tot] = (Side) {y, z, w, head[x]};
    head[x] = tot;
}
int W, n, m, d[N][M], ans = MN;
bool v[N][M];
queue< pair<int, int> > q;
void Spfa(int u) {//基本上算是板子
    memset(d, 0x3f, sizeof(d));
    d[u][0] = 0;
    q.push(make_pair(u, 0));
    while (!q.empty()) {
        int x = q.front().first, s = q.front().second;
        q.pop(); v[x][s] = 0;
        for (int i = head[x]; i; i = e[i].next) {
            int y = e[i].t, w = s + e[i].w;
            if (w > W) continue;
            if (d[y][w] > d[x][s] + e[i].d) {
                d[y][w] = d[x][s] + e[i].d;
                if (v[y][w]) continue;
                v[y][w] = 1;
                q.push(make_pair(y, w));
            }
        }
    }
}
int main() {
    scanf("%d%d%d", &W, &n, &m);
    while (m--) {
        int x, y, z, w;
        scanf("%d%d%d%d", &x, &y, &z, &w);
        Add(x, y, z, w);
    }
    Spfa(1);
    for (int i = 0; i <= W; ++i)
        ans = min(ans, d[n][i]);
    if (ans == MN) puts("NO");
    else printf("%d\n", ans);
    return 0;
}

Solve2

Code2

#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 105;
struct Side {
    int t, d, w, next;
}e[10005];
int head[N], tot;
void Add(int x, int y, int z, int w) {
    e[++tot] = (Side) {y, z, w, head[x]};
    head[x] = tot;
}
int n, m, ans = 1<<30, W;
bool v[N];
void Dfs(int x, int s, int d) {
    if (s > W || d >= ans) return;
    if (x == n) {
        ans = min(ans, d);
        return;
    }
    for (int i = head[x]; i; i = e[i].next) {
        int y = e[i].t;
        if (v[y]) continue;
        v[y] = 1;
        Dfs(y, s + e[i].w, d + e[i].d);
        v[y] = 0;
    }
}
int main() {
    scanf("%d%d%d", &W, &n, &m);
    while (m--) {
        int x, y, z, w;
        scanf("%d%d%d%d", &x, &y, &z, &w);
        Add(x, y, z, w);
    }
    v[1] = 1; 
    Dfs(1, 0, 0);
    if (ans == 1<<30) puts("NO");
    else printf("%d\n", ans);
    return 0;
}

Cloakroom

题目描述

输入格式

输出格式

样例输入

5
6 2 7
5 4 9
1 2 4
2 5 8
1 3 9
5
2 7 1
2 7 2
3 2 0
5 7 2
4 1 5

样例输出

TAK
NIE
TAK
TAK
NIE

Solve

Code

#include <cstdio>
#include <algorithm>
using namespace std;
struct Node1 {
    int a, b, c;
    bool operator < (const Node1 &b) const {
        return a < b.a;
    }//重载运算符,对物品按a值从小到大排序
}a[1005];
struct Node2 {
    int m, k, s, id;
    bool operator < (const Node2 &b) const {
        return m < b.m;
    }/重载运算符,对询问按m值从小到大排序
}b[1000005];
int n, q, f[100005];//f数组题解中的加粗部分进行了详细的解释
bool ans[1000005];
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
        scanf("%d%d%d", &a[i].c, &a[i].a, &a[i].b);
    scanf("%d", &q);
    for (int i = 1; i <= q; ++i)
        scanf("%d%d%d", &b[i].m, &b[i].k, &b[i].s), b[i].id = i;
       //记录编号id,离线排序后便于保存答案
    sort(a + 1, a + n + 1);//对物品按a值从小到大排序
    sort(b + 1, b + q + 1);//对询问按m值从小到大排序
    f[0] = 1 << 30;//初始化f[0]为极大值,防止在运行 min(f[k-a[j].c], a[j].b)时出现结果都是0的情况
    for (int i = 1, j = 1; i <= q; ++i) {
        for (; j <= n && a[j].a <= b[i].m; ++j)//满足条件1:a<=m
            for (int k = 100000; k >= a[j].c; --k)
                f[k] = max(f[k], min(f[k-a[j].c], a[j].b));
        if (f[b[i].k] > b[i].m + b[i].s) ans[b[i].id] = 1;
        //满足条件3:c之和==k
        //满足条件2:b>m+s
    }
    for (int i = 1; i <= q; ++i)
        puts(ans[i] ? "TAK" : "NIE");
    //三目运算符,个人比较喜欢使用,挺方便
    return 0;
}

D. 星际网络

题目描述

输入格式

输出格式

样例输入

2
4 0 1 2 3 4
2 0 3 1

样例输出

6

数据范围与提示

Solve

Code

#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
const int N = 30, M = 55440;//1~24中除去13,17,19,23的数的最小公倍数
int n, l[N];
ll a[N][N], ans;
void Add(int x) {
    ll m = 0;
    for (int i = 0; i < x; ++i)
        m = max(m, a[x][i]);
    ans += m;
}
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        int d, s;
        scanf("%d%d", &d, &s);
        for (int i = 0; i < d; ++i)
            scanf("%d", &l[i]);
        for (int i = 0; i < d; ++i)
            a[d][i] += l[(i+s)%d];    
    }
    for (int j = 0; j < M; ++j) {
        ll s = 0;
        for (int i = 1; i <= 24; ++i)
            if (i != 13 && i != 17 && i != 19 && i != 23)
                s += a[i][j%i];
        ans = max(ans, s);
    }
    Add(13); Add(17); Add(19); Add(23);
    printf("%lld\n", ans);
    return 0;
}

标签:int,题解,d%,20200718,城镇,ans,include,星球,模拟
来源: https://www.cnblogs.com/Z8875/p/13337291.html