1. 程式人生 > 其它 >靈動ICPC冬令營基礎-5

靈動ICPC冬令營基礎-5

A - Brainman

計算一組數的計算逆序對數,可以直接暴力求解。

#include<cstdio>
#include<cstdio>
#include<cstring>
int a[10001];
int main(){
	int n;
	scanf("%d",&n);
	int t,t1=1;
	while(n--)
	{
		int c=0;
		scanf("%d",&t);
		for(int i=1;i<=t;++i)
		{
			scanf("%d",&a[
i]); } for(int j=1;j<=t;++j) { for(int k=j;k<=t;++k) if(a[j]>a[k]) ++c; } printf("Scenario #%d:\n%d\n\n",t1++,c); } return 0; }

B - Ultra-QuickSort

用遞迴計算次數,要是暴力算可能會超時。

#include<cstdio>
using namespace std;
long long a[500001],b[500001];
long long ans=0;
void merge
(int low,int mid,int high) { int i=low,j=mid+1,k=low; while(i<=mid&&j<=high) { if(a[i]<=a[j]) { b[k++]=a[i++]; } else { ans+=j-k; b[k++]=a[j++]; } } while (i<=mid) b[k++]=a[i++]; while (j<=high) b[k++]=a[j++]; for (i=low;i<=high;++i) { a[i]=
b[i]; } } void mergeSort(int a, int b) { if (a<b) { int mid=(a+b)/2; mergeSort(a,mid); mergeSort(mid+1,b); merge(a,mid,b); } } int main(){ int n; while (scanf("%d",&n)!=EOF&&n>0) { ans=0; for(int i=1;i<=n;++i) { scanf("%lld",&a[i]); } mergeSort(1,n); printf("%lld\n", ans); } return 0; }

C - Who’s in the Middle

用c++自帶的排序就行了

#include<stdio.h>
#include<algorithm>
#include<iostream>
int a[10001];
using namespace std;
int main()
{
	int n;
	while(~scanf("%d",&n))
	{	for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
			sort(a,a+n);
		printf("%d\n",a[n/2]);
	}
	return 0;
}

D - Word Amalgamation

設字典表示為字元陣列word。在字典被輸入後,字典word就被建立了。然後,對於每個在word中的單詞word [i],通過選擇排序,完成word的字典序排列。
• 接下來,依次輸入待處理的單詞,每輸入一個單詞,存入字串str,通過sort函式,對其按字元升序進行排序;然後和word中的單詞word[i]逐個比較:word[i]也通過sort函式按字元升序進行排序,如果兩者相等,則輸出word [i];比較結束時,沒有相等的情況,則輸出"NOT A VALID WORD"。
• 在參考程式中,字串比較函式strcmp按字典序比較兩個字串,並返回結果:如果兩個字串相等,則返回零;字元複製函式strcpy則是將源字串變數的內容複製到目標字串變數中

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    char words[101][10],str[10],str1[10];
    int i,j,length1,length2,s = 0;
    while(1){
        scanf("%s",words[s]);
        if(strcmp(words[s++],"XXXXXX") == 0) break;
    }
    for(i = 0;i < s - 2; i++)
        for(j = i + 1;j < s - 1; j++)
            if(strcmp(words[i],words[j]) > 0){
                strcpy(str,words[i]);
                strcpy(words[i],words[j]);
                strcpy(words[j],str);
            }
    while(scanf("%s",str) != EOF && strcmp(str,"XXXXXX") != 0)
	{ 
        int flag = 1;
        for(i = 0;i < s - 1; i++)
		{
            length1 = strlen(str);
            length2 = strlen(words[i]);
            strcpy(str1,words[i]);
            sort(str,str + length1);
            sort(str1,str1 + length2);
            if(strcmp(str1,str) == 0)
			{
                printf("%s\n",words[i]);
                flag = 0;
            }
        }
        if(flag)
            printf("NOT A VALID WORD\n");
        printf("******\n");
    }
    return 0;
}

