疯狂的游戏 8.7笔试真题
作者:ontheway12138
链接:https://www.nowcoder.com/discuss/218832?type=all&order=time&pos=&page=1
来源:牛客网
1、大数取余
#include <iostream>
using namespace std;
long long strToInt(string str){
long long res = 0;
for(auto &it:str)
res = res * 10 + it-'0';
return res;
}
int main(){
string m;
long long n;
cin >> m >> n;
// Long long 最多支持 19位数, 就按照正常取余的过程,一步一步来即可
while(m.size() > 18){
string tem = m.substr(0,18);
long long num = strToInt(tem);
long long re = num % n;
m = to_string(re) + m.substr(18);
}
cout << strToInt(m) % n << endl;
}
2、小鹅冲冲冲
把经过的补给站都放到一个优先级队列里,每次补给
题目里的补给站的距离是距离终点的距离,这一块儿一定要看清
作者:ontheway12138
链接:https://www.nowcoder.com/discuss/218832?type=all&order=time&pos=&page=1
来源:牛客网
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int main(){
int n, M, E;
cin >> n >> M >> E;
priority_queue<int> que;
vector<pair<int,int>> arr(n);
for(int i=0; i<n; i++)
cin >> arr[i].first >> arr[i].second;
sort(arr.begin(), arr.end());
// ind 表示走到第几个补给站了 cur 表示离终点的距离,为 0 时,到达终点, res 记录去了几次补给站
int ind = arr.size()-1, cur = M-E, res = 0;
// 先把第一次能经过的补给站入堆
while(ind >=0 && arr[ind].first >= cur)
que.push(arr[ind--].second);
// 进入补给站
while(!que.empty() && cur > 0){
res++;
cur -= que.top();
que.pop();
// 遍历可能经过的补给站
while(ind >=0 && arr[ind].first >= cur)
que.push(arr[ind--].second);
}
if(cur > 0) cout << "-1" << endl;
else cout << res << endl;
return 0;
}