洛谷 P10711 Amusement Park 题解

前言

2024/9/30 upd:更新简化题意

模拟赛的 T3,因为没题解调了好久,现在自己写一个造福人民。

题目分析

简化题意

(这个简化题意为什么和原题一样长)

有一个空序列,有 q q q 次操作,共有以下三种操作:

  1. join 将一个新的正整数加入到序列尾,其编号为它被加入时的 join 操作的数量。每个数有一个属性 w w w。若 w w w 0 0 0,则该整数不可分;若 w w w 1 1 1,则该整数可分。

  2. leave 移除编号为 i i i 的整数。

  3. board 给定 b b b。从第一个整数 s 1 s_1 s1 开始依次考虑,直到考虑完整个序列或 b b b 为零。若 b ≥ s i b \ge s_i bsi,则 b ← b − s i b \gets b - s_i bbsi 并将 s i s_i si 移出序列;若 b < s i b < s_i b<si s i s_i si 可分,则 s i ← s i − b s_i \gets s_i - b sisib b ← 0 b \gets 0 b0;否则考虑下一个整数。

    考虑完后,输出所有被移除的整数和被修改的整数。

给出所有操作,求每次操作 3 3 3 的询问。

思路

考虑 s ≤ 10 s \le 10 s10 的情况:可以开 11 11 11 个桶子分别存可以分开的团队和不可以分开的大小为 1 1 1 到大小为 10 10 10 的团队。对于每个 join 操作,直接将其加入到对应的桶子里;对于每个 leave 操作,直接将离开的团队从其所对应的桶子中删掉;对于每个 board 操作,令车中剩下的空间为 b b b,重复在能分开的团队的桶子和大小为 1 1 1 b b b 的桶子中找排在最前面的团队并让它(或它的一部分)上车,直到车满或无人可上。

对于 1 ≤ s ≤ 200000 1 \le s \le 200000 1s200000 的情况下,上述做法如果暴力找排在最前面的团队的话必定超时,考虑对其优化。

发现这一部分所在做的事其实是在找每个桶子中最小数构成的数列的区间最小值(可以将其想象为每个桶子中的团队都按大小从小到大的顺序从上到下摆放,要从这 s + 1 s + 1 s+1 个桶子的最顶部的团队中找到一个最小的,它就是所有团队中排在最前面的那一个)。也就是说可以用线段树将总复杂度优化为 O ( n log ⁡ n ) O(n \log n) O(nlogn)

然后就是正解了。

参考代码

蒟蒻的码风有些烂,巨佬们请将就着看。

