zoukankan      html  css  js  c++  java
  • code 4A-4D(greedy,hash,dp)

    4A简直逗..

    4B很简单的贪心,首先判断一下min的和是否大于sum或者max的和是否小于sum,如果是则不可能

    否则,首先先取每天已学习min的时间,最后一个个加到max,等于sum时退出。

    4C我nc,看到就以为要用字符串hash,照着写个BKDRHash,最后添加尾部的数字搞了半天,到了10000个字符串时就是过不去,想了下hash取模不能保证一定无冲突.

    后来看人题解map<string,int> int记录次数...简直太简单了..

    4D dp,绕了好久,后来想通,首先把不可能装礼物的盒子去掉再dp,这里要注意下,严格满足w1>w2&&h1>h2的,要保证1在2后面,不然dp会有问题.

    //4B
    
    #include<iostream>
    using namespace std;
    
    int main()
    {
        int time[40][2]={0};
        int avecnt[40]={0};
        int d,sumTime;
        cin>>d>>sumTime;
        int sum1=0,sum2=0;
        for(int i=0;i<d;i++)
        {
            cin>>time[i][0]>>time[i][1];
            sum1+=time[i][1];
            sum2+=time[i][0];
        }
        if(sum1<sumTime||sum2>sumTime)
        {
            cout<<"NO"<<endl;
            return 0;
        }
        sumTime-=sum2;
        int i=0;
        while(sumTime>0)
        {
            int t=time[i][1]-time[i][0];
            if(sumTime>t)
            {
                sumTime-=t;
                avecnt[i]=t;
            }
            else
            {
                
                avecnt[i]=sumTime;
                sumTime=0;
            }
            i++;
        }
    
        cout<<"YES"<<endl;
        for(int i=0;i<d;i++)
            cout<<time[i][0]+avecnt[i]<<" ";
        cout<<endl;
    
        return 0;
    }


    //4C
    #include<iostream>
    #include<string>
    #include<map>
    using namespace std;
    
    //unsigned int BKDRHash(char* str)
    //{
    //    unsigned int seed=131;//31 131 1313
    //    unsigned int hash=0;
    //    while(*str)
    //    {
    //        hash=hash*seed+(*str++);
    //    }
    //    return (hash&0x7FFFFFFF);
    //}
    //
    //bool Existed[1000003];
    //#define M 1000003 
    //int main()
    //{
    //    int n;
    //    char str[40]={0};
    //    cin>>n;
    //    for(int i=0;i<n;i++)
    //    {
    //        memset(str,0,sizeof(str));
    //        cin>>str;
    //        unsigned int hash=BKDRHash(str)%M;
    //        if(!Existed[hash])
    //        {
    //            cout<<"OK"<<endl;
    //            Existed[hash]=1;
    //        }
    //        else
    //        {
    //            int len=strlen(str);
    //            int s=1;
    //            str[len]='1';
    //            hash=BKDRHash(str)%M;
    //            while(Existed[hash])
    //            {
    //                int slen=0;
    //                s++;
    //                int t=s,t2=s;
    //                while(t>0)
    //                {
    //                    t/=10;
    //                    slen++;
    //                }
    //                for(int j=0;j<slen;j++)
    //                {
    //                    int m=t2%10;
    //                    str[len+slen-1-j]=m+'0';
    //                    t2/=10;
    //                }
    //                hash=BKDRHash(str)%M;
    //            }
    //            Existed[hash]=1;
    //            cout<<str<<endl;
    //        }
    //    }
    //    return 0;
    //}
    map<string,int>mp;
    int main()
    {
        string str;
        int n;
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>str;
            int p;
            p=mp[str]++;
            if(p==0)
                cout<<"OK"<<endl;
            else
                cout<<str<<p<<endl;
        }
        return 0;    
    }
    //4D
    
    #include<iostream>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    
    int main()
    {
        int n,w,h;
        int dp[5002]={0};
        int record[5002]={0};
        vector<pair<int,int>>vp,vp2;
        cin>>n>>w>>h;
        for(int i=0;i<n;i++)
        {
            int bw,bh;
            cin>>bw>>bh;
            vp.push_back(make_pair(bw,bh));
        }
        
        vp2=vp;
    
    
    
        for(vector<pair<int,int>>::iterator it=vp.begin();it!=vp.end();it++)
        {
            if(it->first<=w||it->second<=h)
            {
                it=vp.erase(it);
                --it;
            }
        }
        if(vp.empty())
        {
            cout<<0<<endl;
            return 0;
        }
        sort(vp.begin(),vp.end());
    
    
        for(int i=0;i<vp.size();i++)
            for(int j=0;j<vp.size();j++)
            {
                if(vp[j].first>vp[i].first&&vp[j].second>vp[i].second)
                {
                    if(dp[j]<dp[i]+1)
                    {
                        dp[j]=dp[i]+1;
                        record[j]=i+1;
                    }
                }
            }
        int maxv=0,maxi=0;
        for(int i=0;i<vp.size();i++)
        {
            if(dp[i]>=maxv)
            {
                maxv=dp[i];
                maxi=i;
            }
        }
    
        cout<<dp[maxi]+1<<endl;
        vector<int>vi;
        for(int j=0;j<n;j++)
        {
            if(vp[maxi]==vp2[j])
            {
                vi.push_back(j+1);
                break;
            }
        }
    
        int m=maxi;
        while(record[m])
        {
            for(int j=0;j<n;j++)
            {
                if(vp[record[m]-1]==vp2[j])
                {
                    vi.push_back(j+1);
                    break;
                }
            }
            m=record[m]-1;
        }
    
        for(int i=0;i<vi.size();i++)
            cout<<vi[vi.size()-i-1]<<" ";
        cout<<endl;
        return 0;
    }
  • 相关阅读:
    java发送邮件..转
    SSHE框架整合(增删改查)
    easyui-conbotree树形下拉框。。。转
    spring和Hibernate整合
    php实现注册
    原生ajax实现登录(一部分代码)
    Apache 与 php的环境搭建
    SSH框架整合(代码加文字解释)
    数据库中树形列表(以easyui的tree为例)
    SVN源代码的版本控制系统使用简介
  • 原文地址:https://www.cnblogs.com/cavehubiao/p/3432534.html
Copyright © 2011-2022 走看看