您的位置:首页 > 其它

Amr and The Large Array

2015-07-18 23:41 330 查看
题意:

这道题的题意有点扯,He wants to choose the smallest subsegment of this array such that the beauty of it will be the same as the original
array.这句话到现在还没理解,太扯了。提交在第二个测试数据的时候错了(刚开始的时候我还以为对了),我才意识到时自己的sort函数中的cmp写错了,改了一下,就过了,坑爹。这道题就是给你一个数组,统计这个数组中的数字出现、次数最多的那个数字的所在数组中的哪一段的子数组中,如果存在出现次数一样多的数字,要求该子数组段的包含的的数字个数最少,若还有一样多的数字个数,要求该段的开始下标最靠左。

分析:

建立一个数据结构,包括数字a出现的次数,该数字a在数字段的开始下标,结束下标,然后排序即可;

代码如下:

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

#define N 1000010

struct apple{
int num;
int sta;
int end;
}app
;

int cmp(struct apple a,struct apple b) {
if(a.num>b.num) return 1;
else if(a.num==b.num&&(a.end-a.sta)<(b.end-b.sta)) return 1;
else if(a.num==b.num&&(a.end-a.sta)==(b.end-b.sta)&&a.sta<b.sta) return 1;
return 0;
}

int main() {
int i,n,a;
memset(app,0,sizeof(app));
cin>>n;
for(i=0; i<n; i++) {
cin>>a;
if(app[a].num) {
app[a].end = i+1;
}
else {
app[a].sta=i+1;
app[a].end=i+1;
}
app[a].num++;
//cout<<a<<' '<<app[a].num<<endl;
}
sort(app,app+N,cmp);
//for(i=0; i<5; i++) cout<<app[i].num<<endl;
cout<<app[0].sta<<' '<<app[0].end<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: