很久以前写的,到现在才改对了--||。先排下序然后再一次次找,效率很一般!
Accepted | 1025 | C++ | 00:00.03 | 492K |
#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#define swap(x,y) if(x != y) {x = x^y; y = y^x; x = x^y;}
struct s { int l, w; bool b; };
void sort(struct s a[],int p,int r)
{
for(int i = p - 1; p < r; i = p - 1 )
{
for(int j = p; j < r; j++ )
if((a[j].l < a[r].l) || (a[j].l == a[r].l && a[j].w < a[r].w))
{
i++ ;
swap(a[i].l,a[j].l);
swap(a[i].w,a[j].w);
}
i++ ;
swap(a[i].l,a[r].l);
swap(a[i].w,a[r].w);
sort(a,p,i - 1 );
p = i + 1 ;
}
}
void solve()
{
int n;
scanf("%d",& n);
struct s *stick = (struct s*)malloc(n * sizeof(struct s));
memset(stick, 0, sizeof(stick[0]) * n);
int i, cnt = 0, res = 0 ;
for(i = 0; i < n; i++ )
scanf("%d%d", &stick[i].l, & stick[i].w);
sort(stick, 0, n - 1 );
for(int temp = -1; cnt < n; ++res, temp = -1 )
for(i = 0; i < n; i++ )
if(!stick[i].b && stick[i].w >= temp)
temp = stick[i].w, stick[i].b = true, cnt++ ;
free(stick);
printf("%d/n" ,res);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1025.txt","r" ,stdin);
#endif
int t;
while(scanf("%d", &t) != EOF)
while(t-- ) solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0 ;
}
#include <string.h>
#include <stdlib.h>
#define swap(x,y) if(x != y) {x = x^y; y = y^x; x = x^y;}
struct s { int l, w; bool b; };
void sort(struct s a[],int p,int r)
{
for(int i = p - 1; p < r; i = p - 1 )
{
for(int j = p; j < r; j++ )
if((a[j].l < a[r].l) || (a[j].l == a[r].l && a[j].w < a[r].w))
{
i++ ;
swap(a[i].l,a[j].l);
swap(a[i].w,a[j].w);
}
i++ ;
swap(a[i].l,a[r].l);
swap(a[i].w,a[r].w);
sort(a,p,i - 1 );
p = i + 1 ;
}
}
void solve()
{
int n;
scanf("%d",& n);
struct s *stick = (struct s*)malloc(n * sizeof(struct s));
memset(stick, 0, sizeof(stick[0]) * n);
int i, cnt = 0, res = 0 ;
for(i = 0; i < n; i++ )
scanf("%d%d", &stick[i].l, & stick[i].w);
sort(stick, 0, n - 1 );
for(int temp = -1; cnt < n; ++res, temp = -1 )
for(i = 0; i < n; i++ )
if(!stick[i].b && stick[i].w >= temp)
temp = stick[i].w, stick[i].b = true, cnt++ ;
free(stick);
printf("%d/n" ,res);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("1025.txt","r" ,stdin);
#endif
int t;
while(scanf("%d", &t) != EOF)
while(t-- ) solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0 ;
}