codeforce 3A-3B(Greedy)

3A要求到最短路径,直接算就完了,很简单

3B确实能用01背包问题解决,不过数据太多,dp表可能mle,题目给的分类是greedy贪心,于是考虑了下:

由于只有两种体积的船,1与2,所以根据value/体积,进行排序

根据贪心选择有限选择平均价值较高的,这里用两个数组ary1,ary2分别记录1和2的价值,维护两个指针分别指向ary1,ary2最大,比较,较大的选入,

如果不能够正常选完,说明肯定是最后总体积只剩1,而当前贪心选择最优是体积2的船,于是在判断是否还剩体积1的船未选,

如果有那么查找已选如的体积1的船中的最小价值+剩余体积1的船最大价值 是否大于当前最优体积2的船,如果是,选择1,如果不是,剔除以选择的最小1,选入2..

 不过写完后虽然过了,想了想我的代码还是有点问题,如果在进行最后判断时,以前的选择一直没有选入1,则会越界,看来codeforce的数据也不完善。

比如下面的数据

4 5

2 10

2 9

2 8

1 3

 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 int main()
 5 {
 6     char ch1,ch2;
 7     int y1,y2;
 8     cin>>ch1>>y1>>ch2>>y2;
 9     int x1,x2;
10     x1=ch1-'a'+1;
11     x2=ch2-'a'+1;
12 
13     if(x1==x2&&y1==y2)
14     {
15         cout<<endl;
16         return 0;
17     }
18     else if(x1==x2&&y1!=y2)
19     {
20         cout<<abs(y2-y1)<<endl;
21         for(int i=1;i<=abs(y2-y1);i++)
22             cout<<(y2>y1?'U':'D')<<endl;
23     }
24     else if(x1!=x2&&y1==y2)
25     {
26         cout<<abs(x2-x1)<<endl;
27         for(int i=1;i<=abs(x2-x1);i++)
28             cout<<(x2>x1?'R':'L')<<endl;
29     }
30     else
31     {
32         char xch=x2>x1?'R':'L';
33         char ych=y2>y1?'U':'D';
34         cout<<max(abs(x2-x1),abs(y2-y1))<<endl;
35 
36         if(abs(x2-x1)>abs(y2-y1))
37         {
38             for(int i=1;i<=abs(x2-x1)-abs(y2-y1);i++)
39                 cout<<xch<<endl;
40             for(int i=1;i<=abs(y2-y1);i++)
41                 cout<<xch<<ych<<endl;
42         }
43         else
44         {
45             for(int i=1;i<=abs(y2-y1)-abs(x2-x1);i++)
46                 cout<<ych<<endl;
47             for(int i=1;i<=abs(x2-x1);i++)
48                 cout<<xch<<ych<<endl;
49         }
50     }
51 }
#include<iostream>
#include<algorithm>
#include<vector>
#include<deque>
#include<utility>
using namespace std;

//int ary1[100002][2];
vector<pair<int,int>>ary1,ary2;
deque<pair<int,int>>de;
//int ary2[100002][2];

int main()
{
    int sum=0;
    int number, cap;
    int cnt1=0,cnt2=0;
    cin>>number>>cap;

    for(int i=1;i<=number;i++)
    {
        int x,y;
        cin>>x>>y;
        if(x==1)
        {
            ary1.push_back(make_pair(y,i));
        }
        else
        {
            ary2.push_back(make_pair(y,i));
        }
    }
    ary1.push_back(make_pair(0,0));
    ary2.push_back(make_pair(0,0));
    
    sort(ary1.begin(),ary1.end());
    sort(ary2.begin(),ary2.end());
    cnt1=ary1.size();
    cnt2=ary2.size();
    int used1=0,used2=0;

    int m1=cnt1-1,m2=cnt2-1;
    while(cap>0&&(ary1[m1].first>0||ary2[m2].first>0))
    {
        if(ary1[m1].first>((float)(ary2[m2].first)/2))
        {
            cap-=1;
            sum+=ary1[m1].first;
            used1++;
            m1--;
            de.push_back(make_pair(1,ary1[m1+1].second));
        }
        else
        {
            if(cap>=2)
            {
                cap-=2;
                sum+=ary2[m2].first;
                used2++;
                m2--;
                de.push_back(make_pair(2,ary2[m2+1].second));
            }
            else
            {
                break;
            }
        }
    }

    int mark=0;
    if(cap>0)
    {
        if(ary2[m2].first>0)
        {
            if(ary1[m1+1].first+ary1[m1].first<ary2[m2].first)
            {
                sum-=ary1[m1+1].first;
                sum+=ary2[m2].first;
                used1--;
                used2++;
                de.push_back(make_pair(2,ary2[m2].second));
                mark=ary1[m1+1].second;
            }
            else
            {
                if(ary1[m1].first>0)
                {
                    sum+=ary1[m1].first;
                    used1++;
                    de.push_back(make_pair(1,ary1[m1].second));
                }
            }
        }
    }
    cout<<sum<<endl;
    for(int i=0;i<de.size();i++)
    {
        if(de[i].second!=mark)
        cout<<de[i].second<<" ";
    }
    cout<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/cavehubiao/p/3422293.html