Robot Vacuum Cleaner 贪心

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溢出了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值