我正在练习Google kickstart的B轮公交路线问题。我实际上查看了他们的分析并实施了他们的替代答案。
我还将问题提示粘贴到我的代码下方。 https://codingcompetitions.withgoogle.com/kickstart/round/000000000019ffc8/00000000002d83bf
我的解决方案通过了第一个测试集,但在第二个测试集上得到了错误的答案。我不知道第二个测试集是什么,只知道它很大。我很困惑,因为我的解决方案是对问题的分析,实际上是对T的替代解决方案的实现。我也不知道如何找出哪个测试用例可能给出错误的答案,似乎有很多可能性!
我什至不知道如何调试这样一个模糊的答案。也许有些我不考虑的情况?
#include <iostream> // includes cin to read from stdin and cout to write to stdout
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, d;
cin >> t; // read t. cin knows that t is an int, so it reads it as such.
for (int i = 1; i <= t; ++i) {
cin >> n >> d; // read n and then m.
stack <int> bus;
for(int j=0; j<n; j++){
int x;
cin >> x;
bus.push(x);
}
while(!bus.empty()){
int b = bus.top();
bus.pop();
d = d - d%b;
}
cout << "Case #" << i << ": " << d << endl;
}
return 0;
}
****这是问题提示的简化版****
问题 Bucket计划乘公共汽车穿越乡村很长的路程。她的旅程包括N条巴士路线,按必须乘坐的顺序从1到N。公交车本身非常快,但不经常行驶。第i条公交路线每隔Xi天运行一次。
更具体地说,她只能在第Xi,2Xi,3Xi等天乘坐第i条巴士。由于公共汽车非常快,她可以在同一天乘多辆公共汽车。
Bucket必须在D天之前完成旅程,但是她想尽可能晚地开始旅程。她最晚可以乘搭第一班车,但仍在D日前完成旅程?
可以肯定的是,Bucket可以在D天结束她的旅程。
输入 输入的第一行给出测试用例的数量,T。每个测试用例都从包含两个整数N和D的行开始。然后,接下来的一行包含N个整数,第i个是Xi。
输出 对于每个测试用例,输出一行,其中包含案例#x:y,其中x是测试用例编号(从1开始),y是她可以乘搭第一班车的最后一天,但仍要在D天之前完成旅程。 / p>
限制 时间限制:每个测试集10秒。 内存限制:1GB。
答案 0 :(得分:1)
我的猜测是使用int
是不够的,因为在测试集2中D
最多可以达到10 ^ 12
编辑:我证实了我的猜测。您将可以通过解决此错误来解决此问题。而且我相信long long
在编码竞赛中很常见,即每次都要注意输入/输出约束。
答案 1 :(得分:0)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main ()
{
int t;
long long int x,i,j,n,d;
cin >> t;
for ( i = 0; i < t; i++)
{
cin >> n >> d;
stack < long long int >route;
for (j = 0; j < n; j++)
{
cin >> x;
route.push (x);
}
while (!route.empty ())
{
long long int c = route.top ();
route.pop ();
d = d - d % c;
}
cout << "Case #" << i+1 << ": " << d << endl;
}
return 0;
}