首页 > 其他分享 >abc270d Stones

abc270d Stones

时间:2023-08-13 11:22:27浏览次数:30  
标签:Stones txt int ll abc270d include fo

abc270d

直接贪心每次取最大的会有问题,比如说下面的例子
11
2 4 5

我们考虑dp
\(f[i]\)表示在先手的情况下,有i个石头的局面,最多能拿多少个石头,同时记录\(g[i]\)表示选的哪一个\(a[i]\)
那么转移就是
\(f[i]=max(f[i-a[j]-a[g[i-a[j]]] ]+a[j])\)

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#define fo(i,a,b) for (int (i)=(a);(i)<=(b);(i)++)
#define fd(i,b,a) for (int (i)=(b);(i)>=(a);(i)--)
using namespace std;
typedef long long ll;
const int N=2e5+5;

int n,k,cnt,s,j;
ll a[N],f[N],g[N];
int main() {
   
    // #ifdef  LOCAL
    //     freopen("in.txt","r",stdin);
    //     freopen("out.txt","w",stdout);
    // #endif

    scanf("%d %d",&n,&k);
    fo(i,1,k) scanf("%d",&a[i]);
    fo(i,0,a[1]-1) {
        f[i]=0;
        g[i]=0;
    }
    f[a[1]]=a[1];
    g[a[1]]=1;



    fo(i,a[1]+1,n) {
        fo(j,1,k){
            if (i<a[j]) continue;

            if (f[i-a[j]-a[g[i-a[j]]] ] +a[j]>f[i]) {
                f[i]=f[i-a[j]-a[g[i-a[j]]] ] +a[j];
                g[i]=j;
            }
        }
    }
    printf("%lld\n",f[n]);

    return 0;
}

标签:Stones,txt,int,ll,abc270d,include,fo
From: https://www.cnblogs.com/ganking/p/17626298.html

相关文章

  • 【每日一题】Problem 443B. Kuriyama Mirai's Stones
    原题解决思路两个数组,一个未排序,一个排序;使用前缀和的方式减少时间复杂度#include<bits/stdc++.h>intmain(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);std::cout.tie(nullptr);intn;std::cin>>n;std::vector<int>v(n+1,0);f......
  • leetcode 771. Jewels and Stones
    You'regivenstrings J representingthetypesofstonesthatarejewels,and S representingthestonesyouhave. Eachcharacterin Sisatypeofstoneyouhave. Youwanttoknowhowmanyofthestonesyouhavearealsojewels.Thelettersin J areg......
  • 2023-04-20:有一堆石头,用整数数组 stones 表示 其中 stones[i] 表示第 i 块石头的重量
    2023-04-20:有一堆石头,用整数数组stones表示其中stones[i]表示第i块石头的重量。每一回合,从中选出任意两块石头,然后将它们一起粉碎假设石头的重量分别为x和y,且x<=y那么粉碎的可能结果如下:如果x==y,那么两块石头都会被完全粉碎;如果x!=y,那么重量为x的石头将......
  • 2023-04-20:有一堆石头,用整数数组 stones 表示 其中 stones[i] 表示第 i 块石头的重量
    2023-04-20:有一堆石头,用整数数组stones表示其中stones[i]表示第i块石头的重量。每一回合,从中选出任意两块石头,然后将它们一起粉碎假设石头的重量分别为x和y,且x<=y那么粉碎的可能结果如下:如果x==y,那么两块石头都会被完全粉碎;如果x!=y,那么重量为x的石头将......
  • [leeccode]771. Jewels and Stones
    J representingthetypesofstonesthatarejewels,and S representingthestonesyouhave. Eachcharacterin Sisatypeofstoneyouhave. Youwantto......
  • 【ABC270D】Stones
    首先很显然直接贪心是不行的,就好像背包的时候一直选价值最大的肯定会假一样。诶?背包?这题还真有点像背包。考虑像背包一样设,\(f_i\)表示剩下\(i\)个石子的先手最大获得......
  • LeetCode - 771. Jewels and Stones
    题目You’regivenstrings​​J​​​representingthetypesofstonesthatarejewels,and​​S​​representingthestonesyouhave.EachcharacterinSisa......
  • [LeetCode] 1753. Maximum Score From Removing Stones
    Youareplayingasolitairegamewith threepiles ofstonesofsizes a​​​​​​, b,​​​​​​and c​​​​​​respectively.Eachturnyouchoosetw......
  • [LeetCode] 947. Most Stones Removed with Same Row or Column
    Ona2Dplane,weplace n stonesatsomeintegercoordinatepoints.Eachcoordinatepointmayhaveatmostonestone.Astonecanberemovedifitshareseit......
  • ABC 270 D - Stones(博弈DP)
    https://atcoder.jp/contests/abc270/tasks/abc270_d题目大意:给定我们总共n个石子,我们每次拿的数量都必须是数组a中的一个,高桥先手,青木后手。问我们高桥可以拿到的最......