昨晚GG总结
第一题90
Runtime Error:Floating point exception
辅助解释:
Floating point exception:浮点错误,检查是否有除以零的情况
mdzz
#include
using namespace std;
#define Maxn 50005
int tl,rn,mr;
long long l[Maxn];
#ifdef DEBUG
FILE* debug;
#endifinline long long getLL(){register long long s=0;register int w=1;register char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;
}inline int geti(){register int s=0;register int w=1;register char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;
}void input(){tl=geti();rn=geti();mr=geti();for(register int i=0;i<rn;i++)l[i]=getLL();
}inline bool searchLength(long long length){
#ifdef DEBUGfprintf(debug,"Checking:%lld\n",length);
#endifregister long long lastLen=0;register int mrRemain=mr;for(register int i=0;i<rn;i++){if(l[i]-lastLen<length){mrRemain--;if(mrRemain<0){
#ifdef DEBUGfprintf(debug,"Check unpassed! %d node unpassed!lastLen=%lld,moveRockRemain=%d",i,lastLen,mrRemain);
#endifreturn false;}}elselastLen=l[i];}if(tl-lastLen>length&&mr<1){
#ifdef DEBUGfprintf(debug,"Check unpassed! Last node unpassed!lastLen=%lld,moveRockRemain=%d",lastLen,mrRemain);
#endif return false;}
#ifdef DEBUGfprintf(debug,"Check passed!\n");
#endifreturn true;
}long long binarySearch(long long minL,long long maxL){
#ifdef DEBUGfprintf(debug,"Searching:[%lld,%lld]\n",minL,maxL);
#endiflong long midL=(minL+maxL)>>1;if(midL<=minL) return searchLength(maxL)?maxL:minL;return searchLength(midL)?binarySearch(midL,maxL):binarySearch(minL,midL);
}int main(){
#ifdef DEBUGfreopen("0.txt","r",stdin);debug=fopen("debug0.txt","w");
#endifinput();
#ifdef DEBUGfprintf(debug,"End input:totalLength=%d,\nrockNum=%d,\nmoveableRock=%d\n",tl,rn,mr);for(int i=0;i<rn;i++){fprintf(debug,"Rock%d:%lld\n",i,l[i]);}
#endifprintf("%lld",binarySearch(1,tl/(rn-mr)+1));
}
然后发现最后一句会在测试点2炸了
本来只是想避免TLE,结果RE
把最后一句改成这样就AC了
printf("%lld",rn!=mr?binarySearch(1,tl/(rn-mr)+1):tl);
第二题:dp还是爆了0
TLE
//DEV又炸了,编译不执行MMP
#include
using namespace std;
int al,bl,sn;
char a[1005],b[205];
#ifdef DEBUG
FILE* debug;
#endifinline int geti(){register int s=0;register int w=1;register char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;
}inline void getvc(char*vc){register char ch=getchar();register int i=0;while(ch<'a'||ch>'z'){ch=getchar();}while(ch>='a'&&ch<='z') {*(vc+i)=ch;/*try{(*vc).push_back(ch);}catch(bad_alloc wrong){printf("caught bad_alloc!");}*/}
}void input(){al=geti();bl=geti();sn=geti();getvc(a);getvc(b);
}long long solve(int minb){if(minb>bl)return 0;if(minb==bl)return 1;int j;long long result=solve(minb+1);for(int i=0;i<al-bl;i++){if(a[i]==b[minb]){for(j=1;a[i+j]==b[minb+j];j++)if(minb+j+1==bl)break;}result=(result+solve(minb+j+1))%1000000007;}return result;
}int main(){
#ifdef DEBUGfreopen("1.txt","r",stdin);debug=fopen("debug1.txt","w");
#endifinput();
#ifdef DEBUG
#endifprintf("%lld",solve(0));
}
更早的代码(MLE)
//我知道会RE,我也知道哪句的问题,可我就是不知道咋办
#include
using namespace std;
#define Maxn 50005
int al,bl,sn;
vector<char>a,b;
#ifdef DEBUG
FILE* debug;
#endifinline int geti(){register int s=0;register int w=1;register char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();return s*w;
}inline void getvc(vector<char>*vc){register char ch=getchar();while(ch<'a'||ch>'z'){ch=getchar();}while(ch>='a'&&ch<='z') /*这句RE,抛出std::bad_alloc异常*/{(*vc).push_back(ch);}
}void input(){al=geti();bl=geti();sn=geti();getvc(&a);getvc(&b);
}long long solve(int minb){if(minb>bl)return 0;if(minb==bl)return 1;int j;long long result=solve(minb+1);for(int i=0;i<al-bl;i++){if(a[i]==b[minb]){for(j=1;a[i+j]==b[minb+j];j++)if(minb+j+1==bl)break;}result=(result+solve(minb+j+1))%1000000007;}return result;
}int main(){
#ifdef DEBUGfreopen("1.txt","r",stdin);debug=fopen("debug1.txt","w");
#endifinput();
#ifdef DEBUG
#endifprintf("%lld",solve(0));
}
当时先用的Vector,结果过了编译,
用TDM-GCC 4.8.1 32bit debug编译后抛出了std::bad_alloc
用try-catch后发现是vector的指针调用这个push_back函数时抛出的异常;
用gdb调试出异常 (*vc).push_back=
mdzz现在还是这个代码异常不出了
Sie ist ohne Ehre!
Verräter.

