import java.util.Scanner; /** * Created by Coder on 05.12.2014. */ public class Copy1131_ { public static void main(String[] args) { Scanner x = new Scanner(System.in); int n = x.nextInt(); int k = x.nextInt(); int time = 0, i = 1; long s = 1; while (s < n) { if (i >= k) { s += k; } else { s += i++; } time++; } System.out.println(time); } } Here is my program var n,k,i,time:longint; begin readln(n,k); if k=1 then writeln(n-1) else begin i:=0;time:=0;n:=n-1; while n>0 do begin if i<k then i:=i+1; n:=n-i;time:=time+1; end; writeln(time); end; end. > Here is my program > var > n,k,i,time:longint; > > begin > readln(n,k); > if k=1 then writeln(n-1) > else > begin > i:=0;time:=0;n:=n-1; > while n>0 do > begin >(***) if i<k then i:=i+1; > n:=n-i;time:=time+1; > end; > writeln(time); > end; > end. Have a look at the iteraton..as I understand i is the number of used cables... But on each iteration you should not add only one cable (***) but as much as possible, I mean if you have used i cables by now, next time you could use 2*i cables, if you have so much, NOT i:=i+1! Sample input: 12 6 Sample output: 4 1 sec. 1-2 2 sec. 1-3, 2-4 3 sec. 1-5,2-6,3-7,4-8 4 sec. 1-9,2-10,3-11,4-12 Bye! and Have a luck :) n = 8, k = 3 one computer already has got prog installed, so you start copying from that one. you copied to any one (to exactly one as problem requires), 1 hour passed, k = 2 (2 cables left). you copied to next(any) one, 2 hours passed, k = 1 (and 1 cable left), copied to next - 3 hours passed and no cables left !! at the moment there are 4 computers remaining without programm, so as i see its impossible to copy to all computers, becuase no cables left ... how the hell output is 4 ?? and how you use them 3 cables to copy to all computers ... Whats wroung in this ??? help me please, thanks ! #include <iostream> #include <algorithm> using namespace std; int main() { int n, a; cin >> n >> a; if (n == 1) cout << 0 << endl; else { int S = n - 1; //int S = n; for (int i = 1; i <= n; i++) { S = S - min(a, i); if (S <= 0) { cout << i << endl; break; } } } } Какие есть способы избежать Time limit? What are the ways to avoid Time limit? This is my Prog: var i,n,k,l,g:integer; begin read(n,k); l:=1; i:=1; while i<k do begin l:=l+i; inc(g); if l<n then i:=i*2 else break; end; while l<n do begin l:=l+k; inc(g); end; writeln(g); end. Why? This is my solution: #include <iostream> using namespace std; int main(){ long int a,b,f=0; long int k=1; cin>>a>>b; if (a==1) { f=0; cout<<f; return 0;
}
if (b>=a && a<=2) f=1;
else if(a!=1){ while (k<=b){ f++; k*=2;
} a -=k; while (a>0){ f++; a=a-b;
} } cout<<f;
return 0; } Some test for your program 10 1 ans 9 3 8 ans 2 Some test for your program 10 1 ans 9 3 8 ans 2 Спасибо, помогло=) import java.io.BufferedReader; import java.io.InputStreamReader; public class _1131 { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int minut = 0; int a = Integer.parseInt(br.readLine()); int b = Integer.parseInt(br.readLine()); if (b == 1 && a == 1) { System.out.println(b); return; } for (int ok = 1; ok < a; ) { if (ok == 1) { ok++; minut++; } if (ok - b == 0 && a - ok > b) { ok += b; minut++; } else if (ok - b == 0 && a - ok < b) { ok += a - ok; minut++; } if (ok < b && a - ok > b) { ok += ok; minut++; } else if (ok < b && a - ok < b) { ok += a - ok; minut++; } if (ok > b && a - ok > b) { ok += b; minut++; } else if (ok > b && a - ok < b) { ok += a - ok; minut++; } } System.out.println(minut); } } Why? I had the same problem and my mistake was treating n as always a power of 2, thus doing some extra work in the last step. Revise your code for that. Hope this helps. Please help, I feel that I got the correct idea. In this test use LONG INT. I use INT and got WA, but later I use LONG INT and got AC. Pleaze, give me some tests that I will be able to find my error. when i was writing my first solution i used ceil and log functions for calculating how many computers get copies with available cables (without using the same number of cables as one copy before)... so i calculated "int init = (int)ceil(log((double)k) / log(2.0));" and used that in my program but i got WA#6... then i wrote my own function that does exactly the same: int getPow(int x) { int c = 0; int n = 1; while(n < x){ c++; n *= 2; } return c; } and then i got AC... can someone explain in what case the ceil and log functions would give wrong results. THANK YOU I also didn't got that, but this is fact: I changed my ceils and logs to self-written function and got AC. It is still challenge for me to explain, why I've do the same.... Maybe the problem is due to the compiler... Same here....and I compared my log2(x)) to (int)(log((double)x) / log(2.0)) for all x from 1 to 0x7FFFFFFF on my system and they're all identical. You needn't use function log() or something else,You can use a const array like (2,4,8,16,32,64,128,256,512,1024....). import java.util.Scanner; public class Computers { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = 0; int com = in.nextInt(); int mod = in.nextInt();
if (mod == 1 && com == 1 ){ System.out.println(mod); return; }
for(int x = 1;x < com;){ if (x == 1){ x++; a++; } if (x - mod == 0 && com - x > mod){ x+=mod; a++; }else if (x - mod == 0 && com - x < mod){ x+= com - x; a++; } if (x < mod && com - x > mod){ x+=x; a++; }else if (x < mod && com - x < mod){ x+= com - x; a++; } if (x > mod && com - x > mod){ x+=mod; a++; }else if (x > mod && com - x < mod){ x+= com - x; a++; }
} System.out.println(a); } } The statement of the problem says: "In order to let them do it the program has to be copied to all the N computers". You should know that N is the quantity of all the computers INCLUDING the one which has the program already installed. So the program has to be copied not to N, but to (N-1) computers. I've just got WA because of it. Thanks! I got the same issue. It's quite not obvious from the problem statement. #include<stdio.h> int main(void){ int n,k,time,n1,i; scanf("%d%d",&n,&k); i=1; n1=1; time=0; while(n1<k && n1<n){ n1=n1+i; i++; time++; } if(n - n1>0){ if((n-n1) % k == 0) time=time+(n-n1)/k; else time=time+(n-n1)/k+1; } printf("%d",time); return 0; } Hints: 1) your while loop condition is not quite right. 2) you are not growing n1 properly in the while loop 3) your "if (n - n1>0)" is not necessary, if n-n1 == 0 then the division result will be zero 4) integer ceiling of x/y can be done in a single step like this: (x + y - 1)/y 5) it's generally a good idea to finish each line of output with a newline character In general: do some examples on paper, and step through your code in the debugger, or put in a bunch of print statements to follow the progress of the solution, and you'll discover where the problems are. Where can be error? #include <iostream> int main() { int k,s,t=0; scanf("%d%d",&k,&s); for (long int p=1; p<k; t++) p+=(p>s)?s:p; printf("%ld",t); return 0; } I think you can try this one: [code deleted] Edited by moderator 24.11.2019 12:47 Try this test: input: 1000000000 1 output: 999999999 Time is money ;) The only input line contains two integers separated with a space: N and K (0 < N ≤ 109, 1 < K ≤ 109). and There's a Test case with K = 1 which made me got 2 wrong answer...So funny Help Edited by author 25.04.2010 21:50 Edited by author 25.04.2010 21:50 Of course You can't solve this in O(n). Limitations is to high. Just try to find out a O(1) solution! =) |
|