soj 4389模拟
来源:互联网 发布:php把字符串变成数组 编辑:程序博客网 时间:2024/06/06 14:13
背景:周赛A题,
学习:1.开始没有注意到题目中对于帖子数大于1/2的描述,纯暴力计数,各种超!
2.后来发现按顺序扫描每一个数看是否有它在数组中的数量大于1/2,是则找到。我的代码:
#include<stdio.h>#include<string.h>int str[10000000];int main(void){int n;scanf("%d",&n);while(n--){int m,cout=0;scanf("%d",&m);for(int i=0;i<m;i++){scanf("%d",&str[i]);}for(int i=0;i<m;i++){for(int j=0;j<m;j++){if(str[j]!=-1){ if(str[j]==str[i]) { cout++; if(cout>=m/2+1) {printf("%d\n",str[i]);goto l1; } }}}str[i]=-1;cout=0;}l1: cout=0;}return 0;}
3.这样能过但是开了巨大的内存(oj极限是10的8次方个int?)
解题报告给出了比较先进的方法:假定第一个数是并计数count为1,以后遇见和它相同的就count自增一,当count为0时,可以就变为当前读入的数,最后剩下的可以必然是要找的大于1/2的数。给出代码:
#include<stdio.h>#include<string.h>int str[10000000];int main(void){int n;scanf("%d",&n);while(n--){ int m,count=0,x,key=-1;scanf("%d",&m);while(m--){scanf("%d",&x);if(x!=key) count--;else count++;if(count<0) {key=x;count=0;}}printf("%d\n",key);} return 0;}
0 0
- soj 4389模拟
- soj 4393模拟
- SicilyOJ(SOJ) 1128 DICE(模拟)
- SOJ 4445 2015四川省赛模拟题
- SOJ--4389: 川大贴吧水王
- soj
- SOJ-4072(数组模拟双向链表)
- soj 2013 weekly-3 暨校赛模拟I 7763-7770
- soj 2013 weekly-4 暨校赛模拟II 7904-7911
- soj 2013 weekly-5 暨校赛模拟III 7966-7974
- soj 4521 -0你电脑炸啦 topo排序,模拟
- soj 1814
- SOJ-1012
- SOJ-1013
- SOJ-1015
- SOJ-1016
- SOJ-3327
- soj 3109
- Linux网络配置
- wikioi 1545 最简单的排序
- qt动态库与静态库编译、应用以及pri文件创建
- 深入探索透视投影变换
- UNIX环境高级编程-第9章- 进程关系
- soj 4389模拟
- jython语法
- android 事件分发
- 密码生成器
- 第17周项目6-学生成绩统计(从文件中读入)
- 获取Spring ApplicationContext 几种方法
- View中的draw和onDraw的区别
- ERLANG 语法基础
- Exercise:Self-Taught Learning 代码示例