题目
数据
2 2
1 1
2
3 2
2 2 2
2+2/3
8 8
3 2 1 3 3 4 4 5
4+1/7
8 7
3 2 1 3 3 4 4 5
3+4/5
思路
卡了一个点,忘记输出带分数的时候整数为0的情况是不用输出0和+号的,不然这次测试就满分了。。还是脑子不太灵光
首先记录各个位置的海拔
然后用数组al来记录 海拔 == i(i ∈ 【0,max】)的情况
不难发现,后一个数加上前一个数就是每增加一个单位的高度需要填满的“沟壑数”
最后需要填满的数量单独进行GCD运算即可
代码
#include <iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
#include<stdlib.h>
#include<cstdio>
#define ll long long
using namespace std;
void gcd(int rain,int dif,int fina)
{
int a = rain;
int b = dif;
if(!rain)
return;
else
{
int temp;
while(b % a)
{
temp = b % a;
b = a;
a = temp;
}
rain /= a;
dif /= a;
cout << (fina?"+":"") << rain << "/" << dif;
}
}
int main()
{
int n,s;
cin >> n >> s;
ll a[1020];
for(int i = 0; i < 1020;i++)a[i]=0;
for(int i = 0; i < n; i++)
cin >> a[i];
long long low = 99999999999LL,high = 0;
for(int i = 0; i < n; i++)
{
if(low > a[i]) low = a[i];
if(high < a[i]) high = a[i];
}
int *al = new int[high+10];
for(int i = 0; i < high + 10; i++)
al[i] = 0;
for(int i = 0; i < n; i++)
{
al[a[i]] += 1;
}
for(int i = 1; i <= high; i++ )
{
al[i] +=al[i-1];
}
//cout << al[high-1] << endl;
int loc = 0;
while(loc < high && al[loc] == 0)
loc++;
int dif = al[loc];
int rain = s;
int fina = low;
while(rain >= dif)
{
rain -= dif;
fina++;
if(loc < high)loc++;
dif = al[loc];
}
if(fina)
cout << fina;
//cout <<endl << rain << endl << dif << endl;
gcd(rain,dif,fina);
return 0;
}