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

蒟蒻的AC纪念

Posted by CrystalBall at 2016-10-05 23:43:35 on Problem 1007
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <algorithm>
using namespace std;

#define elif else if
const int maxlen = 50;
const int maxn = 100;

struct node{
	char s[maxlen+1];
	int con;
}dna[maxn+1];

void skip_white()
{
	int t;
	while ( (t=getchar()) != EOF && t==' ')
		;
}
int Merge(char a[], int lo, int mid, int hi)
{
	int count = 0;
	int i = lo;
	int j = mid+1;
	int k;
	char temp[maxlen+1];
	
	for (k = lo; k <= hi; k++)
		temp[k] = a[k];
	for (k = lo; k <= hi; k++)
	{
		if (i > mid)
			a[k] = temp[j++];
		elif (j > hi)
			a[k] = temp[i++];
		elif (temp[i] > temp[j])
			{
				count += mid - i +1;
				a[k] = temp[j++];
			}
		else 
			a[k] = temp[i++];
	}
	return count;
}

int Inversion(char a[], int n)
{
	int sz;
	int lo;
	int count = 0;
	for (sz = 1;sz < n; sz <<= 1)
		for (lo = 0; lo < n-sz ; lo += sz+sz)	
			count += Merge(a, lo, lo+sz-1, min(lo+sz+sz-1, n-1));
	return count;
}
bool _cmp(node a,node b)
{
	return a.con < b.con;
}
int main()
{	
	int i, j;
	int l, n;
	int t;
	char temp[maxlen+1];

	scanf("%d%d",&l,&n);
	skip_white();
	for(i = 1;i <= n; i++)
	{
		for(j = 0; j < l; j++)
		{
			scanf("%c",&temp[j]);
			dna[i].s[j] = temp[j];
		}
		dna[i].con = Inversion(temp, l);
		skip_white();
	}
	for (int kk=1;kk<=n;kk++)
		printf("\n%d   %s\n",dna[kk].con,dna[kk].s);
	sort(dna+1,dna+n+1,_cmp);
	for (i = 1; i <= n; i++)
	{
		printf("%s----%d\n",dna[i].s,dna[i].con);
	}
	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