- Today in doing POJ above a topic "MXN Puzzle", problem id:2893, test all passed, is prompted to time out, find the original source code comparison found on the Internet the same logic ... And then finally finally found the problem, in this article why does I get a time Limit exceeded? In the article mentions in C + +, do not use Cin/cout-use scanf and printf Inst EAD. Replace all cin,cout after accepted!
#include <iostream> #include <string.h>using namespace std; #define N 1000007int a[n];int B[n];long Long inv;/ /void merge (int first, int mid, int last) {int I, J, k;memcpy (b + First, A + first, (Last-first + 1) *sizeof (int)); k = i = First, J = mid + 1;while (I <= mid | | J <= last) {if ' (J > Last | | (i <= mid&&b[j] >= b[i])) {a[k++] = B[i++];inv + = (J-(Mid + 1));} else{a[k++] = B[j++];//inv + = ((mid + 1)-i) & 1;}}} void Msort (int first,int last) {if (first>=last) return;int mid = (first + last)/2;msort (first, mid); Msort (mid + 1, Las t); Merge (First, Mid, last);} int main () {int m, n, S, J, Iter, V, y = 0;while (scanf ("%d%d", &m,&n) ==2) {if (!m | |!n) break;s = m * N;for (iter = j = 0; J < S; J + +) {scanf ("%d", &v); v? (a[iter++] = v): (y = m-1-j/n);} INV = 0;msort (0, iter-1);//cout << INV << endl;//If the number of columns is odd, the upper and lower left and right movements of the numbers do not affect the total number of final reverse pairs if (N & 1) {y = 0;} printf (((y% 2 = = Inv% 2)? "yes\n": "no\n");} System ("Pause");}
Possible reasons for time Limit exceeded