ZOJ:3365 Integer Numbers
来源:互联网 发布:cad高达数据 编辑:程序博客网 时间:2024/06/07 03:31
比赛的时候没能出来,其实离AC的思路也不太远了,但是没做出来就是没做出来。。
思路:如果元素a[i],a[j]满足a[i] - i = a[j] - j,则a[i]和a[j]要么一同修改要么都不需修改。记录a[i]-i的最大出现次数cnt,cnt就是不需改变的数的最大个数,n-cnt就是需要改变的数的最少个数。
#include <cstdio>#include <cmath>#include <algorithm>#include <iostream>#include <map>#include <cstring>using namespace std;int a[50005];map<int,int> p;int main(){ int n; while(scanf("%d",&n)!=EOF) { memset(a,0,sizeof(a)); p.clear(); for(int i=1; i<=n; ++i) scanf("%d",&a[i]); int ansx=0,anst; for(int i=1; i<=n; ++i) { p[a[i]-i]++; if(p[a[i]-i]>ansx) { ansx=p[a[i]-i]; anst=i; } } int ft=a[anst]+1-anst,cou=0; for(int i=1; i<=n; ++i,++ft) if(ft!=a[i]) { a[i]=ft; cou++; } printf("%d\n",cou); for(int i=1; i<=n; ++i) if(i==1)printf("%d",a[i]); else printf(" %d",a[i]); printf("\n"); } return 0;}
首先要从数据量上入手,考虑线性时间的算法。
判断这个位置是否需要改变要看a[i]-a[j]==i-j; 实际上这个不是一个固定的值,但是变形之后a[i]-i==a[j]-j的值,对于每个与它同时改变的值来说都是相同的。。
- Integer Numbers zoj 3365
- ZOJ:3365 Integer Numbers
- ZOJ 3365 Integer Numbers 思路题
- hust-Integer Numbers
- ZOJ 1292 Integer Inquiry
- ZOJ 1292 Integer Inquiry
- zoj 1292 Integer Inquiry
- ZOJ 1292 Integer Inquiry
- ZOJ 1292 Integer Inquiry
- zoj 1292 Integer Inquiry
- zoj 1095 Humble Numbers
- zoj 1095 Humble Numbers
- ZOJ 1095 Humble Numbers
- ZOJ 1095 Humble Numbers
- zoj 1180 Self Numbers
- ZOJ 2000 Palindrome Numbers
- ZOJ 1154 Niven Numbers
- zoj 1133 Smith Numbers
- 利用ffmpeg做视频解码的顺序
- 问:当前计算机系统一般会采用层次结构存储数据,请介绍下典型计算机存储系统一般分为哪几个层次,为什么采用分层存储数据能有效提高程序的执行效率?
- 运算符重载
- 关于C++和C中字符串的几点说明
- 使用clone解决hibernate+spring集成中的延迟加载问题及分析(no session or session was closed)
- ZOJ:3365 Integer Numbers
- COJ 1223 JAVA入门训练modpow
- 腾讯14研发笔试回忆
- NYOJ 题目854 留恋
- JavaScript对象和数组
- 继承与派生
- android组件之Spinner
- java方法中的参数用final来修饰的原因
- 有关cocos2d学习视频很不错