我试图解决这个问题:https://www.hackerrank.com/challenges/board-cutting为什么这种()会改变矢量的值?
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#define LL long long
#define M 1000000007LL
using namespace std;
int T,n,m;
LL cc;
struct Cut{
int dir;
LL cost;
Cut(const int& dir, const LL& cost): dir(dir), cost(cost){}
Cut(){}
bool operator<(const Cut& cut) const
{
return this->cost >= cut.cost;
}
};
vector<Cut> c;
int main() {
cin >> T;
while(T--){
cin >> n >> m;
LL ans = 0, cnt[2] = {0};
c.clear();
for(int i=0; i<n-1; i++) cin >> cc, c.push_back(Cut(0, cc));
for(int i=0; i<m-1;i++) cin >> cc, c.push_back(Cut(1, cc));
sort(c.begin(), c.end());
for(int i=0; i<c.size(); i++){
cout << c[i].cost << endl;
}
cout << "END" << endl;
for(Cut x : c){
(ans += x.cost*(1+cnt[!x.dir]))%=M;
cnt[x.dir]++;
}
cout << ans << endl;
}
return 0;
}
我有这样一个C++代码,从STD I/O
我添加了一个环,以输出矢量对象成本属性消耗输入。 现在以下输入:
2
83 99
24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24
34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34
99 49
11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11
61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61
第二个测试案例不具有成本24(仅第一测试案例有24个) 但是,如果你运行代码,你会发现,载体含有切割对象在排序之后花费24()!
为什么会发生这种情况,我该如何解决?
(以上样品输入针对此问题hackerrank正式测试用例输入,所以它的正确性是有保证的)
你应该更好地描述你的问题。也许尝试更简单的测试集?使用调试器来验证输入和输出。什么是输入,输出什么? – JeffRSon
@JeffRSon已编辑包含问题链接,我试着减少n,m,但不能再现同一问题:( – shole