下面是关于std::bad_alloc异常的一篇博文
作者:tvxq_xy
来源:CSDN
原文:https://blog.csdn.net/tvxq_xy/article/details/50392384
版权声明:本文为博主原创文章,转载请附上博文链接!
当然,我这里只是针对我这一会儿的情况。最近跑的一个C++程序,迭代时间长,而且一共有800个迭代,等了一天,跑到第284个迭代就挂了,重新跑一次,同样的地方又挂了。各种检查是不是有申请了内存没释放(delete[])没释放的。最后,千辛万苦,终于发现,是vector这个小家伙捣的鬼。
在http://www.educity.cn/wenda/257316.html中说:
”事实超乎你的想象,我一个朋友用vector直接把内存吃光了,你好好检查下
如果有很多个的话,不妨手动清空掉,或者你不用vector试试,估计就不会出问题了
主要你知道吗,vector不止是出现内部内存碎片,它还导致外部内存碎片
vector开辟的是一个连续的空间,比方你这个空间是100M,分配不出来这么大的连续空间就报错了。
即使内存里还有1G但是没有连续100M的空间也会bad_alloc的“
下面摘自http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/08/2627666.html,很好的一个vector总结
说v.~ vector () ,销毁所有数据,释放内存。
而后又看到另一个帖子:
http://www.cnblogs.com/lxshanye/archive/2013/05/20/3088558.html
说C++中的vector不能像C#一样有托管的垃圾回收机制回收被占用的内存空间,但是你可以在使用完vector后调用~vector()析构函数释放内存。
赶紧试一下这种方法,果然一举成功!
再次感谢网上的大神们!
后来又不行了,跑到第1200多个迭代的时候,又崩了。这回请教了下坐在我身后的阿龙大神,他说,我一次读入vector的数据只有20万行的话,其实只有一两兆,很小。建议我自己先开个数组,然后不要clear,下一回就直接覆盖上去就好了。我谢过后,正准备改,阿龙一个激灵,站起身来跟我说,其实也不用自己开,只要将”new vector“那句放出去就好了,意思是因为我是在函数内new vector,这就只能在stack中开空间,stack只有一两兆。应该放到函数外,即file scope上,这样就是在heap里开空间,这很大,不用担心。
身边有大神的感觉就像现在窗外的天,让人心情明媚极了!
mmp我正在用vector 然后删了?

