PAT(甲级)2020年秋季考试

难度排序:2<3<4<1

7-1

Panda and PP Milk

(20分)

PP milk (盆盆奶)is Pandas' favorite. They would line up to enjoy it as show in the picture. On the other hand, they could drink in peace only if they believe that the amount of PP milk is fairly distributed, that is, fatter panda can have more milk, and the ones with equal weight may have the same amount. Since they are lined up, each panda can only compare with its neighbor(s), and if it thinks this is unfair, the panda would fight with its neighbor.

Given that the minimum amount of milk a panda must drink is 200 ml. It is only when another bowl of milk is at least 100 ml more than its own that a panda can sense the difference.

Now given the weights of a line of pandas, your job is to help the breeder(饲养员)to decide the minimum total amount of milk that he/she must prepare, provided that the pandas are lined up in the given order.

Input Specification:

Each input file contains one test case. For each case, first a positive integer n (≤104) is given as the number of pandas. Then in the next line, n positive integers are given as the weights (in kg) of the pandas, each no more than 200. the numbers are separated by spaces.

Output Specification:

For each test case, print in a line the minimum total amount of milk that the breeder must prepare, to make sure that all the pandas can drink in peace.

Sample Input:

10
180 160 100 150 145 142 138 138 138 140

Sample Output:

3000

Hint:

The distribution of milk is the following:

400 300 200 500 400 300 200 200 200 300

这题没做全对,思路错了,以下为参考了网上的思路,应该是AC代码

#include <algorithm>
#include <iostream>

using namespace std;

const int N = 10010;
const int INF = 1e9;
int l_milk[N], r_milk[N], w[N];
int n;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> w[i];
  //设立边界条件
  w[0] = INF, w[n + 1] = INF;
  l_milk[0] = r_milk[n + 1] = 200;

  //从左往右进行扫描
  for (int i = 1; i <= n; i++) {
    if (w[i] > w[i - 1])
      l_milk[i] = l_milk[i - 1] + 100;
    else if (w[i] == w[i - 1])
      l_milk[i] = l_milk[i - 1];
    else
      l_milk[i] = 200;
  }
  //从右往左进行扫描
  for (int j = n; j >= 1; j--) {
    if (w[j] > w[j + 1])
      r_milk[j] = r_milk[j + 1] + 100;
    else if (w[j] == w[j - 1])
      r_milk[j] = r_milk[j - 1];
    else
      r_milk[j] = 200;
  }

  int sum = 0;
  for (int i = 1; i <= n; i++)
    sum += max(l_milk[i], r_milk[i]);

  cout << sum << endl;
  return 0;
}

2、3两题比较简单,省略。

7-4

Professional Ability Test

(30分)

Professional Ability Test (PAT) consists of several series of subject tests. Each test is divided into several levels. Level A is a prerequisite (前置要求) of Level B if one must pass Level A with a score no less than S in order to be qualified to take Level B. At the mean time, one who passes Level A with a score no less than S will receive a voucher(代金券)of D yuans (Chinese dollar) for taking Level B.

At the moment, this PAT is only in design and hence people would make up different plans. A plan is NOT consistent if there exists some test T so that T is a prerequisite of itself. Your job is to test each plan and tell if it is a consistent one, and at the mean time, find the easiest way (with minimum total S) to obtain the certificate of any subject test. If the easiest way is not unique, find the one that one can win the maximum total value of vouchers.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (≤1000) and M, being the total numbers of tests and prerequisite relations, respectively. Then M lines follow, each describes a prerequisite relation in the following format:

T1 T2 S D

where T1 and T2 are the indices (from 0 to N−1) of the two distinct tests; S is the minimum score (in the range (0, 100]) required to pass T1 in order to be qualified to take T2; and D is the value of the voucher (in the range (0, 500]) one can receive if one passes T1 with a score no less than S and plan to take T2. It is guaranteed that at most one pair of S and D are defined for a prerequisite relation.

Then another positive integer K (≤N) is given, followed by K queries of tests. All the numbers in a line are separated by spaces.

Output Specification:

Print in the first line Okay. if the whole plan is consistent, or Impossible. if not.

If the plan is consistent, for each query of test T, print in a line the easiest way to obtain the certificate of this test, in the format:

T0->T1->...->T

However, if T is the first level of some subject test (with no prerequisite), print You may take test T directly. instead.

