CodeForces - 922D 点击打开链接
思路:s/h越大的越往前排,这样整体的字符串才是s/h值由大变小。
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX=100005;
struct T
{
int sNum;
int hNum;
float sDh;
int sh;
}tManager[MAX];
bool cmp(T a,T b)
{
return a.sDh>b.sDh;
}
int main()
{
int total,id=0;
char str[MAX];
cin>>total;
while(id<total)
{
cin>>str;
int i=0;
while(str[i]!='\0')
{
if(str[i]=='s')
tManager[id].sNum++;
else if(str[i]=='h')
{
tManager[id].hNum++;
tManager[id].sh+=tManager[id].sNum;
}
i++;
}
if(!tManager[id].hNum)
{
tManager[id].sDh=MAX;//s的数量与h的数量的比值最大有可能为99999;
//所以用MAX就足够了
}
else
tManager[id].sDh=(float)tManager[id].sNum/tManager[id].hNum;
id++;
}
sort(tManager,tManager+id,cmp);
long long ans=0,sCot=0;//ans最大有可能是5w*5w,所以要用long long
for(int i=0;i<id;i++)
{
// cout<<"NO."<<i<<":"<<endl;
// cout<<tManager[i].sNum<<endl;
// cout<<tManager[i].hNum<<endl;
// cout<<tManager[i].sDh<<endl;
// cout<<tManager[i].sh<<endl;
// cout<<"---------------"<<endl;
ans+=sCot*tManager[i].hNum;
ans+=tManager[i].sh;
sCot+=tManager[i].sNum;//这句要放在最下面。。。。
}
cout<<ans<<endl;
}
一发RuntimeError:MAX小了,数组越界;
二发Runtime:没有判断h的数量为0的情况,除以0了
三发WrongAnswer:几个循环的终止条件没有理清,有无等号的错误
四发WrongAnswer:ans没有用long long溢出了