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;
}

 

posted @ 2013-11-19 23:52  cavehubiao  阅读(287)  评论(0编辑  收藏  举报