10.27比赛题解

· · 个人记录

10.27比赛题解

t1.数字游戏

题目大意

小 K 同学向小 P 同学发送了一个长度为 8 的 01 字符串来玩数字游戏,小 P 同学想要知道字符串中究竟有多少个 1。注意:01 字符串为每一个字符是 0 或者 1 的字符串,如 101 为一个长度为 3 的 01 字符串。

题目解析

送分题, 遍历一遍字符串就行


#include<bits/stdc++.h>
using namespace std;
int ans;
string s;
int main () {
cin >> s;
for (int i = 0; i < s.size(); i ++) ans += (s[i] == '1');
cout << ans;
return 0;
}
> ## t2.公交换乘
> > ### 题目大意
> > 著名旅游城市 B 市为了鼓励大家采用公共交通方式出行,推出了一种地铁换乘公交车的优惠方案:1. 在搭乘一次地铁后可以获得一张优惠票,有效期为 45 分钟,在有效期内可以消耗这张优惠票,免费搭乘一次票价不超过地铁票价的公交车。在有效期内指开始乘公交车的时间与开始乘地铁的时间之差小于等于 45 分钟,即:$t_{bus} - t_{subway} \leq 45$。2. 搭乘地铁获得的优惠票可以累积,即可以连续搭乘若干次地铁后再连续使用优惠票搭乘公交车。3. 搭乘公交车时,如果可以使用优惠票一定会使用优惠票;如果有多张优惠票满足条件,则优先消耗获得最早的优惠票。现在你得到了小轩最近的公共交通出行记录,你能帮他算算他的花费吗?
> > ### 题目解析
> > 可以使用动态数组$vector$模拟队列, 然后把优惠票进行一个管理
> >
> > **管理方法:**  
> > * 定义队头$head$和队尾$len$用于模拟队列
> > * 每次遍历队列, 如果和当前时间相差大于$45$, 那么久直接删除该元素
> > * 使用时直接用队头的就行
```cpp
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 1e5 + 5;
struct node{
    bool flag;
    int p, t;
} a[MAXN];
vector <node> q;
ll n, ans = 0;  
int head, len;
int main() {    
    cin >> n;
    for(int i = 0; i < n; i ++) cin >> a[i].flag >> a[i].p >> a[i].t;
    for (int i = 0; i < n; i ++) {
        if (!a[i].flag) {  
            q.push_back(a[i]);  
            ans += a[i].p;   
        } else { 
            bool f = 1;  
            len = q.size();  
            while (head < len && a[i].t - q[head].t > 45) head ++;
            for (int j = head; j < len; j ++) {
                node x = q[j];
                if(a[i].t - x.t <= 45 && a[i].p <= x.p){
                    f = false;
                    q[j].p = -1;
                    break;      
                }
            }
            if (f) ans += a[i].p;
        }
    }
    cout << ans << '\n';
    return 0;
}