#include<bits/stdc++.h>
using namespace std;
#define LL long long    // 不开 long long 见祖宗
// 方便起见,重定义了线段树左子节点和右子节点的编号
#define leftson u << 1
#define rightson (u << 1) + 1
const LL NR = 2e5;
const LL inf = 1e18;     //无穷大,某个桶子中尚无团队时其在线段树上对应的节点的值就是 inf
// 线段树板子,不做过多解释
struct Node{
    LL l, r;
    LL Min, add;
};
struct Segtree{
    Node tr[4 * NR + 10];
    LL n;
    LL root;
    void pushup(LL u){
        tr[u].Min = min(tr[leftson].Min, tr[rightson].Min);
    }
    void pushdown(LL u){
        if(tr[u].add){
            tr[leftson].add += tr[u].add;
            tr[leftson].Min += tr[u].add;
            tr[rightson].add += tr[u].add;
            tr[rightson].Min += tr[u].add;
            tr[u].add = 0;
        }
    }
    void modify(LL u, LL l, LL r, LL d){
        if(l <= tr[u].l && r >= tr[u].r){
            tr[u].Min += (LL)d;
            tr[u].add += d;
            return;
        }
        pushdown(u);
        LL mid = (tr[u].l + tr[u].r) >> 1;
        if(l <= mid) modify(leftson, l, r, d);
        if(r > mid) modify(rightson, l, r, d);
        pushup(u);
    }
    LL query(LL u, LL l, LL r){
        if(l <= tr[u].l && r >= tr[u].r){
            return tr[u].Min;
        }
        pushdown(u);
        LL mid = (tr[u].l + tr[u].r) >> 1;
        pushup(u);
        LL ret = inf;
        if(l <= mid) ret = min(ret, query(leftson, l, r));
        if(r > mid) ret = min(ret, query(rightson, l, r));
        return ret;
    }
    void build(LL u, LL l, LL r){
        if(l == r){
            tr[u] = Node{l, r, inf, 0};
            return ;
        }
        tr[u].l = l;
        tr[u].r = r;
        LL mid = (l + r) >> 1;
        build(leftson, l, mid);
        build(rightson, mid + 1, r);
        pushup(u);
    }
};
Segtree seg;
struct Team{    // 输出答案用,表示编号为 id 的团队走了 x 个人
    LL x;
    bool flag;
    LL id;
};
vector<Team> v;      // 用来统计答案
LL sz[NR + 10];      // 表示团队 i 还剩下 sz[i] 个人
LL pos[NR + 10];     // 表示团队 i 被存在了第 pos[i] 个桶子里
set<LL> st[NR + 10]; // 桶子
void leave(LL id){          //团队离开队列时与团队全部上车后需要将其删除
    st[pos[id]].erase(id);  // 从桶子中删除编号为 id 的团队
    seg.modify(1, pos[id], pos[id], (st[pos[id]].empty() ? inf : *st[pos[id]].begin()) - seg.query(1, pos[id], pos[id])); // 更新这个桶子在线段树中对应的节点,注意若桶子为空,一定要在线段树中将其设为 inf
}

