HDU 5742 It's All In The Mind(贪心)
来源:互联网 发布:mac mini 换ssd 编辑:程序博客网 时间:2024/05/22 03:18
Description
一个长度为n的序列,只知道其中m个点且知道这个序列满足一下三个性质
1.0<=ai<=100,i=1,2,…,n
2.a1>=a2>=a3>=…>=an
3.a1+a2+…+an!=0
求(a1+a2)/(a1+a2+…+an)的最大值
Input
第一行一整数T表示用例组数,每组用例首先输入两个整数n和m分别表示原序列长度和已知元素的数量,之后m行每行两个整数xi,yi表示a[xi]=yi
(2<=n<=100,0<=m<=n,1<=xi<=n,0<=yi<=100,xi严格递增,yi非严格递减)
Output
对于每组用例,输出(a1+a2)/(a1+a2+…+an)的最大值,化为最简分数形式
Sample Input
2
2 0
3 1
3 1
Sample Output
1/1
200/201
Solution
简单题,贪心的使a1和a2尽量大,使其他数尽量小
Code
#include<cstdio>#include<iostream>#include<cstring>using namespace std;#define maxn 111int T,n,m,a[maxn];int gcd(int a,int b){ return b?gcd(b,a%b):a;}int main(){ scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); memset(a,-1,sizeof(a)); while(m--) { int x,y; scanf("%d%d",&x,&y); a[x]=y; } if(a[1]==-1&&a[2]==-1)a[1]=a[2]=100; else if(a[1]==-1&&a[2]!=-1)a[1]=100; else if(a[1]!=-1&&a[2]==-1)a[2]=a[1]; for(int i=3;i<=n;i++) if(a[i]==-1) { int j=i; while(j<=n&&a[j]==-1)j++;; if(j==n+1)for(;i<j;i++)a[i]=0; else for(;i<j;i++)a[i]=a[j]; } int ans1=a[1]+a[2],ans2=0; for(int i=1;i<=n;i++)ans2+=a[i]; int g=gcd(ans1,ans2); ans1/=g,ans2/=g; printf("%d/%d\n",ans1,ans2); } return 0;}
0 0
- HDU 5742 It's All In The Mind(贪心?)
- HDU 5742 It's All In The Mind(贪心)
- 贪心-HDU-5742-It's All In The Mind
- HDU 5742 It's All In The Mind 贪心
- It's All In The Mind HDU
- hdu 5742 It's All In The Mind
- HDU 5742 It's All In The Mind (水题)
- hdu 5742 It's All In The Mind
- 【HDU】5742 It's All In The Mind
- hdu 5742 It's All In The Mind
- HDU 5742 It's All In The Mind
- HDU 5742 It's All In The Mind 水题
- 5742 ( It's All In The Mind )
- HDU5742 It's All In The Mind(贪心)
- HDU 5742 It's All In The Mind(水题【多校联合】)
- HDU 5742 It's All In The Mind(思维水)
- 2016 Multi-University Training Contest 2 1009 It's All In The Mind (贪心)
- 2016HDU多校联赛-HDU-5742-It's All In The Mind
- JQuery学习之旅(1)
- C语言关键字
- windows+xampp环境下升级mysql
- SSL常见加密算法
- onSaveInstanceState() 和 onRestoreInstanceState()
- HDU 5742 It's All In The Mind(贪心)
- Hadopp 入门 基本概念(一)
- 关于android串口通信
- 简单实现Android搜索功能 显示清除历史搜索记录
- Graylog——日志聚合工具的后起之秀
- Android学习第一天
- Matrix 和 ColorMatrix详细分析
- 86. Partition List
- spring定时器使用心得