OI program common design pitfalls

Do not be lazy

Sometimes for convenience, I will use a lot of macros. But recently I found the following two macros always problems:

1 #define SET(x,a) memset(x,a,sizeof(x))
2 inline void work(){
3     SET(head,0),SET(vis,0),SET(dis,0x3f);
4     //do something
5 }

This macro definition seems to be some baffling questions during initialization.

In addition, there is this:

1 #define RP(i,a,b) for(register int i=a; i<=b; i++)
2 inline void work(){
3     vector<int> ver;
4     //do something
5     RP(i,0,ver.size()-1){
6         //do something
7     }
8 }

When you define a  RP macro cycle, it will determine the number of issues. For example, in the above example, if ver.size () == 0 , it will not enter normal for this cycle. But after its macro definitions to determine the order seems to have changed.

Very strange, right? Therefore, some macro or do not mess with. Note also that this problem after me.

Do not mess card often

There are some online cards often weird way, for example, the i ++ written as i = - ~ i, or comma with plenty of links to a number of statements. For the former, this binary Optimized fact, not as imagined so well, to enhance the speed is not obvious. (Although I also like to write i ++ ++ i to increase speed) for the latter, though the comma operator to a certain extent, can improve efficiency, but some time will be some strange problems. I remember a long time ago, I like to put a lot of statements by a number of connected into a comma; but sometimes, which is part of the program may only few, the rest will be ignored! I do not know really have this problem, but my advice is do not mess with commas.

Note space complexity

When learning any algorithm or data structure, must be full and accurate record of its space complexity. For example, the space complexity of the trie degree is how much? Generally open much? No deposit to the stars with the chain before the drawing, whether the open side of the array to give the title twice? These issues do not pay attention, you may be able to pass a sample, but in the end may fail to get a point. If you are in the online evaluation, evaluation machine sometimes does not feedback RE, but WA. If you do not pay attention, because you debug this problem for a long time.

Note that type conversion

Always pay attention to the type of conversion. Sometimes even if you put all the numeric variables open to long long type, you might forget when assigned to an int expression of type conversion. In this way, the expression will overflow before being assigned. Any time you want to keep in mind the type of variables, and convert it from time to time.

In addition, the cast is considerably slower. If you want to convert into a long long type directly in front of the variable multiplied 1ll; for ordinary double type, can be directly multiplied by 1.0; for more specific or __int128 long double type, you can only cast a.

Guess you like

Origin www.cnblogs.com/LinearODE/p/11614318.html