int main(){
    seg.build(1, 0, NR); // 建树
    LL T;
    scanf("%lld", &T);  // 读入
    LL cnt = 0;
    while(T--){
        LL op;
        scanf("%lld", &op);
        if(op == 1){ // join
            LL x, y;
            scanf("%lld%lld", &x, &y);
            cnt++;
            sz[cnt] = x;
            pos[cnt] = x;
            if(y) pos[cnt] = 0;
            st[pos[cnt]].emplace(cnt);
            seg.modify(1, pos[cnt], pos[cnt], *st[pos[cnt]].begin() - seg.query(1, pos[cnt], pos[cnt]));
        }
        else if(op == 2){ // leave
            LL x;
            scanf("%lld", &x);
            leave(x);
        }
        else if(op == 3){ // board
            v.clear();    // 多测要清空
            LL x;
            scanf("%lld", &x);
            while(x > 0){                               // 模拟上车
                LL tmp = seg.query(1, 0, x);            // 找到最靠前的团队
                if(tmp == inf) break;                   // 没有则 break
                LL del = min(x, sz[tmp]);               // 注意有的团队可以拆分,只要 x < sz[tmp] 时,团队一定都可以拆分(因为找的时候就保证了只要是不可拆的团队,其大小一定都小于 x)
                v.emplace_back(Team{del, false, tmp});  // 计入答案
                x -= del;                               // 更新车上剩余空间
                sz[tmp] -= del;                         // 更新团队没上车的人的数量
                if(sz[tmp] == 0) leave(tmp);            // 若全部上车,将空的团队删掉并更新这个桶子中排在最前面的
            }
            printf("%lld\n", (LL)v.size());   // 输出答案
            if(v.size() == 0) continue;
            for(auto i : v){
                printf("%lld %lld\n", i.id, i.x);
            }
        }
    }
    return 0;
}
好的,下面是一个简单的游乐场管理系统的实现,包括游乐设施、游乐场和文件读写三个类。请注意,这只是一个简单的实现,还有很多细节需要完善和优化。 ``` #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <string> using namespace std; class AmusementFacility { public: AmusementFacility(int id, string name, string manager, double price, string date): id_(id), name_(name), manager_(manager), price_(price), date_(date) {} int GetId() const { return id_; } string GetName() const { return name_; } string GetManager() const { return manager_; } double GetPrice() const { return price_; } string GetDate() const { return date_; } private: int id_; string name_; string manager_; double price_; string date_; }; class AmusementPark { public: AmusementPark() {} void AddFacility(const AmusementFacility& facility) { facilities_.push_back(facility); } void DeleteFacility(int id) { for (auto it = facilities_.begin(); it != facilities_.end(); ++it) { if (it->GetId() == id) { facilities_.erase(it); break; } } } double GetTotalIncome(string date) const { double total_income = 0; for (const auto& facility : facilities_) { if (facility.GetDate() == date) { total_income += facility.GetPrice(); } } return total_income; } double GetTotalIncome(string name, string date) const { double total_income = 0; for (const auto& facility : facilities_) { if (facility.GetName() == name && facility.GetDate() == date) { total_income += facility.GetPrice(); } } return total_income; } vector<pair<string, double>> GetFacilityIncome(string date_from, string date_to) const { vector<pair<string, double>> facility_income; for (const auto& facility : facilities_) { if (facility.GetDate() >= date_from && facility.GetDate() <= date_to) { bool found = false; for (auto& p : facility_income) { if (p.first == facility.GetName()) { p.second += facility.GetPrice(); found = true; break; } } if (!found) { facility_income.push_back(make_pair(facility.GetName(), facility.GetPrice())); } } } sort(facility_income.begin(), facility_income.end(), [](const pair<string, double>& p1, const pair<string, double>& p2) { return p1.second > p2.second; }); return facility_income; } private: vector<AmusementFacility> facilities_; }; class FileIO { public: static void ReadFromFile(string filename, AmusementPark& park) { ifstream fin(filename); if (!fin) { cout << "Error opening file " << filename << endl; return; } int id; string name; string manager; double price; string date; while (fin >> id >> name >> manager >> price >> date) { AmusementFacility facility(id, name, manager, price, date); park.AddFacility(facility); } fin.close(); } static void WriteToFile(string filename, const AmusementPark& park) { ofstream fout(filename); if (!fout) { cout << "Error opening file " << filename << endl; return; } for (const auto& facility : park) { fout << facility.GetId() << " " << facility.GetName() << " " << facility.GetManager() << " " << facility.GetPrice() << " " << facility.GetDate() << endl; } fout.close(); } }; int main() { AmusementPark park; FileIO::ReadFromFile("amusement_park.txt", park); // Add a new facility AmusementFacility new_facility(5, "Roller Coaster", "John", 50.0, "2021-07-01"); park.AddFacility(new_facility); // Delete a facility park.DeleteFacility(2); // Get total income of a certain date double total_income = park.GetTotalIncome("2021-06-30"); cout << "Total income on 2021-06-30: " << total_income << endl; // Get total income of a certain facility on a certain date double facility_income = park.GetTotalIncome("Ferris Wheel", "2021-06-30"); cout << "Income of Ferris Wheel on 2021-06-30: " << facility_income << endl; // Get income of all facilities in a certain time period vector<pair<string, double>> facility_income = park.GetFacilityIncome("2021-06-01", "2021-06-30"); cout << "Facility income from 2021-06-01 to 2021-06-30:" << endl; for (const auto& p : facility_income) { cout << p.first << ": " << p.second << endl; } FileIO::WriteToFile("amusement_park.txt", park); return 0; } ``` 以上代码实现了从文件里读出游乐场管理信息,并能将管理信息保存到文件。游乐场信息包括:游乐设施编号、游乐设备名称、管理人员、消费金额、消费日期。同时实现了增加和删除游乐场的消费记录,查询游乐场某个游乐项目某日的收入总额,查询游乐场某日的总收入,查询所有游乐设备的收入情况,并能根据某时段和某日的游乐设备收入高低排序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值