目录
A-Fence
B-D++ again
C-Cut the Sequence
D-Parade
E-trade
A-Fence
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<queue>
using namespace std;
struct nob
{
int v,p;
bool operator <(const nob &a)const
{
return this->v<a.v;
}
nob() {};
nob(int _v,int _p)
{
this->v=_v;
this->p=_p;
}
};
struct node
{
int l,p,s;
bool operator < (const node &a)const
{
return this->s<a.s;
}
} A[110];
int dp[110][16010];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=1; i<=m; i++)
scanf("%d%d%d",&A[i].l,&A[i].p,&A[i].s);
sort(A+1,A+m+1);
memset(dp,0,sizeof(dp));
for(int i=1; i<=m; i++)
{
priority_queue<nob>q;
int li=A[i].l,pi=A[i].p,si=A[i].s;
for(int j=max(0,si-li); j<si; j++)
{
q.push(nob(dp[i-1][j]-j*pi,j));
}
for(int j=1; j<=n; j++)
{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
if(j<si||j>=si+li)
continue;
while(!q.empty()&&q.top().p+li<j) q.pop();
if(q.empty())
continue;
dp[i][j]=max(dp[i][j],q.top().v+pi*j);
}
}
printf("%d\n",dp[m][n]);
}
}
B-D++ again
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdlib>
#include<iomanip>
#include<string>
#include<vector>
#include<map>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
#define Max(a,b) (a>b)?a:b
#define lowbit(x) x&(-x)
int main()
{
map<char,bool>vis;
vis['1']=1;
vis['2']=1;
vis['3']=1;
vis['4']=1;
vis['5']=1;
vis['6']=1;
vis['7']=1;
vis['8']=1;
vis['9']=1;
vis['0']=1;
vis['+']=1;
vis['-']=1;
vis['*']=1;
vis['/']=1;
vis['\n']=1;
vis['=']=1;
char t,last=0;
int sum=0,flag=0,ans=1;
while(~scanf("%c",&t))
{
if(flag==1||ans==0)
{
if(t==')')
{
if(last=='*')
{
flag=0;
}
}
last=t;
continue;
}
if(t=='*'&&last=='(')
{
flag=1,sum--;
continue;
}
else if(sum==0&&t==')'&&last=='*')
ans=0;
if(sum<0)
{
ans=0;
continue;
}
else if(sum>0)
{
if(t==')')
{
sum--;
}
else if(t=='(')
sum++;
else if(vis[t]==0)
ans=0;
last=t;
}
else
{
if(t=='(')
{
sum++;
}
else if(t==')')
sum--;
last=t;
}
}
if(sum||flag||ans==0)
puts("NO");
else
puts("YES");
}
C-Cut the Sequence
#include <iostream>
#include <cstdio>
#include <cmath>
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(s<t&&a[que[t-1]]<=a[i]) t--;
que[t++]=i;
while(sum[que[t-1]]-sum[p]>M){
p++;
if(que[s]<=p){
s++;
}
}
dp[i]=dp[p]+a[que[s]];
for(int j=s;j<t;j++){
dp[i]=min(dp[i],dp[que[j]]+a[que[j+1]]);
}
}
}
if(flag==0){
cout<<-1;
}
else{
cout<<dp[N];
}
}
D-Parade
思路:很显然这是一个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<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#include<algorithm>
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;
}
E-trade
知道之后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 //注意要求第i-w----i-1天都不能交易.
卖(k-j)股时为dp[i-w-1][k]+(k-j)*s[i] j=<k<=p
当固定某个j时,dp[i-w-1][k]+k*b[i] 是固定的(k,j有个关系),所以可以用单调队列优化。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
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<w+2)continue;
int pre=i-w-1;
int s=0,e=-1;
for(int j=0;j<=p;j++)//buy j-k,so is from small to big
{
int tmp=dp[pre][j]+j*ap[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&&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]);
}
}