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在数字段的开始下标,结束下标,然后排序即可;
代码如下:
这道题的题意有点扯,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; }
相关文章推荐
- 硬件路由转发原理浅析
- [BZOJ2152]聪聪可可
- 配置DTcms伪静态功能图文教程
- PHP中如何通过Web执行C/C++应用程序?
- 命令模式 - 行为模式
- fork()+printf(/n)或printf()的区别?
- 2015071804 - 经历多了是否可以淡然面对呢?
- RTMP流媒体入门
- HDU 5280 Senior's Array
- 【理解】如何在内存中篡改数据
- plc和嵌入式的区别
- jq和thinkphp经常使用的几种ajax
- [BZOJ1187][HNOI2007]神奇游乐园
- poj 3084 dinic最大流
- 支持按行号区域文本选择的NotePad++插件开发
- 《STL源码剖析》空间配置器
- 2015071803 - 番石榴园下的喧嚣
- webqq协议请求交互过程
- 游戏编程与游戏种类
- Python应用phy模块生成html表格