ABC 271 ABCD(dp)

https://atcoder.jp/contests/abc271
A - 484558

题目大意:

将数字转化成两位的16进制数字,字符需要大写。

可补前导0。
Sample Input 2  
12
Sample Output 2  
0C
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL INF=1e9;
const LL N=1000200,M=2002;
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    map<LL,char> mp;
    for(int i=0;i<=9;i++)
        mp[i]=(char)(i+48);
    mp[10]='A'; mp[11]='B'; mp[12]='C';
    mp[13]='D'; mp[14]='E'; mp[15]='F';
    while(T--)
    {
        LL n;
        cin>>n;
        cout<<mp[n/16]<<mp[n%16]<<endl;
    }
    return 0;
}

B - Maintain Multiple Sequences

题目大意:

数字的存储以及查找。
Sample Input 2  
3 4
4 128 741 239 901
2 1 1
3 314 159 26535
1 1
2 2
3 3
1 4
Sample Output 2  
128
1
26535
901
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL INF=1e9;
const LL N=1000200,M=2002;
vector<LL> v[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        LL n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            LL num;
            cin>>num;
            for(int j=1;j<=num;j++)
            {
                LL x;
                cin>>x;
                v[i].push_back(x);
            }
        }
        while(m--)
        {
            LL a,b;
            cin>>a>>b;
            cout<<v[a][b-1]<<endl;
        }
    }
    return 0;
}

C - Manga

题目大意:

给定n本书的编号,我需要看这个书籍的连续集,每次可以用2本换自己想要的一本,问我们能看到哪一本?
Sample Input 1 
6
1 2 4 6 7 271
Sample Output 1 
4

无用的数据时,不用管具体的数值

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL INF=1e9;
const LL N=1000200,M=2002;
LL a[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        for(LL i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+1+n);
        LL st=0,ed=n;
        //ed表示剩余可操作次数
        for(LL i=1;i<=n; )
        {
            //刚好完美抵消一个数字,可操作数字--
            if(a[i]==st+1) st++,i++,ed--;
            //比当前需要的还更小,没什么用处
            else if(a[i]<st+1) i++;
            //比当前需要的还更大,调用可操作数字
            else if(a[i]>st+1)
            {
                if(ed>=2) ed-=2,st++;
                else break;
            }
            if(ed==0) break;
        }
        st+=ed/2;
        cout<<st<<endl;
    }
    return 0;
}

D - Flip and Adjust

题目大意:

给定n个牌,每张牌有正反面,都标了数值,问我们能不能凑出总和m?

可以的话是怎么凑的?用‘T’表示正面,用‘H’表示反面。
Sample Input 1 
3 11
1 4
2 3
5 7
Sample Output 1 
Yes
THH

看起来很简单的一个题目,一通dfs怼上去妥妥爆时hh(这题我还是不懂这时间复杂度怎么搞的)
换一种写法:dp就行啦

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL INF=1e9;
const LL N=5000200,M=2002;
LL n,m;
LL a[N],b[N];
LL f[150][10020];
char s[N];
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        memset(f,0,sizeof f);
        cin>>n>>m;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i]>>b[i];
        }
        f[0][0]=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=10020;j>=a[i];j--)
            {
                f[i][j]=f[i][j]|f[i-1][j-a[i]];
            }
            for(int j=10020;j>=b[i];j--)
            {
                f[i][j]=f[i][j]|f[i-1][j-b[i]];
            }
        }
        if(f[n][m]==1)
        {
            cout<<"Yes"<<endl;
            for(int i=n;i>=1;i--)
            {
                //从a回退一步数值依然存在
                if(m>=a[i]&&f[i-1][m-a[i]]==1)
                {
                    s[i]='H';
                    m-=a[i];
                }
                //从b回退一步数值依然存在
                else
                {
                    s[i]='T';
                    m-=b[i];
                }
            }
            for(int i=1;i<=n;i++)
                cout<<s[i];
            cout<<endl;
        }
        else cout<<"No"<<endl;
    }
    return 0;
}
posted @ 2022-11-09 20:12  高尔赛凡尔娟  阅读(40)  评论(0编辑  收藏  举报