Amr and The Large Array

题意:

这道题的题意有点扯,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[N];

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值