2 solutions
-
0
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> int n, m; int a[10010], f[10010], oil[10010], oil_cost[10010]; int read()//输入 { char c=getchar(); int x = 0, flag = 1; if(c == '-') { flag = -1; c = getchar(); } while('0' <= c && c <= '9') { x = x * 10 + (c - '0'); c = getchar(); } return x * flag; } void write(int r)//输出 { if(r>9) { write(r/10); } putchar((r % 10) + '0'); } int main(){ n = read(); m = read(); for(int i = 1 ; i <= n + 1; i ++) { a[i] = read(); } f[0] = 0; oil[0] = 0; oil_cost[0] = 0; for(int i = 1; i <= n + 1; i ++) { f[i] = f[i - 1] + 1; oil[i] = oil[i - 1] + a[i]; oil_cost[i] = a[i]; if(i == 1)//i==1时不用加油 { oil[i] = 0; oil_cost[i] = a[i]; } int flag = 1, x = i, s = 0; while(flag == 1 && x >= 1) { x --; s += a[x + 1]; if(s > m)//距离超过限制直接退出 { flag = 0; } else { if(oil_cost[x] + s <= m)//要满足在第x个加油车站时剩下的油足够跑过去 { f[i] = f[x]; oil[i] = oil[x]; oil_cost[i] = oil_cost[x] + s; } } } if(f[i] > f[i - 1] && !(i == 1))//如果加了一次油,更新加油数据(i==1时不用) { oil[i] = oil[i - 1] + oil_cost[i - 1]; } //std::cout << f[i] << " " << oil[i] << " " << oil_cost[i] << std::endl; } write(f[n + 1]); putchar(' '); write(oil[n + 1]); return 0; }
-
0
一眼dp
修bug修了半小时//Luogu : aaa_Pigeon 2023/12 #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> int n, m; int a[10010], f[10010], oil[10010], oil_cost[10010]; int read()//输入 { char c=getchar(); int x = 0, flag = 1; if(c == '-') { flag = -1; c = getchar(); } while('0' <= c && c <= '9') { x = x * 10 + (c - '0'); c = getchar(); } return x * flag; } void write(int r)//输出 { if(r>9) { write(r/10); } putchar((r % 10) + '0'); } int main(){ n = read(); m = read(); for(int i = 1 ; i <= n + 1; i ++) { a[i] = read(); } f[0] = 0; oil[0] = 0; oil_cost[0] = 0; for(int i = 1; i <= n + 1; i ++) { f[i] = f[i - 1] + 1; oil[i] = oil[i - 1] + a[i]; oil_cost[i] = a[i]; if(i == 1)//i==1时不用加油 { oil[i] = 0; oil_cost[i] = a[i]; } int flag = 1, x = i, s = 0; while(flag == 1 && x >= 1) { x --; s += a[x + 1]; if(s > m)//距离超过限制直接退出 { flag = 0; } else { if(oil_cost[x] + s <= m)//要满足在第x个加油车站时剩下的油足够跑过去 { f[i] = f[x]; oil[i] = oil[x]; oil_cost[i] = oil_cost[x] + s; } } } if(f[i] > f[i - 1] && !(i == 1))//如果加了一次油,更新加油数据(i==1时不用) { oil[i] = oil[i - 1] + oil_cost[i - 1]; } //std::cout << f[i] << " " << oil[i] << " " << oil_cost[i] << std::endl; } write(f[n + 1]); putchar(' '); write(oil[n + 1]); return 0; }
- 1
Information
- ID
- 1
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 8
- Tags
- (None)
- # Submissions
- 14
- Accepted
- 6
- Uploaded By