寒假刷题第四天

发布时间:2024年01月11日

PTA甲级

1017 Queueing at Bank

可以使用小顶堆模拟窗口的情况,一定是最小的时间的窗口先空出来

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>

using namespace std;

struct people
{
    int time;
    int process;
};
int n , k;
vector<people>v;
int close = 17 * 3600 + 1;
priority_queue<int , vector<int> , greater<int>>window;

bool cmp(people a,people b)
{
    return a.time < b.time;
}

int main()
{
    cin >> n >> k;
    for(int i = 0;i < n;i ++)
    {
        int h , m , s , p;
        scanf("%d:%d:%d %d" ,&h ,&m ,&s ,&p);
        int t = h * 3600 + m * 60 + s;
        if(t >= 61201) continue;
        v.push_back({t , p * 60}); // p是分钟
    }
    sort(v.begin() , v.end() , cmp);
    
    for(int i = 0;i < k;i ++) 
        window.push(28800);
    double res = 0;
    for(auto i : v)
    {
        int t = i.time , p = i.process;
        int w = window.top(); // 最早结束的窗口
        window.pop();
        if(w > t)
        {
            res += w - t;
            window.push(p + w);
        }
        else window.push(p + t);
    }
    //注意 注意 注意 有可能cnt为0
    if(v.size()) printf("%.1lf" , (res / 60.0 / v.size()));
    else puts("0.0");
}

1018 Public Bike Management

真的阴间,经典的dfs

#include<iostream>
#include<unordered_map>
#include<vector>
#include<cstring>

using namespace std;

const int N = 510;

int c , n , s , m;
int bike[N];
int g[N][N];
int dist[N];

vector<int>path;
vector<int>res; // 存最终的路径
int send , last , all_dist = 0x3f3f3f3f;

void dfs(int u , int cursend , int curlast , int curdist)
{
    if(curdist > dist[u]) return ;
    path.push_back(u);
    if(u == s) // 走到终点
    {
        if(all_dist > curdist || all_dist == curdist && send > cursend) // 之前的路径长
        {
            res = path;
            all_dist = curdist;
            send = cursend;
            last = curlast;
        }
        // 阴间题目怎么不说清楚呢
        if(all_dist == curdist && send == cursend && last > curlast)
        {
            res = path;
            all_dist = curdist;
            send = cursend;
            last = curlast;
        }
    }
    else
    {
        if(curdist < dist[u]) dist[u] = curdist;
        for(int i = 1;i <= n;i ++)
        {
            if(g[u][i])
            {
                if(c > bike[i] + curlast) dfs(i , c - bike[i] - curlast + cursend , 0 , curdist + g[u][i]); // 需要放入自行车从last中取
                else dfs(i , cursend , curlast + bike[i] - c , curdist + g[u][i]);
            }
        }
    }
    path.pop_back();
}

int main()
{
    memset(dist , 0x3f , sizeof dist);
    memset(g , 0x3f , sizeof g);
    scanf("%d %d %d %d" ,&c ,&n ,&s ,&m);
    for(int i = 1;i <= n;i ++) scanf("%d" ,&bike[i]);

    while(m --)
    {
        int a , b , d;
        scanf("%d %d %d" ,&a ,&b ,&d);
        g[a][b] = g[b][a] = d;
    }

    c /= 2;
    dfs(0 , 0 , 0 , 0);
    printf("%d " , send);
    for(int i = 0;i < res.size();i ++)
    {
        if(i) printf("->");
        printf("%d" , res[i]);
    }
    printf(" %d" , last);
}

1021 Deepest Root

依然是dfs,注意不能使用unordered_map会超时

#include<iostream>
#include<vector>
#include<set>
#include<cstring>

using namespace std;

const int N = 1e5 + 10;
int n;
vector<int>mp[N];
vector<int>res[N];
bool st[N];
int depth[N];
int p[N];

int find(int x)
{
    if(p[x] != x) p[x] = find(p[x]);
    return p[x];
}

void dfs(int u , int dep , int &h)
{
    h = max(h , dep);
    if(st[u]) return ;
    if(!mp[u].size()) return ;
    st[u] = true;
    for(auto i : mp[u])
        dfs(i , dep + 1 , h);
}

int main()
{
    scanf("%d" ,&n);
    int cnt = n;
    for(int i = 1;i <= n;i ++) p[i] = i;
    for(int i = 0;i < n - 1;i ++)
    {
        int a , b;
        scanf("%d %d" ,&a ,&b);
        int pa = find(a) , pb = find(b);
        if(pa != pb) p[pa] = pb , cnt --;
        mp[a].push_back(b) , mp[b].push_back(a);
    }
    if(cnt > 1) 
    {
        printf("Error: %d components", cnt);
        return 0;
    }
    int maxx = 1;
    for(int i = 1;i <= n;i ++)
    {
        memset(st , 0 , sizeof st);
        int h = 1;
        dfs(i , 1 , h);
        maxx = max(maxx , h);
        res[h].push_back(i);
    }
    for(auto i : res[maxx])
        printf("%d\n" , i);
    return 0;
}

文章来源:https://blog.csdn.net/xp_xht123/article/details/135532358
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。