Ignatius and the Princess IV---hdu1029(动态规划或者sort)
2015-10-24 13:19
453 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029
就是给你n(n是奇数)个数找出个数大于(n+1)/ 2 的那个数;
n的取值范围是 n(1<=n<=999999)
这是很久之前做的一道题了,当时就是用sort排序,输出a[(n+1)/2]就行了,在这道题上很容易就过了,但是如果说n很大的大到无法开数组呢,所以有搜了一下别人的题解;
我们可以知道结果的那个数的个数一定大于总个数的一半(关键),所以我们可以线性的做,当cnt=0的时候更新ans;否则当当前数为ans的时候让cnt++,不相等时就让cnt--;
View Code
就是给你n(n是奇数)个数找出个数大于(n+1)/ 2 的那个数;
n的取值范围是 n(1<=n<=999999)
这是很久之前做的一道题了,当时就是用sort排序,输出a[(n+1)/2]就行了,在这道题上很容易就过了,但是如果说n很大的大到无法开数组呢,所以有搜了一下别人的题解;
我们可以知道结果的那个数的个数一定大于总个数的一半(关键),所以我们可以线性的做,当cnt=0的时候更新ans;否则当当前数为ans的时候让cnt++,不相等时就让cnt--;
#include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> #include<iostream> using namespace std; #define N 1000000 int a ; int main() { int n; while(scanf("%d", &n)!=EOF) { for(int i=0; i<n; i++) scanf("%d", &a[i]); sort(a, a+n); printf("%d\n", a[(n+1)/2]); } return 0; }
View Code
相关文章推荐
- qt中的拖拽及其使用技巧
- iOS ARC 内存管理要点
- Android XUtils的使用
- php array_multisort对数据库结果多个字段进行排序
- Uva 1589 - Xiangqi (模拟)
- android ListView几个比较特别的属性
- Android自定义view之弹出式dialog
- jQuery获取页面及个元素高度、宽度
- DP+滚动数组-HDU-1176-免费馅饼
- PIC单片机配置字
- 内网渗透的一些小总结(长期更新)
- linux c进程内存分配
- 面试的一些问题
- OCP解决问题053-16 MEMORY_TARGET
- Android消息机制深入了解
- POJ2299【树状数组】
- 【C语言】【笔试题】找出一组数据中成单出现的数字,其他所有数字都是成对出现的。(移位)
- android下的三种动画
- 调试临时日志输出
- Android 百度地图路径规划