本题应该注意观察数据,可以首先看出如果距离L小于最大体力,直接输出Yes即可 如果L大于最大体力,我们可以再一次根据初始硬币分为c<1000,不能补给,直接输出No,1000<=c<2000,可以补给一次,此时只需要寻找补给点(如果存在补给点的距离小于最大体力,补给后能到达终点,且补给点收费低于你带的硬币),存在这样的补给点,就可以到达终点,也就是一直出队判断是否存在即可 带了2000的硬币,相对来说比较复杂,如果带了2000,补给一次可以到达,我们就输出Yes,如果不能到达,有可能补给两次就能到达了,但这样子每个商店的售价都必须为1000(这是题目的特点,要我们去发现),然后我们逐个出队将售价为1000的商点push-back到数组ths中去,(此处有一个小逻辑,如果只有一个1000的商店,假如还能到达,那我们应该在第一次判断补给一次的时候就已经成功了,所以1000的商店个数一定大于2个)。然后我们找出离起点最远但体力能到,离终点最远但体力能到的商店位置,如果这两个商店距离小于体力,则输出Yes,否则则输出No
#include<deque>
#include<vector>
using namespace std;
struct store {
int pi;
int ci;
store(int pi, int ci) {
this->pi = pi;
this->ci = ci;
}
};
class contest {
public:
int N, L, Max, S;
deque<store>astore;
contest(int N, int L, int Max, int S, deque<store>&q) {
this->L = L;
this->Max = Max;
this->N = N;
this->S = S;
while (!q.empty()) {
astore.push_back(q.front());
q.pop_front();
}
}
string answer() {
if (Max >= L)return "Yes";
if (Max < L && S < 1000)return "No";
else if (Max < L && S >= 1000 && S < 2000) {
int cnt = astore.size();
for(int i=0;i<cnt;i++){
store temp = astore.front();
if (temp.pi <= Max && temp.pi + Max >= L&&temp.ci<=S)astore.push_back(temp);
astore.pop_front();
}
if (!astore.empty())return "Yes";
else return "No";
}
else {
vector<store>ths;//辅助队列,只花费1000的补给的商店
int cnt = astore.size();
for (int i = 0; i < cnt; i++) {
store temp = astore.front();
if (temp.ci == 1000)ths.push_back(temp);
astore.push_back(temp);
astore.pop_front();
}
for (int i = 0; i < cnt; i++) {
store temp = astore.front();
if (temp.pi <= Max && temp.pi + Max >= L && temp.ci <= S)astore.push_back(temp);
astore.pop_front();
}
if (!astore.empty())return "Yes";
if (ths.size() < 2)return "No";//假如1000的商店小于两个还能走完,说明路程肯定小于两背体力,在上面就已经出去了
else {
if (ths[0].pi > Max)return "No";
if (ths.back().pi + Max < L)return "No";
int tmax,tmin;
for (int i = 0; i < ths.size(); i++) {
if (ths[i].pi < Max)tmax = i;
else break;
}
for (int i = ths.size()-1; i>0; i--) {
if (ths[i].pi+Max>L)tmin = i;
else break;
}
if (ths[tmin].pi - ths[tmax].pi <= Max)return "Yes";
else return "No";
}
}
}
};
int main() {
int N, L, Max, S;
deque<store>astore;
vector<contest>acontest;
while (cin >> N >> L >> Max >> S) {
for (int i = 0; i < N; i++) {
int pi, ci;
cin >> pi >> ci;
astore.push_back((store(pi, ci)));
}
acontest.push_back(contest(N, L, Max, S, astore));
}
for (int j = 0; j < acontest.size(); j++) {
cout << acontest[j].answer() << endl;
}
}js
我是菜鸡,不要喷,只是想写个题解记录一下自己而已,有改正意见欢迎指出。