Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:为何我不能AC。。求助

Posted by towelie at 2014-01-20 08:10:55 on Problem 1007
In Reply To:为何我不能AC。。求助 Posted by:hbhb41307 at 2014-01-19 20:10:05
there's a O(n*log(n)) divide-and-conquer method for counting the # of inversion(s) :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LEN 50
#define MAX_NUM_STR 100

struct dna {
	size_t idx;
	size_t num_inv;
	char str[MAX_LEN + 1];
};

int comp(const void *p1, const void *p2) {
	return ((*(struct dna **)p1) -> num_inv < (*(struct dna **)p2) -> num_inv || ((*(struct dna **)p1) -> num_inv == (*(struct dna **)p2) -> num_inv && (*(struct dna **)p1) -> idx < (*(struct dna **)p2) -> idx)) ? -1 : 1;
}

size_t count_num_inv(const size_t start, const size_t end, char str[], char tmp[]) {
	if (start == end) {
		return 0;
	}
	size_t idx, l_idx, r_idx, num_inv;
	size_t mid = (start + end) >> 1;
	num_inv = count_num_inv(start, mid, str, tmp) + count_num_inv(mid + 1, end, str, tmp);	
	for (idx = start; idx <= end; ++idx) {
		tmp[idx] = str[idx];
	}
	idx = start;
	l_idx = start;
	r_idx = mid + 1;
	while (l_idx <= mid && r_idx <= end) {
		if (tmp[r_idx] < tmp[l_idx]) {
			str[idx++] = tmp[r_idx++];
			num_inv += mid + 1 - l_idx;
		}else {
			str[idx++] = tmp[l_idx++];
		}
	}
	while (l_idx <= mid) {
		str[idx++] = tmp[l_idx++];
	}
	while (r_idx <= end) {
		str[idx++] = tmp[r_idx++];
	}
	return num_inv;
}

int main(int argc, char *argv[]) {
	unsigned int n, N;
	size_t length;
	char str[MAX_LEN + 1], tmp[MAX_LEN + 1];
	struct dna dna_ent[MAX_NUM_STR], *dna_ptr[MAX_NUM_STR];
	/*

	some test data for counting # of inversion(s) 

	strcpy(str, "ZWQM");
	printf("%lu\n", count_num_inv(0, 3, str, tmp));  
	printf("%s\n", str);
	strcpy(str, "AACEDGG");
	printf("%lu\n", count_num_inv(0, 6, str, tmp));  
	printf("%s\n", str);
	strcpy(str, "DAABEC");
	printf("%lu\n", count_num_inv(0, 5, str, tmp));  
	printf("%s\n", str);
	*/
	scanf("%lu", &length);
	scanf("%u", &N);
	for (n = 0; n < N; ++n) {
		dna_ent[n].idx = n;
		scanf("%s", str);
		strcpy(dna_ent[n].str, str);
		dna_ent[n].num_inv = count_num_inv(0, length - 1, str, tmp);
		dna_ptr[n] = &(dna_ent[n]);
	}
	qsort(dna_ptr, N, sizeof(struct dna *), comp);
	for (n = 0; n < N; ++n) {
		printf("%s\n", dna_ptr[n] -> str);
	}
	return 0;
}


> #include<stdio.h>
> #include<stdlib.h>
> int main()
> {
> 	char a[101][51];
> 	int i,j,n,m,k,max=0,x;
> 	scanf("%d%d",&i,&j);
> 	int b[j];
> 	fflush(stdin);
> 	for(m=0;m<j;m++)
> 	{
>         fflush(stdin);
> 		for(n=0;n<i;n++)
> 		{
> 		    scanf("%c",&a[m][n]);
> 		}
> 		b[m]=0;
> 	}
> 	
> 	for(m=0;m<j;m++)
> 	{
> 		x=0;
> 		for(n=0;n<i-1;n++)
> 		{
> 			for(k=n+1;k<i;k++)
> 			{
> 				
> 				if(a[m][n]>a[m][k])
> 	            {
>             		x++;
>             	}
> 	            if(max<x)
> 	            {
>             		max=x;
>             	}
> 			}
> 		}
> 		b[m]=x;
> 	}
> 	for(k=0;k<=max;k++)
> 	{
> 		for(m=0;m<j;m++)
> 		{
> 			if(b[m]==k)
> 			{
> 				for(n=0;n<i;n++)
> 				{
> 					printf("%c",a[m][n]);
> 				}
> 				printf("\n");
> 			}
> 		}
> 	}
> 	return 0;
> }

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator