CodeForces1154F
CodeForces1154F
\(ZS\)大佬说这是一道\(SBDP\)题.然鹅我懵逼了半天才懵逼过来怎么做(还是在\(solution\)和\(ZS\)大佬的指导下才明白...)
数据范围疯狂暗示你\(O(k^2)DP\),事实上稍微一想状态就出来了,\(f[i]\)表示买\(k\)双鞋的最少花费.
然后这个方程需要\(O(n^2)\)的转移,但是,由于限制了要买严格\(k\)双鞋,所以就只需要枚举到\(k\)即可 .
而\(k\le 2000\),所以这就随便过了.
就每次考虑一下从哪里转移过来,显然\(f[i]\)可以从\(f[0],f[1],f[2]...f[i-1]\)转移过来.每次只需要考虑有没有对应的套餐去用.
比较一下就好了,这里可以采用一个辅助数组帮助实现转移:令\(g[i]\)表示买\(i\)双鞋最多可以免费多少双.这样就比较显然了.
当然,中间由于需要区间和(每次考虑套餐都可能涉及一段区间的和),所以需要前缀和预处理一下.
由此,得到方程是:
\[f[i]=\min_{j=0}^i{f[j]+s[i]-s[j+g[i-j]]}
\]
\(Code:\)
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = a ; i <= b ; ++ i)
#define per(i,a,b) for (int i = a ; i >= b ; -- i)
#define pii pair < int , int >
#define X first
#define Y second
#define rint read<int>
#define int long long
#define pb push_back
using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
template < class T >
inline T read () {
T x = 0 , f = 1 ; char ch = getchar () ;
while ( ch < '0' || ch > '9' ) {
if ( ch == '-' ) f = - 1 ;
ch = getchar () ;
}
while ( ch >= '0' && ch <= '9' ) {
x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
ch = getchar () ;
}
return f * x ;
}
const int N = 2e5 + 100 ;
int n , m , k , v[N] , p[N] , f[N] ;
inline bool cmp (int a , int b) { return a > b ; }
signed main (int argc , char * argv[] ) {
n = rint () ; m = rint () ; k = rint () ;
rep ( i , 1 , n ) v[i] = rint () ; int x ;
rep ( i , 1 , m ) x = rint () , p[x] = max ( rint () , p[x] ) ;
MEM ( f , 0x7f ) ; f[0] = 0 ; std::sort ( v + 1 , v + n + 1 ) ;
rep ( i , 1 , n ) v[i] += v[i-1] ;
rep ( i , 1 , k ) rep ( j , 0 , i )
f[i] = min ( f[i] , f[j] + v[i] - v[j+p[i-j]] ) ;
printf ("%lld\n" , f[k] ) ;
system ("pause") ; return 0 ;
}
May you return with a young heart after years of fighting.