zoukankan      html  css  js  c++  java
  • Zepto Code Rush 2014 A. Feed with Candy

    此题用贪心求解,

    首先将caramel drop类别的糖果按照高度从小到大排序,如果高度相同,按照重量从小到大排序

    fruit drop类别的糖果按照高度从小到大排序,如果高度相同,按照重量从小到大排序

    现在有两种可能

    第一种可能是第一个获得的糖果是caramel drop,

    则先搜索caramel drop类别的,然后找到高度小于x的最大高度的index,则在0~index索引之间的高度都小于x,则搜索0~index之间的mass最大的,这样之后高度变得最大,计数值加1,更新x

    在搜索fruit drop类别的,然后找到高度小于x的最大高度的index,则在0~index索引之间的高度都小于x,则搜索0~index之间的mass最大的,这样之后高度变得最大,计数值加1,更新x(跟caramel drop类别搜索的一样)

    第二种可能是第一个获得的糖果是fruit drop,其搜索过程是上面的两个过程反过来

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <cstring>
    
    using namespace std;
    
    struct  Fruit{
        int height;
        int mass;
        Fruit(int height_ = 0 , int mass_ = 0):height(height_),mass(mass_){}
        bool operator <(const Fruit& a) const {
            if(height != a.height) return height < a.height;
            else return mass < a.mass;
        }
    };
    
    int main(){
        int n,x;
        cin >> n>>x;
        vector<Fruit>  fruit[2];
        for(int i = 0 ; i < n; ++ i){
            int t,h,m;
            cin >> t >> h >> m;
            fruit[t].push_back(Fruit(h,m));
        }
    
        sort(fruit[0].begin(),fruit[0].end());
        sort(fruit[1].begin(),fruit[1].end());
        
        int  ans = 0;
        for(int type = 0 ; type < 2; ++ type ){
            vector<vector<bool> > visit(2);
            for(int i = 0 ; i < fruit[0].size(); ++ i) visit[0].push_back(false);
            for(int i = 0 ; i < fruit[1].size(); ++ i) visit[1].push_back(false);
            int res = 0,new_x = x;
            bool flag = true;
            while(flag){
                for(int k = 0; k < 2; ++ k){
                    int new_type = (type+k)%2, index = fruit[new_type].size()-1;
              //搜索高度小于new_x的最大高度
    for(;index>=0; --index){ if(!visit[new_type][index] && fruit[new_type][index].height <= new_x) break; } if(index < 0 ) {flag = false;break;}
              //在满足条件的高度中搜索质量最大的
    int maxMassIndex = index,maxMass = fruit[new_type][index].mass; for(int i = index -1; i >=0; -- i){ if(!visit[new_type][i] && fruit[new_type][i].mass > maxMass){ maxMass = fruit[new_type][i].mass ; maxMassIndex = i; } } index = maxMassIndex; visit[new_type][index] = true; //标识该糖果已被访问 new_x +=fruit[new_type][index].mass; //更新x res ++; } } ans = max(ans,res); } cout<<ans<<endl; }
  • 相关阅读:
    一个利用扩展方法的实例:AttachDataExtensions
    正则表达式语法
    正则表达式30分钟入门教程
    js正则验证两位小数 验证数字最简单正则表达式大全
    SQL Server DBA三十问【转】
    Vue(踩坑)vue.esm.js?efeb:628 [Vue warn]: Error in render: "TypeError: Cannot read property 'length' of undefined" found in
    vue(有必要做的项目优化)
    vue_(根据多种条件过滤评论内容)
    vue(ref父组件使用子组件中定义的方法)
    Vuex(实现加减操作,Vue.set解决自定义属性没有双向数据绑定)
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3788989.html
Copyright © 2011-2022 走看看