然后换TMD-GCC 8.1.0 32-bit debug
然后就是TLE
现在我才发现push_back后没getchar
mmp
第三题爆0
编译错误?
Main.cc: In function ‘void solve()’:Main.cc:55:127: warning: operation on ‘newCost[j][l]’ may be undefined [-Wsequence-point] newCost[j][l]=newCost[j][l]=newCost[j][l]-1?newCost[j][k]+newCost[k][l]:min(newCost[j][l],newCost[j][k]+newCost[k][l]); ^Main.cc:57:127: warning: operation on ‘newCost[l][k]’ may be undefined [-Wsequence-point] newCost[l][k]=newCost[l][k]=newCost[l][k]-1?newCost[l][j]+newCost[j][k]:min(newCost[l][k],newCost[l][j]+newCost[j][k]); ^Main.cc:59:127: warning: operation on ‘newCost[k][j]’ may be undefined [-Wsequence-point] newCost[k][j]=newCost[k][j]=newCost[k][j]==-1?newCost[k][l]+newCost[l][j]:min(newCost[k][j],newCost[k][l]+newCost[l][j]); ^/usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o): In function
std::basic_stringstd::string::_Rep::_S_empty_rep_storage’ defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o)/usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o): In function, std::allocator >::basic_string(std::string&&)':(.text._ZNSsC2EOSs[_ZNSsC5EOSs]+0x9): relocation truncated to fit: R_X86_64_PC32 against symbol std::string::_S_empty_rep()':(.text._ZNSs12_S_empty_repEv[_ZNSs12_S_empty_repEv]+0x3): relocation truncated to fit: R_X86_64_PC32 against symbolstd::string::_Rep::_S_empty_rep_storage’ defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o)/usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o): In functionstd::basic_stringstd::string::_Rep::_S_empty_rep_storage’ defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o)/usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o): In function, std::allocator >::basic_string()':(.text._ZNSsC2Ev[_ZNSsC5Ev]+0x3): relocation truncated to fit: R_X86_64_PC32 against symbol std::string::_Rep::_S_empty_rep()':(.text._ZNSs4_Rep12_S_empty_repEv[_ZNSs4_Rep12_S_empty_repEv]+0x3): relocation truncated to fit: R_X86_64_PC32 against symbolstd::string::_Rep::_S_empty_rep_storage’ defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o)/usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o): In functionstd::string::_S_construct(unsigned long, char, std::allocatorstd::string::_Rep::_S_empty_rep_storage’ defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o)/usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o): In functionconst&)':(.text._ZNSs12_S_constructEmcRKSaIcE[_ZNSs12_S_constructEmcRKSaIcE]+0xf): relocation truncated to fit: R_X86_64_PC32 against symbol std::string::_M_mutate(unsigned long, unsigned long, unsigned long)':(.text._ZNSs9_M_mutateEmmm[_ZNSs9_M_mutateEmmm]+0x8c): relocation truncated to fit: R_X86_64_PC32 against symbolstd::string::_Rep::_S_empty_rep_storage’ defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o)/usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o): In functionstd::string::_M_mutate(unsigned long, unsigned long, unsigned long)':(.text._ZNSs9_M_mutateEmmm[_ZNSs9_M_mutateEmmm]+0x104): relocation truncated to fit: R_X86_64_PC32 against symbolstd::string::_Rep::_S_empty_rep_storage’ defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o)/usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o): In functionstd::string::_M_mutate(unsigned long, unsigned long, unsigned long)':(.text._ZNSs9_M_mutateEmmm[_ZNSs9_M_mutateEmmm]+0x11f): relocation truncated to fit: R_X86_64_PC32 against symbolstd::string::_Rep::_S_empty_rep_storage’ defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o)/usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o): In functionstd::string::assign(std::string const&)':(.text._ZNSs6assignERKSs[_ZNSs6assignERKSs]+0x26): relocation truncated to fit: R_X86_64_PC32 against symbolstd::string::_Rep::_S_empty_rep_storage’ defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o)/usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o): In functionstd::string::assign(std::string const&)':(.text._ZNSs6assignERKSs[_ZNSs6assignERKSs]+0x69): relocation truncated to fit: R_X86_64_PC32 against symbolstd::string::_Rep::_S_empty_rep_storage’ defined in .bss._ZNSs4_Rep20_S_empty_rep_storageE[_ZNSs4_Rep20_S_empty_rep_storageE] section in /usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o)/usr/lib/gcc/x86_64-linux-gnu/4.8/libstdc++.a(string-inst.o): In function `std::basic_stringstd::operator+ (char const*, std::basic_string const&)’?.text.ZStplIcSt11char_traitsIcESaIcEESbIT_T0_T1_EPKS3_RKS6[ZStplIcSt11char_traitsIcESaIcEESbIT_T0_T1_EPKS3_RKS6]+0x20): additional relocation overflows omitted from the outputcollect2: error: ld returned 1 exit status
mmp
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
