其他分享
首页 > 其他分享> > PAT甲级2020秋季

PAT甲级2020秋季

作者:互联网

PAT甲级2020秋季

//01  
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <cmath>
#include <cstring>
using namespace std;

int n, m, k;
int lw[10009];
int milkl[10009];
int milkr[10009];
int minn = 200;

int main() {
    cin >> n;
    for(int i = 0 ; i < n; i++)
        scanf("%d", &lw[i]);
    int pre = minn;
    milkl[0] = milkr[n-1] = minn;
    for(int i = 1 ; i < n; i++){
        if(lw[i] > lw[i-1]){
            milkl[i] = pre+100;
        }else if(lw[i] == lw[i-1]){
            milkl[i] = pre;
        }else{
            milkl[i] = minn;
        }
        pre = milkl[i];
    }
    for(int i = n-2 ; i >= 0; i--){
        if(lw[i] > lw[i+1]){
            milkr[i] = pre+100;
        }else if(lw[i] == lw[i+1]){
            milkr[i] = pre;
        }else{
            milkr[i] = minn;
        }
        pre = milkr[i];
    }
    int sum = 0;
    for(int i = 0 ; i < n; i++){
        sum += max(milkl[i], milkr[i]);
    }
    printf("%d", sum);
}

// 02 21->25
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <cmath>
#include <cstring>
using namespace std;

int n, m, k, cnt = 0;
int landcost[10009];

int main() {
    scanf("%d %d", &n, &m);
    for(int i = 0 ; i < n; i++)
        scanf("%d", &landcost[i]);

    for(int i = 0; i < n; i++){
        int sum = 0;
        for(int j = 0; j < n && i+j < n; j++){
            sum += landcost[i+j];
            if(sum <= m) cnt++;
            else break;
        }
    }
    printf("%d\n", cnt);
    return 0;
}

// 03 21
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <cmath>
#include <map>
#include <cstring>
using namespace std;

int n, m, k, cnt = 0;
int in[30], pre[30], pos[30];
vector<vector<int>> ans;

void level(int h1, int h2, int l, int lev){
    if(l == 0) return; 
    int idx = pos[pre[h1]];
    ans[lev].emplace_back(in[idx]);
    int l1 = idx - h2;
    int l2 = l - l1 - 1;
    if(l1 != 0)
        level(h1+1, h2, l1, lev+1);
    if(l2 != 0)
        level(h1+l1+1, idx+1, l2, lev+1);
}

int main() {
    cin >> n;
    for(int i = 0 ; i < n; i++){
        scanf("%d", &in[i]);
        pos[in[i]] = i;
    }
    for(int i = 0 ; i < n; i++)
        scanf("%d", &pre[i]);
    ans.resize(n+1);

    level(0, 0, n, 0);
    int l = 0;
    while(!ans[l].empty()){
        if(l != 0) printf(" ");
        cout << ans[l][0];
        l++;
    }
    return 0;
}

// 04 SPFA算法!!!
#include <bits/stdc++.h>
using namespace std;

const int maxv = 1010;
const int INF = 0x7fffffff;

int n, m, k;
int in[maxv] = {0}, indegree[maxv] = {0};
int w[maxv], dis[maxv], seq[maxv], pre[maxv];
int G[maxv][maxv], money[maxv][maxv];
vector<int> post[maxv];   //记录后继结点

void SPFA(){
    queue<int> q;
    fill(pre, pre+maxv, -1);
    fill(dis, dis+maxv, INF);
    fill(w, w+maxv, 0);
    for(int i = 0; i < n; i++){
        if(!indegree[i]){//源点初始化并入队
            dis[i] = 0;
            w[i] = 0;
            q.push(i);
        }
    }
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int j = 0; j < post[u].size(); j++){
            int v = post[u][j];
            indegree[v]--;
            //若弧尾入度松弛操作后为0 加入队列作为新的源点
            if(!indegree[v])   q.push(v);

            //主导权值
            if(dis[u] + G[u][v] < dis[v]){
                dis[v] = dis[u] + G[u][v];
                w[v] = w[u] + money[u][v];    //此处对权值的更新不要忘
                pre[v] = u;
            }else if(dis[u] + G[u][v] == dis[v]){
                //副权值判断
                if(w[u] + money[u][v] > w[v]){
                    w[v] = w[u] + money[u][v];    //注意不要忘了这句话,即更新最大权值(幸福值)
                    pre[v] = u;
                }
            }
        }
    }
}

int main(){
    int T1, T2, S, D;
    bool flag = true;
    scanf("%d %d", &n, &m);
    fill(G[0], G[0]+maxv*maxv, INF);
    for(int i = 0; i < m; i++){
        scanf("%d %d %d %d", &T1, &T2, &S, &D);
        indegree[T2]++;
        in[T2]++;
        G[T1][T2] = S;
        money[T1][T2] = D;
        post[T1].push_back(T2);
    }
    scanf("%d", &k);
    for(int i = 0; i < k; i++)  scanf("%d", &seq[i]);
    SPFA();
    //最短路径遍历寻一遍后还剩入度不为0的即为成环的点排除
    for(int i = 0; i < k; i++){
        if(indegree[seq[i]]){
            flag = false;
            break;
        }
    }
    if(flag){
        printf("Okay.\n");
        for(int i = 0; i < k; i++){
            vector<int> ans;
            for(int pos = seq[i]; pos != -1; pos = pre[pos]){
                ans.push_back(pos);
            }
            reverse(ans.begin(), ans.end());
            //原始源点直接输出
            if(!in[seq[i]]) printf("You may take test %d directly.\n", seq[i]);
            else    for(int j = 0; j < ans.size(); j++)     printf("%d%s", ans[j], j == ans.size()-1 ? "\n":"->");
        }
    }else{
        printf("Impossible.\n");
        for(int i = 0; i < k; i++){
            if(!in[seq[i]]) printf("You may take test %d directly.\n", seq[i]);
            else    printf("Error.\n");
        }
    }
    return 0;
}#

标签:pre,PAT,int,maxv,++,甲级,2020,ans,include
来源: https://blog.csdn.net/qq_42871911/article/details/121609037