If the plan is impossible, for each query of test T, check if one can take it directly or not. If the answer is yes, print in a line You may take test T directly.; or print Error. instead.

Sample Input 1:

8 15
0 1 50 50
1 2 20 20
3 4 90 90
3 7 90 80
4 5 20 20
7 5 10 10
5 6 10 10
0 4 80 60
3 1 50 45
1 4 30 20
1 5 50 20
2 4 10 10
7 2 10 30
2 5 30 20
2 6 40 60
8
0 1 2 3 4 5 6 7

Sample Output 1:

Okay.
You may take test 0 directly.
0->1
0->1->2
You may take test 3 directly.
0->1->2->4
0->1->2->4->5
0->1->2->6
3->7

Sample Input 2:

4 5
0 1 1 10
1 2 2 10
3 0 4 10
3 2 5 10
2 0 3 10
2
3 1

Sample Output 2:

Impossible.
You may take test 3 directly.
Error.

 先拓扑排序判断是否存在路径,感觉题目用意挺明显的,如果拓扑排序可以成立的话只有单路径(不用考虑复杂情况,题目中说明了只有单路径,那在成立的路径中肯定不会有多个指向一个,这样解不唯一,可以排除情况),接下来大胆用dijkstra进行多关键字排序。对于边的存储我用了数组模拟邻接表,因为vertex比较少用朴素的dijkstra即可。

#include <bits/stdc++.h>

using namespace std;
const int N = 1010, M = 1000010;
bool st[N];
int dist[N], pre[N], vou[N];
int nodes[N], d[N];
bool topo[N];
int h[N], e[M], ne[M], w[M], v[M], idx;
vector<int> head;
int n, m;
int q[N];
vector<int> pr[N];

void add(int a, int b, int c, int d) {
  e[idx] = b, ne[idx] = h[a], w[idx] = c, v[idx] = d, h[a] = idx++;
}

void dij() {
  memset(st, 0, sizeof st);
  memset(dist, 0x3f, sizeof dist);
  memset(pre, -1, sizeof pre);
  for (int i = 0; i < head.size(); i++) {
    dist[head[i]] = 0;
  }
  for (int i = 0; i <= n; i++) {
    int t = -1;
    for (int j = 0; j < n; j++) {
      if (!st[j] && (t == -1 || dist[t] > dist[j]))
        t = j;
    }
    if (t == -1)
      break;

    st[t] = true;
    for (int id = h[t]; ~id; id = ne[id]) {
      int u = e[id];
      if (st[u])
        continue;
      if (dist[u] > dist[t] + w[id]) {
        pre[u] = t;
        dist[u] = dist[t] + w[id];
        vou[u] = vou[t] + v[id];
      } else if (dist[u] == dist[t] + w[id] && vou[u] < vou[t] + v[id]) {
        vou[u] = vou[t] + v[id];
        pre[u] = t;
      }
    }
  }
}

void gettopo() {
  queue<int> q;
  for (int i = 0; i < head.size(); i++) {
    topo[head[i]] = true;
    q.push(head[i]);
  }
  while (q.size()) {
    int t = q.front();
    q.pop();
    for (int id = h[t]; ~id; id = ne[id]) {
      int u = e[id];
      if (topo[u])
        continue;
      d[u]--;
      if (d[u] == 0) {
        topo[u] = true;
        q.push(u);
      }
    }
  }
}

int main() {
  cin >> n >> m;
  memset(h, -1, sizeof h);
  while (m--) {
    int a, b, w, v;
    cin >> a >> b >> w >> v;
    add(a, b, w, v);
    nodes[b] = true;
    d[b]++;
  }

  for (int i = 0; i < n; i++) {
    if (!nodes[i])
      head.push_back(i);
  }
  dij();
  gettopo();
  int k;
  cin >> k;
  bool ok = true;

  for (int i = 0; i < k; i++) {
    cin >> q[i];
    if (!topo[q[i]])
      ok = false;
  }
  puts(ok ? "Okay." : "Impossible.");

  for (int i = 0; i < k; i++) {
    int x = q[i];
    if (!topo[x])
      puts("Error.");
    else if (dist[x] == 0)
      printf("You may take test %d directly.\n", x);
    else {
      vector<int> path;
      for (int u = x; ~u; u = pre[u]) {
        path.push_back(u);
      }
      for (int i = path.size() - 1; i > 0; i--)
        printf("%d->", path[i]);
      printf("%d\n", x);
    }
  }

  return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值