目录
A-Fence
B-D++ again
C-Cut the Sequence
D-Parade
E-trade
#include
#include
#include
#include
using namespace std;
struct nob
{
int v,p;
bool operator <(const nob &a)const
{
return this->vv=_v;
this->p=_p;
}
};
struct node
{
int l,p,s;
bool operator < (const node &a)const
{
return this->sq;
int li=A[i].l,pi=A[i].p,si=A[i].s;
for(int j=max(0,si-li); j=si+li)
continue;
while(!q.empty()&&q.top().p+li
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int N;
long long M;
long long a[100005];
int que[100005];
long long sum[100005];
long long dp[100005];
int main()
{
cin>>N>>M;
int flag=1;
for(int i=1;i<=N;i++){
cin>>a[i];
if(a[i]>M)
flag=0;
sum[i]=sum[i-1]+a[i];
}
int s=1,t=1,p=0;
if(flag==1){
for(int i=1;i<=N;i++){
while(sM){
p++;
if(que[s]<=p){
s++;
}
}
dp[i]=dp[p]+a[que[s]];
for(int j=s;j
思路:很显然这是一个dp题,设dp[i][j]到达(i,j)这个点的人气最大值,那么到达(i,j)这个位置可以从东、西、南三个方向到达,从南边到来的很容易得出就是dp[i + 1][j],剩下就是计算从东边和从西边走来的,先处理西边走来的(东边的一样),显然dp[i][j] = min{ dp[i][j], dp[i + 1][k] + valL[i][j] - valL[i][k - 1] } (其中k < j而且sumL[i][j] - sumL[i][k-1] <= K(题目给定的K),sumL[i][j]表示从左边开始第一个点到达第j个点的总距离,valL[i][j]表示从左边开始第一个点到达第j个点的总人气值, 因为从第k个点能到达第j个点的话只能按照(i+1, k)北走再向右走的路线,所以是dp[i + 1][k],而不是dp[i][k],此时的dp[i][k]已经更新了)
#include
#include
#include
#include
#include
typedef long long ll;
const int maxn = 1e2 + 10;
const int maxm = 1e4 + 10;
const ll INF = 1e15;
using namespace std;
int n, m, k, x;
int ql[maxm * 10], qr[maxm * 10];
ll sumL[maxm], valL[maxm];
ll sumR[maxm], valR[maxm];
ll dis[maxn][maxm], dp[2][maxm];
ll val[maxn][maxm];
char s[maxn * maxm];
void init() {
for(int i = 0; i < n + 5; i++) {
dis[i][0] = dis[i][m + 1] = 0;
val[i][0] = val[i][m + 1] = 0;
}
}
ll getint() {
ll data = 0,u = 1;
while(s[x] < '0' || s[x] > '9') x++;
if (x && s[x - 1] == '-') u = -1;
while (s[x] >= '0' && s[x] <= '9') {
data = data * 10 + s[x] - '0';
x++;
}
return data * u;
}
int solve(int cal, int c) {
while(cal) {
sumL[0] = sumR[m + 2] = 0;
valL[0] = valR[m + 2] = 0;
///处理好从南部走上来的
for(int j = 1; j <= m + 1; j++) {
int r = m + 2 - j;
sumL[j] = sumL[j - 1] + dis[cal][j];
valL[j] = valL[j - 1] + val[cal][j];
sumR[r] = sumR[r + 1] + dis[cal][r];
valR[r] = valR[r + 1] + val[cal][r];
dp[c][j] = dp[c ^ 1][j];
}
int tail, real;
///处理从左边走来的
tail = real = 0;
ql[real++] = 1;
for(int j = 2; j <= m + 1; j++) {
while(tail < real && sumL[j - 1] - sumL[ql[tail] - 1] > k) tail++;
if(real - tail > 0) {
ll add = dp[c ^ 1][ql[tail]];
add += valL[j - 1] - valL[ql[tail] - 1];
dp[c][j] = max(dp[c][j], add);
}
ll new_val = dp[c ^ 1][j] - valL[j - 1];
while(real > tail && new_val >= dp[c ^ 1][ql[real - 1]] - valL[ql[real - 1] - 1]) real--;
ql[real++] = j;
}
///处理从右边走来的
tail = real = 0;
qr[real++] = m + 1;
for(int j = m; j >= 1; j--) {
while(tail < real && sumR[j] - sumR[qr[tail]] > k) tail++;
if(real - tail > 0) {
ll add = dp[c ^ 1][qr[tail]];
add += valR[j] - valR[qr[tail]];
dp[c][j] = max(dp[c][j], add);
}
ll new_val = dp[c ^ 1][j] - valR[j];
while(real > tail && new_val >= dp[c ^ 1][qr[real- 1]] - valR[qr[real - 1]]) real--;
qr[real++] = j;
}
cal--; c = !c;
}
return !c;
}
int main()
{
while (gets(s)) {
x = 0; n = getint();
m = getint(); k = getint();
if (!n && !m && !k) break;
init();
for(int i = 1; i <= n + 1; i++) {
gets(s); x = 0;
for(int j = 1;j <= m; j++) {
val[i][j] = getint();
}
}
for(int i = 1;i <= n + 1; i++) {
gets(s); x = 0;
for(int j = 1; j <= m; j++) {
dis[i][j] = getint();
}
}
for(int j = 1; j <= m + 1; j++) dp[0][j] = 0;
int d = solve(n + 1, 1);
ll ans = -INF;
for(int j = 1; j <= m + 1; j++) {
ans = max(ans, dp[d][j]);
}
cout << ans << endl;
}
return 0;
}
知道之后n天的股票买卖价格(api,bpi),以及每天股票买卖数量上限(asi,bsi),问他最多能赚多少钱。开始时有无限本金,要求任两次交易需要间隔W天以上,即第i天交易,第i+w+1天才能再交易。同时他任意时刻最多只能拥有maxp的股票,
dp[i][j]表示前i天当持有j股股票时,获得的最大利益。
状态转移:
当第i天不交易时为dp[i-1][j];
当第i天买(j-k)股时为dp[i-w-1][k]-(j-k)*b[i] 0=
卖(k-j)股时为dp[i-w-1][k]+(k-j)*s[i] j=
当固定某个j时,dp[i-w-1][k]+k*b[i] 是固定的(k,j有个关系),所以可以用单调队列优化。
#include
#include
#include
#include
#include
using namespace std;
const int maxn=2200;
const int inf=0x3f3f3f3f;
int dp[maxn][maxn];
int t,p,w;
struct node
{
int val,hs;
node(int h,int v)
{
hs=h,val=v;
}
node ()
{
}
}v[maxn];
int ap[maxn],bp[maxn],as[maxn],bs[maxn];
int main()
{
int tt;
scanf("%d",&tt);
while(tt--)
{
scanf("%d%d%d",&t,&p,&w);
for(int i=1;i<=t;i++)
{
scanf("%d%d%d%d",&ap[i],&bp[i],&as[i],&bs[i]);
}
memset(dp,-inf,sizeof dp);
for(int i=1;i<=t;i++)
{
for(int j=0;j<=min(as[i],p);j++)
{
dp[i][j]=max(dp[i][j],-j*ap[i]);
}
}
for(int i=2;i<=t;i++)
{
dp[i][0]=0;
for(int j=0;j<=p;j++)//no transaction
{
dp[i][j]=max(dp[i][j],dp[i-1][j]);
}
if(i=v[e].val)//the val is bigger which is better
{
e--;
}
node te(j,tmp);
v[++e]=te;
while(s<=e&&j-v[s].hs>as[i])
s++;
dp[i][j]=max(dp[i][j],v[s].val-j*ap[i]);
}
s=0,e=-1;
for(int j=p;j>=0;j--)//sell k-j,so is from big to small
{
int tmp=dp[pre][j]+j*bp[i];
while(s<=e&&tmp>=v[e].val)//the val is bigger which is better
{
e--;
}
node te(j,tmp);
v[++e]=te;
while(s<=e&&v[s].hs-j>bs[i])
s++;
dp[i][j]=max(dp[i][j],v[s].val-j*bp[i]);
}
}
printf("%d\n",dp[t][0]);
}
}