Страница 3 3 5 -> 4 5 3 -> 5 1 5 -> 0 5 1 -> 1 10 10 -> 18 99999 99999 -> 199996 2147483647 2147483647 -> 4294967292 I hope it helps! Страница 2 import sys n,m=map(int,input().split()) n1=n m1=m j=0 ans=0 if n1==1 or m1 ==1: print(0) sys.exit() while n1>0 and m1>0: if ans%2==0 and j!=0: if m1>0: m1-=1 ans+=1 j+=1 else: print(ans) sys.exit() elif ans%2==1 and j!=0: if n1>0: n1-=1 ans+=1 j+=1 else: n1-=1 m1-=1 ans+=1 j+=1 print(ans) I print 2 * (n - 1) when n <= m and 2 * (m - 1) + 1 when n > m but got WA on test 13. What's wrong with my solution? Oh, I forgot to delete my check for m = 1 (in this case I output 1). This test seem to be 1 1 0 Edited by author 03.10.2018 17:42 answer = min(2*(n-1), 2*m-1); //don't forget about 32-bit signed integer overflow #include <stdio.h> int main() { int N, M; scanf("%d%d",&N,&M); long int res; if (N <= M) res = 2 * (N - 1); else res= 2 * (M - 1)+1; printf("\n%d",res); return 0; } Can't solve this promlem myself. I run your code online on some tests, it seemed ok. Check, maybe you go out of range in res? (know nothing about C) #include<stdio.h> #include<string.h> int f(int n,int m) { if(n==1||m==1) return 0; if(n==2&&m>1) return 2; if(n==3&&m==2) return 3; if(m>2&&n>2) return f(n-2,m-2)+4; } int main() { int n,m; scanf("%d %d",&n,&m); printf("%d",f(n,m)); fflush(stdin); getchar(); return 0; } Input "N=1 M=1" doesn't give you zero. Test 12 is overflow case. Be careful with data type. It is so strange! If I use int for N and M, I get WA in test 12. If I use long, it is OK. But 2^31 - 1 (the max value of N, M) is normal for int. So I think the test 12 uses numbers larger than 2^31 - 1. I got WA 10, where is wrong? /////////////////////// #include <iostream> using namespace std; int main() { int N,M,sum,counter,mul; counter=0; cin>>N>>M; sum=M; mul=N*M; while(sum!=mul) { N--; sum+=N; counter++; if(sum==mul) break; M--; sum+=M; counter++; } cout<<counter<<endl; } hi, in the acceptable answer the answer of 5 4 is 7 but you can easily understand that the real answer is 6(you can test it by drawing a simple 5 4 table). here is a sample answer: #include <iostream> using namespace std; int main() { int unsigned long long N , M , turn = 0; cin >> N >> M;
while( N > 2 && M > 2){ turn += 4; N -= 2; M -= 2; } if (N == 2 && M > 1 || M == 2 && N > 1) turn += 2;
if (M == 1 && N > 1) turn += 1; cout << turn << endl;
system ("PAUSE"); return 0 ;
} Nope...for 5 4 , answer is 7. For 4 5, answer is 6. Edited by author 05.07.2015 11:06 Edited by author 05.07.2015 11:06 System.out.println((Math.min(N - 1L, M - 1L) << 1) + (N > M ? 1 : 0)); #include<cstdio> #include<cstring> #include<cassert> #include<vector> #include<list> #include<queue> #include<map> #include<set> #include<deque> #include<stack> #include<bitset> #include<algorithm> #include<functional> #include<numeric> #include<utility> #include<sstream> #include<iostream> #include<iomanip> #include<cmath> #include<cstdlib> #include<ctime> #include<fstream> #include<typeinfo> #include<locale> #include<iterator> #include<valarray> #include<complex> using namespace std; int main() { int n,m; while(cin>>n>>m){ int sum = 0,flag = 0,count = -1,x=n*m; n--; if(n==0 || m==0){ cout<<"0"<<endl; continue; } while(sum < x){ if(flag==0){ sum += m; flag = 1; m--; } else if(flag==1){ sum += n; flag = 0; n--; } count++; } cout<<count<<endl; } return 0; } here is my code, it has got verdict WA in test case 10.... think about : F(N,M) = F(N-? , M- ? ) and give the initial values. The number of steps is too big. By doing a single recursive step you reduce the size of the original problem to (N - 2, M - 2). If we were to reduce the size to (N / 2, M / 2) (notice that we changed minus sign to division), then we could solve it recursively. Instead you should think of this problem as a whole. There are N rows and M columns. So some part of the robots path is by row and some part is by column: path = row|column|row|column|row|column... And we are counting the number of signs | in the path. As we see, each column is surrounded by sign | from both of its sides: "|column|". The first rough estimate that comes to mind after this observation is that probably the robot does 2 * (number of columns) turns. And by thinking of different specific cases of (N, M) we can turn this estimate into a true measure of the number of turns of the robot in the general case. Edited by author 30.10.2016 17:25 I have correct answer on PC and wrong - here?Why? #include<stdio.h> int main(void){ int n,m; unsigned int k; scanf("%d %d",&n,&m); k=0; while(n>2 && m>2){ k=k+4; n-=2; m-=2; } if(n==2 && m>=2) k=k+2; else if(m==2 && n>2) k=k+3; else if(m==1 && n>1) k++; printf("%d",k); return 0; } when m=1, the answer must be 0, but my AC submission's answer is 1!! Edited by author 22.09.2011 18:12 You are wrong. Answer for m = 1, n > 1 is 1. The robot makes turn in the first cell. what is it?? Use long long if using C++ or BigInteger in Java to save the value of N, M No need for long long or BigIntegers Just unsigned integer is more than enough unsigned int in C/C++ uint in C# don't use longint. do it using int64 When I use doubele i got wrong in 12. But after I use BigInteger and got AC. And what answer is when N=3 and M=1 ? >>> 0 Why 0? Maybe it have to turn once? my AC solution prints 1 Edited by author 19.11.2010 15:14 Страницы: 3 2 1 Предыдущая |
|