相当于做题记录
P1800 software
首先直接求解答案比较困难
注意到时间这个玩意有单调性,考虑二分答案
二分时间 ,然后验证
验证的过程可以考虑 DP
我们记 表示前 个人总共完成了 个软件 模块,同时最多能完成的软件 模块数
转移:
时间复杂度
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define YUANSHEN ios_base::sync_with_stdio(0);
#define QIDONG cin.tie(0);
#define inf 1e14
using namespace std;
const int maxn=1e2+10;
int n,m,d[2][maxn],dp[maxn][maxn];
bool judge(int t){
memset(dp,-0x3f,sizeof(dp)); dp[0][0]=0;
for(int i=1;i<=n;i++)
for(int j=0;j<=m;j++)
for(int k=0;k<=j&&t-d[0][i]*k>=0;k++)
dp[i][j]=max(dp[i][j],dp[i-1][j-k]+(t-d[0][i]*k)/d[1][i]);
return dp[n][m]>=m;
}
signed main(){
YUANSHEN QIDONG
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>d[0][i]>>d[1][i];
int L=1,R=300,ans=-1;
while(L<=R){
int mid=(L+R)>>1;
if(judge(mid))R=mid-1,ans=mid;
else L=mid+1;
}
cout<<ans;
return 0;
} P3080 [USACO13MAR] The Cow Run G/S
嗯,就是挺板的
首先对奶牛排个序
表示已经拴住了 的牛,并且当前在该区间的左/右端点
那么转移比较显然
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define YUANSHEN ios_base::sync_with_stdio(0);
#define QIDONG cin.tie(0);
#define inf 1e14
using namespace std;
const int maxn=1e3+10;
int n,p[maxn],dp[maxn][maxn][2],pos,sum[maxn];
signed main(){
YUANSHEN QIDONG
cin>>n;
for(int i=1;i<=n;i++)cin>>p[i];
sort(p+1,p+1+n);
for(int i=1;i<n;i++)if(p[i]<0&&p[i+1]>0){pos=i+1; break;}
for(int i=n;i>=pos;i--)p[i+1]=p[i];
memset(dp,0x3f,sizeof(dp)); p[pos]=0;
dp[pos][pos][0]=dp[pos][pos][1]=0;
for(int Len=1;Len<=n+1;Len++){
for(int l=1;l+Len<=n+1;l++){
int r=l+Len;
dp[l][r][0]=min({dp[l+1][r][0]+(n+1-Len)*(p[l+1]-p[l]),dp[l+1][r][1]+(n+1-Len)*(p[r]-p[l]),dp[l][r][0]});
dp[l][r][1]=min({dp[l][r-1][1]+(n+1-Len)*(p[r]-p[r-1]),dp[l][r-1][0]+(n+1-Len)*(p[r]-p[l]),dp[l][r][1]});
}
}
cout<<min(dp[1][n+1][0],dp[1][n+1][1]);
return 0;
} P4265 [USACO18FEB] Snow Boots S
首先有一个显然的结论:不可能往回走
设计状态为 表示到 号格子最小扔掉鞋子的个数
从 向后转移
如果当前的鞋子不能到达那个点,就换下一双直到可行
对于每个格子,转移时需要枚举终点和换的鞋子,时间复杂度 ,足以通过本题
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define YUANSHEN ios_base::sync_with_stdio(0);
#define QIDONG cin.tie(0);
#define inf 1e14
using namespace std;
const int maxn=250+10;
int dp[maxn],n,b,f[maxn],d[maxn],s[maxn];
signed main(){
YUANSHEN QIDONG
cin>>n>>b;
for(int i=1;i<=n;i++)cin>>f[i];
for(int i=1;i<=b;i++)cin>>s[i]>>d[i];
memset(dp,0x3f,sizeof(dp)); dp[1]=1;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(dp[i]>b)continue;
int k=dp[i];
while(!(s[k]>=f[j]&&s[k]>=f[i]&&d[k]>=j-i) && k<=b)k++;
if(k<=b)dp[j]=min(dp[j],k);
}
}
cout<<dp[n]-1;
return 0;
} P2467 [SDOI2010] 地精部落
超级南翔的题目
本质是求波动排列的个数
首先我们应该知道一个非常重要的性质:
对于一个满足条件的排列 ,若将所有的 变为 新的排列仍然合法
于是对于所有第一个数是山谷的情况,都有一个第一个数是山峰的情况对应之,于是我们只需求出第一个数是山峰的排列数再乘二即可
若第 位是山峰,即 是奇数,那么下一个数必须小于 ;若第 位是山谷,即 是偶数,那么下一个数必须大于
因为前面的反转性质,我们可以把第 位是山谷的情况转化为第 位是山峰的情况
我们钦定一个长度为 开头是 的合法排列,同时令 表示 到 的排列,并且开头是 的合法排列数
那么
前缀和可做到 转移,总时间复杂度
#include<bits/stdc++.h>
//#define int long long
#define endl '\n'
#define YUANSHEN ios_base::sync_with_stdio(0);
#define QIDONG cin.tie(0);
#define inf 1e14
using namespace std;
const int maxn=4200+10;
int p,dp[maxn][maxn],n,sum[maxn],ans=0;
signed main(){
YUANSHEN QIDONG
cin>>n>>p;
dp[1][1]=1;
for(int i=2;i<=n;i++){
for(int j=1;j<=i-1;j++)sum[j]=(sum[j-1]+dp[i-1][j])%p;
for(int j=1;j<=i;j++)dp[i][j]=(sum[i-1]-sum[i-j]+p)%p;
}
for(int j=1;j<=n;j++)(ans+=dp[n][j])%=p;
cout<<2*ans%p;
return 0;
} P4269 [USACO18FEB] Snow Boots G
贪心地想,有若干个可以走的格子,一定优先走最远的
考虑靴子 走不到是什么情况
也就是存在一个区间 满足任意一个 都有
若构造序列 那么走不到的情况就相当于存在一个长度至少为 的 的连续段,维护连续段可以使用线段树,但是直接做时间复杂度是 的
我们考虑将靴子按 从小到大排序,这样每个 只会被更改一次,时间复杂度
话说这不是 DP 题单里的吗
#include<bits/stdc++.h>
//#define int long long
#define endl '\n'
#define YUANSHEN ios_base::sync_with_stdio(0);
#define QIDONG cin.tie(0);
#define inf 1e14
using namespace std;
const int maxn=1e6+10;
int n,b,ans[maxn];
struct shoes{
int idx,s,d;
friend bool operator <(shoes i,shoes j){return i.s<j.s;}
}B[maxn],f[maxn];
struct Segtree{
struct node{int L,R,lm,rm,ma;}tr[maxn*4];
void pushup(int u){
tr[u].lm=tr[u*2].lm+tr[u*2+1].lm*(tr[u*2].lm==tr[u*2].R-tr[u*2].L+1);
tr[u].rm=tr[u*2+1].rm+tr[u*2].rm*(tr[u*2+1].rm==tr[u*2+1].R-tr[u*2+1].L+1);
tr[u].ma=max({tr[u*2].ma,tr[u*2+1].ma,tr[u*2].rm+tr[u*2+1].lm});
}
void build(int u,int l,int r){
tr[u].L=l,tr[u].R=r;
if(l==r){tr[u].lm=tr[u].rm=tr[u].ma=1; return;}
int mid=(l+r)>>1;
build(u*2,l,mid); build(u*2+1,mid+1,r);
pushup(u);
}
void update(int u,int x){
if(tr[u].L==tr[u].R){tr[u].lm=tr[u].rm=tr[u].ma=0; return;}
else if(tr[u*2].R>=x)update(u*2,x);
else update(u*2+1,x);
pushup(u);
}
int query(int d){return (tr[1].ma<d);}
}T;
signed main(){
YUANSHEN QIDONG
cin>>n>>b;
for(int i=1;i<=n;f[i].idx=i,i++)cin>>f[i].s;
for(int i=1;i<=b;B[i].idx=i,i++)cin>>B[i].s>>B[i].d;
sort(B+1,B+1+b); sort(f+1,f+1+n); T.build(1,1,n);
for(int i=1,j=1;i<=b;i++){
while(f[j].s<=B[i].s&&j<=n)T.update(1,f[j].idx),j++;
ans[B[i].idx]=T.query(B[i].d);
}
for(int i=1;i<=b;i++)cout<<ans[i]<<endl;
return 0;
} P4859 已经没有什么好害怕的了
给定两个数组 ,求有多少种两两配对的方案,使得配对出的二元组 中, 的二元组数量恰好是 的二元组数量加 ,保证没有重复
考虑容斥
表示恰好有 组糖果比药片大, 表示至少有 组糖果比药片大
答案是
先判 不为整数的情况
那么有
考虑 DP 求解
先把两个序列序列升序排序
令 表示前 个 全部配对好,有 组是 的
考虑转移,对于一个新加的 ,其有两种选择:
- 找一个比它小的 配对
- 找一个比它大的 配对
记 表示排序后的 中最后一个 的
那么有转移:
那么 ,含义是选定 组,其他随便选
然后直接反演就行了
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define YUANSHEN ios_base::sync_with_stdio(0);
#define QIDONG cin.tie(0);
#define inf 1e14
#define mod 1000000009
using namespace std;
const int maxn=5e3+10;
int n,a[maxn],b[maxn],k,fac[maxn],dp[maxn][maxn],pos[maxn],inv[maxn],x,ans=0;
int qpow(int a,int x){
int res=1;
for(;x;x>>=1,a=(a*a)%mod)if(x&1)res=(res*a)%mod;
return res;
}
void init(){
fac[0]=inv[0]=1; x=(n+k)/2;
for(int i=1;i<=n;i++)fac[i]=fac[i-1]*i%mod;
inv[n]=qpow(fac[n],mod-2);
for(int i=n-1;i;i--)inv[i]=inv[i+1]*(i+1)%mod;
}
int C(int n,int m){return fac[n]*inv[m]%mod*inv[n-m]%mod;}
signed main(){
YUANSHEN QIDONG
cin>>n>>k; init();
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];
sort(a+1,a+1+n),sort(b+1,b+1+n);
for(int i=1,p=0;i<=n;pos[i]=p,i++)while(b[p+1]<a[i]&&p<n)++p;
dp[0][0]=1;
for(int i=1;i<=n;i++)
for(int j=0;j<=i;j++)
if(j&&(pos[i]>=(j-1)))dp[i][j]=(dp[i-1][j]+dp[i-1][j-1]*(pos[i]-j+1)%mod)%mod;
else dp[i][j]=dp[i-1][j];
for(int i=x;i<=n;i++)
if((i-x)&1)ans=(ans-dp[n][i]*C(i,x)%mod*fac[n-i]%mod+mod)%mod;
else ans=(ans+dp[n][i]*C(i,x)%mod*fac[n-i]%mod)%mod;
cout<<ans;
return 0;
} 没判都过了。。。