码迷,mamicode.com
首页 > 编程语言 > 详细

CF922D Robot Vacuum Cleaner 贪心+排序

时间:2019-10-01 10:02:33      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:highlight   ret   ++i   scanf   put   turn   har   space   bool   

正确的贪心方法:按照比例排序. 

code: 

#include <bits/stdc++.h>  
#define N 200000 
#define ll long long 
#define setIO(s) freopen(s".in","r",stdin) 
using namespace std; 
int n;  
char str[N],S[N];   
struct Node 
{
    int l,r; 
    double perc; 
}t[N]; 
bool cmp(Node a,Node b) 
{
    return a.perc>b.perc; 
}
int main() 
{
    int i,j,cnt=0; 
    // msetIO("input"); 
    scanf("%d",&n);          
    for(i=1;i<=n;++i) 
    {
        scanf("%s",str+1);             
        int len=strlen(str+1),s=0; 
        t[i].l=cnt+1; 
        for(j=1;j<=len;++j) ++cnt, S[cnt]=str[j],s+=(str[j]==‘s‘);              
        t[i].r=cnt; 
        t[i].perc=(double)s/len;            
    }   
    ll pre=0,ans=0; 
    sort(t+1,t+1+n,cmp); 
    for(i=1;i<=n;++i) 
    {
        int l=t[i].l; 
        int r=t[i].r;  
        for(j=l;j<=r;++j) 
        {
            if(S[j]==‘s‘) 
            {
                ++pre; 
            } 
            else 
            {
                ans+=pre;   
            }
        }
    }
    printf("%lld\n",ans); 
    return 0; 
}

  

CF922D Robot Vacuum Cleaner 贪心+排序

标签:highlight   ret   ++i   scanf   put   turn   har   space   bool   

原文地址:https://www.cnblogs.com/guangheli/p/11614789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!