1. 程式人生 > 實用技巧 >Codeforces 1379C 貪心+模擬

Codeforces 1379C 貪心+模擬

xg

題意

  有m種花,每種花數量無限。每個花有兩個幸福值a,b,一種花的第一朵花是a,其餘全是b。問買n朵花,最高幸福值是多少。

思路

  對於每種花。第i種花,n朵花全買這種,然後二分查詢a,找到比bi大的a,然後計算數量cnt(cnt的大小需要仔細斟酌)。n中買i的cnt個不買i了,改買比bi大的a。

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include 
<iomanip> #include <algorithm> #include <queue> #include <stack> #include <set> #include <vector> // #include <bits/stdc++.h> #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sp ' ' #define endl '\n' #define inf 0x3f3f3f3f; #define FOR(i,a,b) for( int i = a;i <= b;++i) #define
bug cout<<"--------------"<<endl #define P pair<int, int> #define fi first #define se second #define pb(x) push_back(x) #define ppb() pop_back() #define mp(a,b) make_pair(a,b) #define ms(v,x) memset(v,x,sizeof(v)) #define rep(i,a,b) for(int i=a;i<=b;i++) #define repd(i,a,b) for(int i=a;i>=b;i--) #define
sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define sca2(a,b) scanf("%d %d",&(a),&(b)) #define sca(a) scanf("%d",&(a)); #define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c)) #define sca2ll(a,b) scanf("%lld %lld",&(a),&(b)) #define scall(a) scanf("%lld",&(a)); using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;} const double Pi = acos(-1.0); const double epsilon = Pi/180.0; const int maxn = 2e5+10; ll n,m,x,k,y; int p[maxn],a[maxn],b[maxn]; int tail; queue<pair<int,int> >que; void Init() { cin>>n>>m; cin>>x>>k>>y; rep(i,1,n) cin>>a[i]; rep(i,1,m) cin>>b[i]; int flag = 1; int pos = 1; tail = 0; rep(i,1,n){ if(a[i] == b[pos]){ p[++tail] = i; pos++; } } if(tail != m){ cout<<-1<<endl; exit(0); } int maxx = a[p[1]]; int cnt = 0; rep(i,1,p[1]-1){ if(a[i] > maxx){ cnt++; } } que.push(mp(cnt,p[1]-1)); rep(i,2,tail){ int l = p[i-1]; int r = p[i]; maxx = max(a[l],a[r]); cnt = 0; rep(j,l+1,r-1){ if(a[j] > maxx) cnt++; } que.push(mp(cnt,r-l-1)); } maxx = a[p[tail]]; cnt = 0; rep(i,p[tail]+1,n){ if(a[i] > maxx) cnt++; } que.push(mp(cnt,n-p[tail])); } ll ans = 0; void solve() { while(que.size()){ int len = que.front().se; int flag = que.front().fi; que.pop(); if(len < k){ if(flag) { cout<<-1<<endl; exit(0); } else { ans += len*y; } } else { int tmp1 = len/k; int tmp2 = len%k; if(flag){ ll sum1 = x + (len-k)*y; ll sum2 = tmp1*x + tmp2*y; ans += min(sum1,sum2); }else{ ll sum1 = len*y; ll sum2 = tmp1*x + tmp2*y; ans += min(sum1,sum2); } } } } int main() { //freopen("input.txt", "r", stdin); Init(); solve(); cout<<ans<<endl; }