标签:arr temp int scanf while 灵动 include 集训
集训第五天
A题 Brainman
思考过程
归并排序,主要是大化小,速度更快一些。
源代码
#include<stdio.h>
#include <limits.h>
void Merge(int r[], int temp[], int s, int m, int t);
void MergeSort(int r[], int temp[], int s, int t);
int r[1010],temp[1010];
int a=0;
int main()
{
int n;
scanf("%d",&n);
int N,i,j,s,t;
for(j=0;j<n;j++)
{
a=0;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d",&r[i]);
}
s=0;
t=N-1;
MergeSort(r,temp,s,t);
printf("Scenario #%d:\n%d\n\n",j+1,a);
}
return 0;
}
void MergeSort(int r[], int temp[], int s, int t)
{
if(s<t)
{
int m=(s+t)/2;
MergeSort(r, temp, s, m);
MergeSort(r, temp, m+1, t);
Merge(r, temp, s, m, t);
}
}
void Merge(int r[], int temp[], int s, int m, int t)
{
int i=s;
int j=m+1;
int k=i;
while(i<=m&&j<=t)
{
if(r[i]>r[j])
{
temp[k++]=r[j++];
a=a+m-i+1;
}
else
{
temp[k++]=r[i++];
}
}
while(i<=m)
temp[k++]=r[i++];
while(j<=t)
temp[k++]=r[j++];
for(int i = s; i <= t; ++i)
r[i] = temp[i];
}
遇到问题
归并排序比较难懂,但是思考一下还是能看懂的。
B题 Ultra-QuickSort
思考过程
和上一题差不多,但数据太多,容易超时,需要把O(n^2)的归并转化成O(nlog2n)的归并。
源代码
#include <stdio.h>
#define N 600000
int a[N];
int temp[600000];
long long w;
void Mergearray(int a[],int left,int mid,int right)
{
int x,y,z;
x=left,y=mid+1,z=0;
while(x<=mid&&y<=right)
{
if(a[x]<=a[y]) temp[z++]=a[x++];
else
{
w+=y-left-z;
temp[z++]=a[y++];}
}
while(x<=mid) temp[z++]=a[x++];
while(y<=right) temp[z++]=a[y++];
for(x=0;x<z;x++) a[left+x]=temp[x];
}
void Merge(int a[],int left,int right)
{
int mid;
if(left<right)
{
mid=(left+right)/2;
Merge(a,left,mid);
Merge(a,mid+1,right);
Mergearray(a,left,mid,right);
}
}
int main()
{
int n,i;
while(scanf("%d",&n)&&n)
{
w=0;
for(i=0;i<n;i++) scanf("%d",&a[i]);
Merge(a,0,n-1);
printf("%lld\n",w);
}
}
遇到问题
总是超时。
C题 Who’s in the Middle
思考过程
是之前做过的题,用快速排序更快一点。
源代码
#include<stdio.h>
void Quick_Sort(int arr[], int begin, int end);
int main()
{
int n,i,j,a[10001],temp,flag;
temp=0;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
Quick_Sort(a,0,n-1);
printf("%d\n",a[n/2]);
}
return 0;
}
void Quick_Sort(int arr[], int begin, int end)
{
if(begin > end)
return;
int tmp = arr[begin];
int i = begin;
int j = end;
while(i != j)
{
while(arr[j] >= tmp && j > i)
j--;
while(arr[i] <= tmp && j > i)
i++;
if(j > i)
{
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
arr[begin] = arr[i];
arr[i] = tmp;
Quick_Sort(arr, begin, i-1);
Quick_Sort(arr, i+1, end);
}
遇到问题
无。
D题 Word Amalgamation
思考过程
有点难,主要是字符串比较与排序,对输出格式要求较高。
源代码
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string word, temp;
map<string, set<string> > dicMap;
#ifndef ONLINE_JUDGE
ifstream cin("d:\\OJ\\uva_in.txt");
#endif
while (cin >> word) {
if (word == "XXXXXX")
break;
temp = word;
sort(temp.begin(), temp.end());
dicMap[temp].insert(word);
}
while (cin >> word) {
if (word == "XXXXXX") break;
sort(word.begin(), word.end());
if (dicMap.count(word)) {
set<string> wordSet = dicMap[word];
for (set<string>::const_iterator it = wordSet.begin();
it != wordSet.end(); it++) {
cout << *it << endl;
}
} else {
printf("NOT A VALID WORD\n" );
}
printf("******\n");
}
return 0;
}
遇到问题
突然从数的比较转到字符串的比较,思维扭转不过来,但多做做这类题应该会好一些。
E题 排名
思考过程
输入比较难,反而是排序方面用C++的sort函数就可以了。
源代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
int a[11];
struct zz
{
char xh[22];
int ts;
int zf;
}q[2010];
int cmp(zz a,zz b)
{
if(a.zf==b.zf)
return strcmp(a.xh,b.xh)<0;
return a.zf>b.zf;
}
int main()
{
int n,m,fz,x;
int i,j,k;
int sum;
while(scanf("%d",&n),n)
{
scanf("%d%d",&m,&fz);
int cnt=0;
for(i=1;i<=m;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
{
sum=0;
scanf("%s%d",q[i].xh,&q[i].ts);
for(j=1;j<=q[i].ts;j++)
{
scanf("%d",&x);
sum+=a[x];
}
q[i].zf=sum;
if(q[i].zf>=fz)
cnt++;
}
if(cnt==0)
printf("0\n");
else
{
printf("%d\n",cnt);
sort(q+1,q+n+1,cmp);
for(i=1;i<=cnt;i++)
printf("%s %d\n",q[i].xh,q[i].zf);
}
}
return 0;
}
遇到问题
C++不是太熟,运用不起来。
F题 Election Time
思考过程
不难,用两个sort函数就行了。
源代码
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct nn{
int first;
int second;
int index;
};
typedef struct nn cow;
cow cows[50005];
bool cmp1(cow o1, cow o2){
return (o1.first > o2.first);
}
bool cmp2(cow o1, cow o2){
return (o1.second > o2.second);
}
int main(){
int n,k;
scanf("%d %d",&n,&k);
for(int i = 1;i<=n;i++){
scanf("%d %d",&cows[i].first,&cows[i].second);
cows[i].index = i;
}
sort(cows+1,cows+n+1,cmp1);
sort(cows+1,cows+k+1,cmp2);
cout<<cows[1].index<<endl;
return 0;
}
遇到问题
无。
G题 Holiday Hotel
思考过程
需要从侧面思考,如果一遍遍循环的话会超时,可以从侧面想,先按距离排序,再比较相邻两个宾馆的价钱,然后就可以选出合适的宾馆了。
源代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct edge
{
int dis,cost;
}map[10001];
bool cmp(edge x,edge y)
{
if(x.dis == y.dis)
{
return x.cost < y.cost;
}
return x.dis < y.dis;
}
int main()
{
int n,i,ans,count;
while(scanf("%d",&n)&&n)
{
ans=10001;
count=0;
for(i=0;i<n;i++)
scanf("%d%d",&map[i].dis,&map[i].cost);
sort(map,map+n,cmp);
for(i=0;i<n;i++)
{
if(map[i].cost<ans)
{
count++;
ans=map[i].cost;
}
}
printf("%d\n",count);
}
return 0;
}
遇到问题
不太能想到从侧面思考。
小结
1.归并排序以及时间复杂度。
2.快速排序。
3.C++中的sort函数。
4.结构体排序。
标签:arr,temp,int,scanf,while,灵动,include,集训 来源: https://blog.csdn.net/weixin_51568638/article/details/113041703
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。