引言:ZZU的朋友们请不要直接抄,我只是记录每次做题的成果,别查重完之后大家一起GG。
汽车加油问题
题目来源:王晓东《算法设计与分析》
一辆汽车加满油后可行驶 n公里。旅途中有若干个加油站。设计一个有效算法,指出应 在哪些加油站停靠加油,使沿途加油次数最少。
输入格式:
第一行有 2 个正整数n和 k(k<=1000 ),表示汽车加满油后可行驶n公里,且旅途中有 k个加油站。 第二行有 k+1 个整数,表示第 k 个加油站与第k-1 个加油站之间的距离。 第 0 个加油站表示出发地,汽车已加满油。 第 k+1 个加油站表示目的地。
输出格式:
输出最少加油次数。如果无法到达目的地,则输出“No Solution!”。
输入样例:
7 7
1 2 3 4 5 1 6 6
输出样例:
4
Code:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <sstream>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+7;
using namespace std;
#define ARR_SIZE 10001
#define Num 500005
int main(){
int n,k;
cin>>n>>k;
int jyz[k+2];
int flag = 1;
for(int i = 0; i < k+1; i++){
cin>>jyz[i];
if(jyz[i] > n){
flag = 0;
}
}
if(flag == 1){
jyz[k+1] = 0;
int time = 0;
for(int i = 0; i < k+1; i++){
int temp = jyz[i];
while(n > temp){
temp += jyz[++i];
}
i--;
time++;
}
cout<<--time;
}
else{
cout<<"No Solution!";
}
return 0;
}