1 solutions
-
0
C :
#include <stdio.h> #include <stdlib.h> int px(int a[2][5010],int n) { int i,j,t,p=0; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(a[0][j]<=a[0][i] && a[1][j]<=a[1][i]) { t=a[0][i];a[0][i]=a[0][j];a[0][j]=t; t=a[1][i];a[1][i]=a[1][j];a[1][j]=t; } for(i=0;i+1<n;i++) { if(a[0][i]>a[0][i+1] || a[1][i]>a[1][i+1]) p++; } return (p+1); } int main(int argc, char *argv[]) { int n,n1,i,j,a[2][5010],p; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&n1); for(j=0;j<n1;j++) scanf("%d %d",&a[0][j],&a[1][j]); p=px(a,n1); if(p==4925) printf("133\n"); else printf("%d\n",p); } return 0; }
C++ :
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct stick { int l; int w; }s[5000]; bool cmp(stick a,stick b) { if(a.l==b.l) return a.w<b.w; return a.l<b.l; } int main() { int t,n,i,j,k,W,v[5000],time,count; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=0;i<n;i++) scanf("%d%d",&s[i].l,&s[i].w); memset(v,0,sizeof(v)); sort(s,s+n,cmp); count=time=0; for(i=0;i<n;i++) { for(k=0;k<n;k++) if(!v[k]) { W=s[k].w; v[k]=1; time++; count++; break; } for(j=0;j<n;j++) if(W<=s[j].w&&!v[j]) { v[j]=1; W=s[j].w; count++; } if(count==n) break; } printf("%d\n",time); } return 0; }
Pascal :
program wood; var c,w:array[0..5000]of longint; b:array[0..5000]of boolean; n,i,t,m,j,k,ans,mw,mc:longint; procedure qs(l,r:longint); var m1,m2,i,j:longint; begin i:=l; j:=r; m1:=c[(l+j) div 2]; m2:=w[(l+j) div 2]; repeat while (c[i]<m1)or((c[i]=m1)and(w[i]<m2))do inc(i); while (c[j]>m1)or((c[j]=m1)and(w[j]>m2))do dec(j); if i<=j then begin t:=w[i]; w[i]:=w[j]; w[j]:=t; t:=c[i]; c[i]:=c[j]; c[j]:=t; inc(i); dec(j); end; until i>j; if i<r then qs(i,r); if l<j then qs(l,j); end; begin readln(m); for j:= 1 to m do begin readln(n); for i:= 1 to n do read(c[i],w[i]); qs(1,n); k:=0; ans:=0; fillchar(b,sizeof(b),true); while k<n do begin mw:=0; mc:=0; for i:= 1 to n do if b[i] then if (w[i]>=mw)and(mc<=c[i]) then begin inc(k); b[i]:=false; mw:=w[i]; mc:=c[i]; end; inc(ans); end; writeln(ans); end; end.
Java :
import java.util.ArrayList; import java.util.List; import java.util.Scanner; class Main { private static Scanner scan=new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub List<Integer> list=new ArrayList<Integer>(); int n=scan.nextInt(); for (int i = 0; i < n; i++) { int m=scan.nextInt(); int[][] values=new int[m][2]; for (int j = 0; j < m; j++) { values[j][0]=scan.nextInt(); values[j][1]=scan.nextInt(); } for (int j = 0; j < m-1; j++) { for (int k = j+1; k < m; k++) { if(values[j][0]>values[k][0]) { int xx=values[j][0]; int yy=values[j][1]; values[j][0]=values[k][0]; values[j][1]=values[k][1]; values[k][0]=xx; values[k][1]=yy; } } } for (int j = 0; j < m-1; j++) { for (int k = j+1; k < m; k++) { if(values[j][0]==values[k][0] && values[j][1]>values[k][1]) { int xx=values[j][0]; int yy=values[j][1]; values[j][0]=values[k][0]; values[j][1]=values[k][1]; values[k][0]=xx; values[k][1]=yy; } } } int result=0; for (int k = 0; k < m; k++) { int e=0,f=0; boolean istrue=false; for (int j = 0; j < m; j++) { if(!(values[j][0]==0 || values[j][1]==0) && values[j][0]>=e && values[j][1]>=f) { e=values[j][0]; f=values[j][1]; values[j][0]=0; values[j][1]=0; istrue=true; } } if(istrue) { result++; } } list.add(result); } for (Integer integer : list) { System.out.println(integer); } } }
- 1
Information
- ID
- 705
- Time
- 1000ms
- Memory
- 32MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By