E - 排名

設考生序列為stu,考生用結構表示,其中字元陣列name表示考號,num表示解決的題目總數,sum表示分數。
• 首先,在輸入每個考生資訊的時候,根據解題的題號,統計考生的分數。然後結構體排序,分數和考號分別為第一和二關鍵字。最後,按題目要求輸出不低於分數線的考生人數,以及分數線上的考生資訊。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int a[11];
struct stu{
	char name[21];
	int ts;
	int zf;
}s[1001];
int cmp(stu a,stu b)
{
	if(a.zf==b.zf)
		return strcmp(a.name,b.name)<0;
	return a.zf>b.zf;
}
int main()
{
	int n,m,g;
	while(scanf("%d",&n),n)
	{
		scanf("%d%d",&m,&g);
		int c=0;
		for(int i=1;i<=m;++i)
		scanf("%d",&a[i]);
		for(int i=1;i<=n;++i)
		{
			int sum=0;
			scanf("%s%d",&s[i].name,&s[i].ts);
			for(int j=1;j<=s[i].ts;++j)
			{
				int x;
				scanf("%d",&x);
				sum+=a[x];
			}
			s[i].zf=sum;
			if(s[i].zf>=g)  c++;
		}
		if(!c) printf("0\n");
		else 
		{
			printf("%d\n",c);
			sort(s+1,s+n+1,cmp);
			for(int i=1;i<=c;i++)
				printf("%s %d\n",s[i].name,s[i].zf);

		}
	}
	return 0;
}

F - Election Time

• 用一個結構體陣列表示n頭競選總統的奶牛,在結構體中,給出一頭奶牛的第一輪預期得票,第二輪預期得票,以及這頭奶牛的編號。
• 求解本題,則要進行兩次排序。第一次,n頭奶牛第一輪預期得票排序;第二次,在第一次排序的前k頭奶牛的第二輪預期得票排序。最後,輸出第二輪中票數最多的牛的編號。

#include<cstdio>
#include<algorithm>
using namespace std;
struct na{
	int a;
	int b;
	int ans;
}niu[50001];
int cmp1(na cow1,na cow2)
{
    return cow1.a>cow2.a;
}

int cmp2(na cow1,na cow2)

{
    return cow1.b>cow2.b;
}
int main (){
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&niu[i].a,&niu[i].b);
		niu[i].ans=i;
	}
		 sort(niu+1,niu+n+1,cmp1);
         sort(niu+1,niu+k+1,cmp2);
         printf("%d\n",niu[1].ans);
	return 0;
}

G - Holiday Hotel

設賓館序列為h,賓館用結構表示,其中第i家賓館離海灘的距離為h[i].dist,住宿費用為h[i].cost。根據侯選賓館的需求,以離海灘距離為第1關鍵字、住宿費用為第2關鍵字,對結構陣列h進行排序。然後,在此基礎上計算侯選賓館的數目ans:
• 對於已經排序的結構陣列h,根據題意,如果賓館a的dist比賓館b小,那麼b的cost一定要比a的cost小,這樣b才能作為候選賓館,以此,依次掃描每家賓館:若當前賓館i雖然離海灘的距離不近但費用低,則賓館i進入候選序列,ans++。最後輸出侯選賓館的數目ans。

#include<cstdio>
#include<algorithm>
using namespace std;
struct h{
	int x,y;
}hh[10001];
int cmp(h a,h b) 
{
	if(a.x==b.x) return a.y<b.y;
	 return a.x<b.x;
}
int main()
{
	int n;
	while(~scanf("%d",&n)&&n)
	{
		for(int i=1;i<=n;++i)
			scanf("%d%d",&hh[i].x,&hh[i].y);
		sort(hh+1,hh+1+n,cmp);
		int a=hh[1].y, ans=1;
		for(int i=2;i<=n;i++)
		{
			if(hh[i].y<a)  a=hh[i].y,ans++;
		}
		printf("%d\n",ans);
	}
	
	return 0;
}