思路:我们可以看数据范围n*m只有5e7,那么我们可以用O(n*m)的算法去解决这个问题,n是说一共有n个人,m是对于任意一条指令抱团的人数和需要的代价,那么我们可以从高到低,从n个人开始,实行每一条指令后的剩余人数的最小代价进行迭代,然后从小往后存在答案的结果就是最少的代价。
代码:
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long
using namespace std;
const int N=1000000+100;
int n ,m,h;
ll dp[N];
struct lk{
ll x;
ll time;
}cnt[N];
void slove( )
{
int t;
sc_int(n),sc_int(m);
for(int i =1;i<=m;i++) sc_ll(cnt[i].time),sc_ll(cnt[i].x);
for(int i =1;i<=n;i++)dp[i]=1e18;
dp[n]=0;
for(int i=n;i>=1;i--)
{
if(dp[i]==1e18)continue;
for(int j=1;j<=m;j++)
{
if(cnt[j].x>=i)continue;
int sum=((int)(i/cnt[j].x))*cnt[j].x;
dp[sum]=min(dp[sum],dp[i]+cnt[j].time);
}
}
for(int i =1;i<=n;i++)
{
if(dp[i]!=1e18)
{
cout<<dp[i]<<endl;
return ;
}
}
cout<<endl;
}
int main()
{
int t;
// sc_int(t);
// while(t--)
slove();
return 0;
}
ps:从今天起就要每日好好打卡了!!