我试着在spoj上解决这个问题。 http://www.spoj.com/problems/BUSYMAN/
虽然我能够解决它,但我得到了一个非常奇怪的错误。 我试过了解它的原因却失败了。 我有两个代码。
////////////////////////////////////////////////// /
#include<iostream> #include<vector> #include<algorithm> using namespace std; class activity { public: int start,end; }; bool comp(activity p, activity q) { if(p.end<q.end)return true; if(p.end==q.end&&p.start<=q.start)return true; return false; } int main() { int t; cin>>t; vector<activity> v; for(int i=0;i<t;i++) { int n; cin>>n; v.resize(n); for(int j=0;j<n;j++)cin>>v[j].start>>v[j].end; sort(v.begin(),v.end(),comp); int ans=0,currend=0; for(int j=0;j<n;j++) { if(v[j].start>=currend){ans++;currend=v[j].end; } } cout<<ans<<endl; } }/////////////////////////////////////////////
#include<iostream> #include<vector> #include<algorithm> using namespace std; class activity { public: int start,end; }; bool comp(activity p, activity q) { if(p.end<q.end)return true; if(p.end==q.end&&p.start>=q.start)return true; return false; } int main() { int t; cin>>t; int n; vector<activity> v; for(int i=0;i<t;i++) { cin>>n; v.resize(n); for(int j=0;j<n;j++) cin>>v[j].start>>v[j].end; sort(v.begin(),v.end(),comp); int ans=0,currend=0; for(int j=0;j<n;j++) { if(v[j].start>=currend) { ans++;currend=v[j].end; } } cout<<ans<<endl; } }//////////////////////////////
我的问题是第一个在spoj上给出了分段错误而第二个没有。 两者之间的唯一区别是比较功能。 我恰好以两种不同的方式定义了比较函数的第二个语句。 但它在第一种情况下给了我分段错误,但在第二种情况下没有。
在此处输入图像描述
在上面的两个图像中,有两个代码具有相应的提交ID,第三个图像显示一个故障,而不是其他。 您也可以使用我的spoj配置文件中的提交ID进行验证。
I tried solving this probblem on spoj. http://www.spoj.com/problems/BUSYMAN/
Although I was able to solve it but I got a very strange error. I tried understanding the cause of it but failed. I have two codes.
///////////////////////////////////////////////////
#include<iostream> #include<vector> #include<algorithm> using namespace std; class activity { public: int start,end; }; bool comp(activity p, activity q) { if(p.end<q.end)return true; if(p.end==q.end&&p.start<=q.start)return true; return false; } int main() { int t; cin>>t; vector<activity> v; for(int i=0;i<t;i++) { int n; cin>>n; v.resize(n); for(int j=0;j<n;j++)cin>>v[j].start>>v[j].end; sort(v.begin(),v.end(),comp); int ans=0,currend=0; for(int j=0;j<n;j++) { if(v[j].start>=currend){ans++;currend=v[j].end; } } cout<<ans<<endl; } }/////////////////////////////////////////////
#include<iostream> #include<vector> #include<algorithm> using namespace std; class activity { public: int start,end; }; bool comp(activity p, activity q) { if(p.end<q.end)return true; if(p.end==q.end&&p.start>=q.start)return true; return false; } int main() { int t; cin>>t; int n; vector<activity> v; for(int i=0;i<t;i++) { cin>>n; v.resize(n); for(int j=0;j<n;j++) cin>>v[j].start>>v[j].end; sort(v.begin(),v.end(),comp); int ans=0,currend=0; for(int j=0;j<n;j++) { if(v[j].start>=currend) { ans++;currend=v[j].end; } } cout<<ans<<endl; } }//////////////////////////////
My problem is that the first one gives segmentation fault on spoj while second one does not. The only difference between the two is the comparison function. I just happen to define the second statement of the comparison function in two different ways which are similar. But it gives me segmentation fault in the first case but not in second case.
enter image description here
In the two images above there are two codes with respective submission ids and in the third it shows seg fault for one while not for other. You can verify with the submission ids on my spoj profile as well.
最满意答案
因为bool comp(activity p, activity q)不符合Compare要求,请参阅std::sort
它应该是这样的:
bool comp(const activity& p, const activity& q) { return p.end < q.end || (p.end ==q.end && p.start < q.start); }要么
struct comp { bool operator()(const activity& p, const activity& q) const { return p.end < q.end || (p.end ==q.end && p.start < q.start); } };要么
struct comp { bool operator()(const activity& p, const activity& q) const { return std::tie(p.end, p.start) < std::tie(q.end, q.start); } };Because bool comp(activity p, activity q) doesn't meet requirements of Compare see std::sort
It should be this:
bool comp(const activity& p, const activity& q) { return p.end < q.end || (p.end ==q.end && p.start < q.start); }or
struct comp { bool operator()(const activity& p, const activity& q) const { return p.end < q.end || (p.end ==q.end && p.start < q.start); } };or
struct comp { bool operator()(const activity& p, const activity& q) const { return std::tie(p.end, p.start) < std::tie(q.end, q.start); } };更多推荐
发布评论