hust-Integer Numbers
来源:互联网 发布:linux more查找字符串 编辑:程序博客网 时间:2024/05/20 05:55
hust-Integer Numbers解题报告
大致题意:给一串数字,要求不改变这些数字的顺序,只改变其中一些数字的值,使这一串数字变为一个连续的数字。求出最小的改变方案。
做题思路:
给的数字啊a[n],每一个都减去自己的序号数,得到一串数字b[n]。例如:5 4 5 2 1 8减完之后就得到:5 3 3 -1 -3 3;然后再对b[n]排序,之后找出b[n]中出现次数最多的数字,将其作为temp.并且记录temp出现的次数,记为max.然后以temp作为标准,再一次加上序号,就可以得到答案。如:5 4 5 2 1 8其temp= 3,max=3.所以最终结果就是3 4 5 6 7 8.更改的数字个数为n-max=6-3=3.
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#define maxn 50010using namespace std;bool cmp(int x,int y){ if(x<y) return true; return false;}int main(){ int n; while(~scanf("%d",&n)) { int a[maxn],b[maxn]; int i,j; for(i=0;i<n;i++) { scanf("%d",&a[i]); b[i]=a[i]-i; } sort(b,b+n,cmp);//对b[n]进行排序 int temp,max=1,count=1; temp=b[0];//找出temp与max for(i=0;i<n-1;i++) { if(b[i]==b[i+1]) count++; else { if(count>max) { max=count; temp=b[i]; } count=1; } } if(count>max) {max=count;temp=b[i];} for(i=0;i<n;i++)//依次用temp加上序号 a[i]=i+temp; printf("%d\n",n-max); for(i=0;i<n-1;i++) printf("%d ",a[i]); printf("%d\n",a[i]); } return 0;}
- hust-Integer Numbers
- hust 1003 - Sibonacci Numbers
- HUST 1600 Lucky Numbers
- HUST 1600 Lucky Numbers
- HUST-1600 - Lucky Numbers 深搜
- Cubic-free numbers II HUST
- hust Integer Inquiry(字符串,大数相加)
- HUST 1003 Sibonacci Numbers(杂题)
- HUST Sum of Consecutive Prime Numbers
- Hust oj 1720 Fibonacci Numbers(水题)
- Integer Numbers zoj 3365
- ZOJ:3365 Integer Numbers
- HUST
- HUST
- HUST
- HUST
- HUST 1214 Cubic-free numbers II (容斥原理)
- HUST 1214 Cubic-free numbers II 容斥原理
- iOS:NSCoder的方法decodeRectForKey:
- 智能手机、平板电脑与GPS定位精度
- Bloom Filter算法
- iOS:NSCoder的方法decodeSize
- Python执行系统命令的方法
- hust-Integer Numbers
- linux命令学习笔记(2)——mkdir,rm,rmdir命令
- Android里merge和include标签的使用
- FusionCharts参数的详细说明
- VIM
- iOS:NSCoder的方法decodeSizeForKey:
- perl基础知识05【数据流控制】
- Win32学习笔记——进程
- 为什么对越亲的人越没耐心,越容易生气发火 (转)