Common Board=== Problem === I launch my solution with params: -Xms64m -Xmx64m. It works about 100 milliseconds on my laptop, but I get TimeLimitExceeded when I submit my solution. === Questions === 1. May you suggest what is wrong in my code below? 2. How can I look at input of failed test? === My code: === import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Cryptography { private final static int FIRST_PRIME = 2; private final static int SECOND_PRIME = 3; private final static int ADVANCE = 2; private final static Map<Integer, Integer> primesByOrder = new HashMap<Integer, Integer>(); static { primesByOrder.put(1000, 7919); primesByOrder.put(2000, 17389); primesByOrder.put(3000, 27449); primesByOrder.put(4000, 37813); primesByOrder.put(5000, 48611); primesByOrder.put(6000, 59359); primesByOrder.put(7000, 70657); primesByOrder.put(8000, 81799); primesByOrder.put(9000, 93179); primesByOrder.put(10000, 104729); primesByOrder.put(10500, 110597); primesByOrder.put(11000, 116447); primesByOrder.put(11500, 122251); primesByOrder.put(12000, 128189); primesByOrder.put(12500, 134053); primesByOrder.put(13000, 139901); primesByOrder.put(13500, 145931); primesByOrder.put(14000, 151703); primesByOrder.put(14500, 157739); primesByOrder.put(15000, 163841); } public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int size = in.nextInt(); for (int i = 0; i < size; i++) { int serialNumber = in.nextInt(); int nPrime = getNPrime(serialNumber); primesByOrder.put(serialNumber, nPrime); out.println(nPrime); } out.flush(); } protected static int getNPrime(int n) { if (n == 1) return FIRST_PRIME; if (n == 2) return SECOND_PRIME; int prime = SECOND_PRIME; int count = 2; for (int i : primesByOrder.keySet()) { if (n > i) { count = i; prime = primesByOrder.get(i); } else break; } while (count < n) { prime += ADVANCE; boolean isPrime = true; for (int i = FIRST_PRIME; i * i <= prime; i++) { if (prime % i == 0) { isPrime = false; break; } } if (isPrime) count++; } return prime; } } import java.util.Scanner; public class _1991 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a, b, i; a = sc.nextInt(); int[] str = new int[a]; for (i = 0; i < a; i++) { str[i] = sc.nextInt(); } b = sc.nextInt(); int counter = 0; for (i = 0; i < b; i++) { int c = sc.nextInt(); for (int aStr : str) { if (aStr == c) { counter++; } } } System.out.println(counter); } } import java.util.Scanner; public class _1636 { public static void main(String args[]) { Scanner s = new Scanner(System.in); int n, sum = 0; int a, b; a = s.nextInt(); b = s.nextInt(); for (int i = 1; i < 10; i++) { n = s.nextInt(); sum = sum + (n * 20); } a = a + sum; if (b < a) { System.out.println("Dirty debug :("); } else { System.out.println("No chance."); } } } import java.util.Scanner; public class AcmTimusRu { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); System.out.println(n * (n + 1) * (n + 2) / 2); } } import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class AcmTimusRu { public static void main(String[] args) { Scanner s = new Scanner(System.in); String[] strings = new String[1000]; Set<String> set = new HashSet<String>(); int n, i; n = s.nextInt(); n =n +1; for (i = 0; i < n; i++) { set.add(s.nextLine()); } if (n == set.size()) { System.out.println(0); } else { int n1 = set.size(); n1 = n - n1; System.out.println(n1); } } } Edited by author 01.05.2014 12:20 Edited by author 01.05.2014 12:20 import java.util.Scanner; public class AcmTimusRu { public static void main(String argv[]) { String[] nf = new String[0]; Scanner s = new Scanner(System.in); double somme = 0; double n = s.nextLong(); for (int i = 0; i < n; i++) { somme += s.nextLong(); } System.out.print((somme / n)); } } import java.text.NumberFormat; import java.util.Scanner; public class Heatingmain { public static void main(String argv[]) { NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(6); nf.setMinimumFractionDigits(6);
Scanner s = new Scanner(System.in);
double somme=0; double n = s.nextLong(); for (int i = 0; i < n; i++) { somme += s.nextLong(); } System.out.print(nf.format(somme/n)); }
} Edited by author 25.11.2010 03:26 The solution is to use format method of String class !!! System.out.print(String.format("%.6f",somme)); But still don't understand why the format method of NumberFormat class doesn't work !!! Edited by author 25.11.2010 03:30 import java.util.Scanner; public class AcmTimusRu { public static void main(String argv[]) { String[] nf = new String[0]; Scanner s = new Scanner(System.in); double somme = 0; double n = s.nextLong(); for (int i = 0; i < n; i++) { somme += s.nextLong(); } System.out.print((somme / n)); } } Because the input info states that "The single line of the input contains a sequence" ... The "Single" line but I got #1WA here's my code: #include<stdio.h> char str[10005],output[85],temp; int i,ptr; main() { /* while (scanf("%c",&temp)!=EOF) { if (temp >= 32) str[i++]=temp; } */ gets(str);
for (i=0;str[i];i++) { if (str[i]=='>') { if (output[ptr]!=0) { ptr++; if (ptr==80) ptr=0; } } else if (str[i]=='<') { if (ptr>0) ptr--; }
else { output[ptr]=str[i]; ptr++;
if (ptr==80) ptr=0; }
} printf("%s",output); scanf(" "); } Where is my mistake? Thanks :) ты строчку длины 85 выводишь package practice; import java.util.Scanner; public class Factorial { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner (System.in); System.out.println("enter the no"); int num = sc.nextInt(); String s = sc.next();
int k = s.length(); int fact = 1;
if(num %k ==0){ for(int i = num; i>=k; i = i-k){ fact = fact*i; } } else { for(int i =num; i >0; i = i-k){ fact = fact*i; } }
System.out.println(fact); } } 1000=1^3+0^3+0^3+0^3=1 1=1^3=1 IF THE DISTANCE BETWEEN TWO POINTS = 1 THAT FOLLOWING TOO WILL BE 1. USE BREAK It is wrong. "Oh, by the way! The value of SMK is always divisible by 3. It’s normal for Martians – all their numbers are divisible by 3." On another if the previous number is equal to next that break f(153)=1^3+5^3+3^3=1+125+27=153 =)) That's really so! :) Thank you! I've got AC! Its my solution: [code deleted] Edited by author 21.06.2007 01:35 Edited by moderator 06.12.2019 20:30 Your prog get AC, but look at this test: 1 33333 313 1 log of my prog: 2) 55 3) 250 4) 133 5) 55 6) 250 7) 133 8) 55 9) 250 10) 133 11) 55 12) 250 13) 133 14) 55 15) 250 16) 133 17) 55 18) 250 19) 133 20) 55 21) 250 22) 133 23) 55 24) 250 25) 133 26) 55 27) 250 ----- there is period! And you never get dist[j]==dist[j-1] And your prog get TLE on my test: ||| 33333 1) ||| 33333 313 1 2) ||| 33333 313 1 3) ||| 33333 313 1 4) ||| 33333 313 1 5) ||| 33333 313 1 i) ||| 33333 313 1 33333) ||| 33333 313 1 But there is no such tests => AC. =) [code deleted] Edited by author 02.12.2011 18:57 Edited by moderator 06.12.2019 20:31 313 isn't divisible by 3. program Project2; {$APPTYPE CONSOLE} uses SysUtils; type tt = record t1, t2, t3: integer; end; var i, n, time, tek: integer; a: array[1..50] of tt; begin readln(n); for i := 1 to n do readln(a[i].t1, a[i].t2, a[i].t3); { sort(1, n);} //....a[1].t1<a[2].t1<.....<a[n].t1 time := 0; tek := 0; for i := 1 to n do begin if tek >= a[i].t1 then tek := tek + a[i].t2 else tek := a[i].t1 + a[i].t2; if tek > a[i].t3 then begin time :=time+ tek - a[i].t3; tek := a[i].t3; end end; writeln(time); end. I don't understand too. Have tried many test... And it works correctly. Help please by giving test =). Try this 3 100 10 120 70 40 150 99 15 100 The answer is 25. Edited by author 09.08.2011 01:40 Why? I think it 35: 0 min: 1:100 10 120 2:70 40 150 3:99 15 100 Q{} 70 min: 1:30 10 120 3:29 15 100 Q{2(40)} 70+29 min: 1:1 10 120 Q{2(11),3(15)} 70+29+1 min: Q(2(10),3(15),1(10)} fin:100+10+15+10 min; Third student doesn't fit into 35 minutes. So? Extra time for the second one is 0 for the third is 25 for the first it is - 15 Shift = 40. Why is it incorrect? Algorithm - sort by T1 - results 2,3,1 Total time for the second student to accomplish = 110 110 > 99 so, here goes the third ready student 110+15=125. 125 - 100 = 25 Total time = 125. 125 > 100 so, here goes the first ready student 125+10=135. 135 - 120= 15 So the total shift is 40 minutes. What's wrong? :/ Oh, that's true!!! Thanks!! Какой там 2 тест? у меня не проходит(time limted); People, can u help with test #3? I'm receiving WA... "It is known as a fact, that ten walnuts is always enough and two walnuts are always not enough." thanks!! that helped me!! I make the same mistake too. It is never too careful to read the question. Hi! That is no surprise for you that O(N*logN) solution using C++ map<int, int> fits in 1 second perfectly. An idea is to create a problem "Order: version 2" with lower time limit (0.5 sec or something like that) and, maybe, higher amount of input. For what? Just for saying "YOU SHALL NOT PASS"© to O(N*logN) solutions and giving a chance only to O(N) algorithms (like Boyer-Moore Majority Vote Algorithm). And difficulty, of course, should be increased correspondingly. Thanks for attention. Long live Timus! Edited by author 11.01.2014 08:22 Looks like they take your advice, although not exactly. But not we can not get pass by using map. Levenshtein problem Not. This is not Levenshtein problem. It's different. Is such an input possible? 2 0 0 0 1 0 100 this means that teacher gave 1020 but the 20 that were given were unnecessary, and it's said that every note is needed. Thanks. No. Btw, my AC solution gives 0 on this test. That, there not such test case here. Because your code should be wrong. The answer for this is : 1 10, which is my AC code give, and should be as we calculate by ourselves. But according to the question statement, it should not give such test cases. Because the teacher would not give unnecessary money. Edited by author 28.04.2014 06:04 var sh,de:string; a:array[1..100]of integer; i,n:Integer; begin readln(sh); n:=length(sh); for i:=1 to n do a[i]:=ord(sh[i])-97; for i:=2 to n do if a[i]<a[i-1] then a[i]:=a[i]+((a[i-1]-a[i])div 26 + 1)*26; for i:=n downto 2 do a[i]:=a[i]-a[i-1]; a[1]:=a[1]-5; de:=''; for i:=1 to n do de:=de+chr(97+a[i] mod 26); writeln(de); end. i didn't understand where is my mistake a[1] can be less than 5. Edited by author 05.08.2011 16:43 Edited by author 05.08.2011 16:43 You are great! I got the same mistake. if you problem input is "adham" . what is output? output is "vdetm" . You should know if the first letter is "a" or "b" or "c" or "d" or "f" (char)("a" + 26) is correct way! Паскаль почти все задачи по лимиту не проходят на одно и то же время, почему??? The correct format of the input is: <width of the book w/o the cover> <width of the cover> <number of the book where the worm _finishes_> <number of the book where the worm _starts_> (so, the last two were just given in the incorrect order) I disagree. Order of input data is absolutely correct. So whould you mind explaining me the 'Sample output'? I treated the input as I wrote above and got AC.. I did as he told and got AC, too. You are wrong, guys. Input format is correct. You're lucky that ACed the problem without thinking about it. Think - and you'll find out that input specification is indeed correct - it's a very famous logical puzzle =) The input format is entirely correct. The problem requires knowledge about how REAL books are USUALLY placed on shelves. The problem is very old trick really. I am sure many of contestants were already familiar with it (I was). Edited by author 12.10.2008 20:12 Edited by author 12.10.2008 20:12 So.. Do you (all of you who says that input format is correct) mean that books are stored in a way that the last page is the leftmost one? If so than it's not surprising that the solution which swaps numbers of the books gets AC. yes, just imagine. when you place books on the shelf, the books are facing inwards, which means, the first page is on the right and the last page is on the left. Hah, I wasn't familiar with this particular trick, but I've done enough of the "simple" problems on Timus now to know that many problems derive their difficulty not from the underlying algorithm, but in correctly understanding problem statements which are sometimes tricky, ambiguous, or rely on unstated assumptions (like this one, assuming how people "normally" put books on shelves, and which side of the book contains the first page). So I'm learning to always ask "what's the catch?" and got AC first time on this one (after initial WA on many other "simple" problems). So solving Timus problems is successfully training me to read problems very carefully! If you are really smart, try the problems not one get AC before, otherwise get the hell out of your pretentious mask. |
|