Edited by author 22.01.2016 17:19 here is my code if someone can help me, thanks you and greetings. import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Scanner; public class P1100 { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int [] vec=new int[n*2]; for(int i=0;i<n*2;i++)vec[i]=in.nextInt(); int [] aux=new int[n]; int mayor=0,menor=100; for(int i=1;i<n*2;i=i+2){ if(menor>vec[i]){ menor=vec[i]; } if(mayor<vec[i]){ mayor=vec[i]; } } int sig=-1; for(int i=0;i<n;i++){ for(int j=1;j<n*2;j=j+2){ if(mayor==vec[j] && vec[j]!=-1 ){ System.out.printf(" %d %d",vec[j-1],vec[j]); vec[j]=-1;
} if(sig<mayor && sig < vec[j] && vec[j]!=-1){ sig=vec[j]; } } mayor=sig; sig=-1; } out.flush();
} } Your solution contains 2 nested loops with range [0..n) each. So you have n^2 complexity. Of course you have TLE.
You should better load input into array of structures (with fields Id, Score), sort array by score field in decreasing order and print it. * "Collections.sort" is stable so you can just use it. im trying now with a arrayList of a node(int ele,int pos), but i dont know how no order that, can you help me? here is my code if someone can help me, thanks you and greetings. import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Scanner; public class P1100 { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); int [] vec=new int[n*2]; for(int i=0;i<n*2;i++)vec[i]=in.nextInt(); int [] aux=new int[n]; int mayor=0,menor=100; for(int i=1;i<n*2;i=i+2){ if(menor>vec[i]){ menor=vec[i]; } if(mayor<vec[i]){ mayor=vec[i]; } } int sig=-1; for(int i=0;i<n;i++){ for(int j=1;j<n*2;j=j+2){ if(mayor==vec[j] && vec[j]!=-1 ){ System.out.printf(" %d %d",vec[j-1],vec[j]); vec[j]=-1;
} if(sig<mayor && sig < vec[j] && vec[j]!=-1){ sig=vec[j]; } } mayor=sig; sig=-1; } out.flush();
} } Do not forget that you should compare solved tasks as ints, not as strings :) Time limit is 1.0s, my program executes test 11 in 0.187s the result is TLE, why ? After conversion my program from C# to Java(with which I am not familiar) I have AC. Thus I think that there is a problem with C# judge for this problem. Yeah, I have the same problem. May be array, u give 150000. But need one more. +1 #include <iostream> using namespace std; int main() { int** m; long n; cin>>n; m = new int*[n]; for (long i=0; i<n; i++) { m[i]=new int[2]; m[i][1]=-1; } for (long j=0; j<n; j++) { int g, t; cin>>g>>t; if (j==0) { m[0][0]=g; m[0][1]=t; continue; } else for (long i=0; i<=j; i++) {
if (m[i][1]==t) while (m[i][1]==t && i<j) i++; else if (m[i][1]>t && m[i][i]!=-1) continue;
if (i==j) { m[j][0]=g; m[j][1]=t; break; } { for (long p=j; p>i; p--) { m[p][0]=m[p-1][0]; m[p][1]=m[p-1][1]; } m[i][0]=g; m[i][1]=t; break; } break; } } for (int i=0; i<n ;i++) { cout<<m[i][0]<<" "<<m[i][1]<<'\n'; } delete[] *m; return 0; } Is not advisable to use dynamic memory. I recommend you use STL <vector> program Bourne; var n,i,j,x:longint; a:array[0..150000] of smallint; b:array[0..150000] of longint; begin readln(n); for i:=1 to n do begin readln(b[i],a[i]); end; for x:=100 downto 0 do begin for i:=1 to n do if a[i]=x then writeln(b[i],' ',a[i]); end; end. :) interesting... good done! :) Re:Your code is wrong!!!!..Memory limite exid Edited by author 13.05.2006 17:02 Edited by author 13.05.2006 17:02 MLE Maybe tests were changed since april... Isnt it, Judges? Hmm... It gets MLE, really. Do they count memory on ALL tests, not on every test separately? Update: I've rewrited it, so it doesn't get MLE: #include <stdio.h> void main() { long n; unsigned char a[150000]; unsigned char hi[150000]; unsigned short lo[150000]; scanf("%ld",&n); for(long i=0;i<n;i++) { long bod; scanf("%ld%d",&bod,&a[i]); lo[i]=bod%65536; hi[i]=bod/65536; } for(int v=100;v>=0;v--) { for(int i=0;i<n;i++) if(a[i]==v) printf("%ld %d\n",hi[i]*65536+lo[i],a[i]); } } How can THIS get WA#11??? Edited by author 06.01.2006 21:12 I try so var a: array[0..100,0..150000]of longint; o,p,n,j,i : longint; begin readln(n); for i:=1 to n do begin readln(o,p); inc(a[p,0]); a[p,a[p,0]]:=o; end;
for i:= 100 downto 0 do begin for j:=1 to a[i,0] do writeln(a[i,j],' ',i); end; end. try to concate K and ID into 1 longint variable (because 10000000=3 bytes, 1 extra) using shl, shr. I've goten AC and wish you the same Edited by author 08.02.2006 17:12 Edited by author 08.02.2006 17:12 1. Replace 150000 with 150001 2. C++ solution doesn't get MLE even without hi-lo representation, simply use long. Hmm... It gets MLE, really. Do they count memory on ALL tests, not on every test separately? Update: I've rewrited it, so it doesn't get MLE: #include <stdio.h> void main() { long n; unsigned char a[150000]; unsigned char hi[150000]; unsigned short lo[150000]; scanf("%ld",&n); for(long i=0;i<n;i++) { long bod; scanf("%ld%d",&bod,&a[i]); lo[i]=bod%65536; hi[i]=bod/65536; } for(int v=100;v>=0;v--) { for(int i=0;i<n;i++) if(a[i]==v) printf("%ld %d\n",hi[i]*65536+lo[i],a[i]); } } How can THIS get WA#11??? Edited by author 06.01.2006 21:12 > 1. Replace 150000 with 150001 Thanks, it works! AC You are good! Thank you! You are genius! But my program work faster :-)) But it doesn't work :-| #include<iostream> int main() { using namespace std; unsigned long long n,id[10000],x; int m[100],y,i,j; cin>>n; for(i=0;i<n;i++) cin>>id[i]>>m[i]; for(i=0;i<n;i++) { for(j=i;j<n;j++) { if(m[i]<m[i+1]) { x=id[i]; id[i]=id[i+1]; id[i+1]=x; y=m[i]; m[i]=m[i+1]; m[i+1]=y; } } }
for(i=0;i<n;i++) cout<<id[i]<<" "<<m[i]<<"\n"; return 0; } #include <iostream> int main() { int data[150000][2],i,j,n,step,temp;
std::cin >> n; std::cin.ignore(255, '\n'); for(i=0;i<n;++i) {
for (j = 0; j < 2; ++j)
std::cin>> data[i][j]; std::cin.ignore(255, '\n'); } j = 1; bool is_sorted; for(step=0;step<n-1;++step){ is_sorted = true; for(i=0;i<n-step-1;++i) {
if(data[i][j]<data[i+1][j]) { temp=data[i][j]; data[i][j]=data[i+1][j]; data[i+1][j]=temp;
temp = data[i][0]; data[i][0]=data[i+1][0]; data[i+1][0] = temp;
is_sorted = false; }
} if(is_sorted) break; } for(i=0;i<n;++i){ for (j =0 ; j < 2; ++j){ std::cout<<data[i][j]<<' '; } std::cout<<'\n'; }
return 0; } what's the differance between sort and stable_sort in C++? Edited by author 21.07.2013 20:38 Edited by author 21.07.2013 20:39 sort() is a realization of Quick Sort, stable_sort() is Merge Sort. del Edited by author 06.01.2015 21:38 if 2 - try 2 4 0 3 2 if 10-11 - just not use sort. especially bubble one))) I have 2 and this test works correct Edited by author 05.12.2014 13:00 #include <stdio.h> int main() { long int n, id[150000], m[150000], max, x, i, j; while(scanf("%ld", &n)==1) { for(i=0; i<n; i++) { scanf("%ld %ld", &id[i], &m[i]); } for(i=0; i<n; i++) { max=0; for(j=0; j<n; j++) { if(m[j]>max) { max=m[j]; x=j; } } printf("%ld %ld\n", id[x], max); m[x]=0; } } return 0; } import java.util.Scanner; /** * Created by Coder on 25.11.2014. */ public class FinalStandings1100_ { public static void main(String[] args) { Scanner x = new Scanner(System.in); int n = x.nextInt(); int a[][] = new int[n][2]; for (int i = 0; i < n; i++) { for (int j = 0; j < 2; j++) { a[i][j] = x.nextInt(); } } int k = 1; int d = 0 for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { if (a[j][k] < a[j + 1][k]) { int v = a[j][d]; a[j][d] = a[j + 1][d]; a[j + 1][d] = v; int c = a[j][k]; a[j][k] = a[j + 1][k]; a[j + 1][k] = c; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < 2; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } } } #include <stdio.h> #include <stdlib.h> struct Team { int id; int m; }; int main() { int n; struct Team * res;
scanf ("%d", &n); res = (struct Team*)malloc( n * sizeof(struct Team) );
for (int i = 0; i < n; i++) scanf ("%d %d", &res[i].id, &res[i].m); struct Team tmp; for (int i = 0; i < n-1; i++) for (int j = 0; j < n-1; j++) if (res[j].m < res[ j+1 ].m) { tmp = res[j]; res[j] = res[ j+1 ]; res[ j+1 ] = tmp; } for (int i = 0; i < n; i++) printf ("%d %d\n", res[i].id, res[i].m); return 0; } Same code get stuck at test #18 due to TLE, then at #17, but got accepted when I submit it again. What magic is this :) import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; import java.io.BufferedReader; import java.io.InputStreamReader; public class hgg { /** * @param args */ public static ArrayList<concho> listcho = new ArrayList<concho>(); public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int a = sc.nextInt(); for (int i = 0; i < a; i++) { listcho.add(new concho(sc.nextInt(),sc.nextInt())); } Collections.sort(listcho, new Comparator<concho>() { public int compare(concho o1, concho o2) { // TODO Auto-generated method stub if(o1.kg<o2.kg) return 1; if(o1.kg>o2.kg) return -1; return 0; } }); for (concho item : listcho) { System.out.println(item.tuoi+" "+item.kg); } } public static class concho { int tuoi; int kg; public concho(int t, int k) { tuoi=t; kg=k; } } } Edited by author 05.06.2014 17:45 Edited by author 05.06.2014 18:23 Edited by author 05.06.2014 18:41 Edited by author 05.06.2014 18:53 As the title says, I get time limit exceeded error with 0.375 seconds execution time. There is something definitely wrong here. My solution works just fine, the problem isn't that difficult. But on test 11, I time out for unknown reason. My C++ solution: #include <iostream> using namespace std; struct team { int id; int n; }; int main() { register int temp, temp_2; team *com; team temp_team; int i; cin >> i; com = new team[i]; for ( temp = 0; temp < i; temp++ ) { cin >> com[temp].id >> com[temp].n; } for ( temp = 0; temp < i; temp++ ) for ( temp_2 = 0; temp_2 < i; temp_2++ ) { if ( com[temp].n > com[temp_2].n ) { temp_com = com[temp]; com[temp] = com[temp_2]; com[temp_2] = temp_com; } } for ( temp = 0; temp < i; temp++ ) { cout << com[temp].id << " " << com[temp].n << endl; } return 0; } |
|