「杂题乱刷2」CF1183E & CF1183H
1.「杂题乱刷」洛谷P10642.「杂题乱刷」洛谷P22853.「杂题乱刷」洛谷P15444.「杂题乱刷」洛谷P12165.「杂题乱刷」CF1272D6.「杂题乱刷」CF1105C7.「杂题乱刷」洛谷P24268.「杂题乱刷」AT_abc280_e9.「杂题乱刷」AT_abc307_e10.「杂题乱刷」AT_abc208_e11.「杂题乱刷」P868712.「杂题乱刷」洛谷 P183113.「杂题乱刷」ZhengRui 611 【19寒假普转提】树的权14.「杂题乱刷」AT_abc314_e15.「杂题乱刷」AT_abc220_f16.「杂题乱刷」Zheng Rui 327 【2018普转提day17专题】洗17.「杂题乱刷」AT_abc354_f18.「杂题乱刷」 AT_abc285_e19.「杂题乱刷」P881620.「杂题乱刷」AT_abc358_g21.「杂题乱刷」CF1987D22.「杂题乱刷2」CF607B23.「杂题乱刷2」CF1513C
24.「杂题乱刷2」CF1183E & CF1183H
25.「杂题乱刷2」CF1839E26.「杂题乱刷2」CF1433F27.「杂题乱刷2」CF1354Evp 到的。
题目链接
CF1183E Subsequences (eazy version)
CF1183H Subsequences (hard version)
解题思路
考虑动态规划。
设
于是我们就有以下转移:
其中,
然后大力将 dp 结果贪心选取即可。
注意,
时间复杂度
参考代码
/*
Tips:
你数组开小了吗?
你MLE了吗?
你觉得是贪心,是不是该想想dp?
一个小时没调出来,是不是该考虑换题?
打 cf 不要用 umap!!!
记住,rating 是身外之物。
该冲正解时冲正解!
Problem:
算法:
思路:
*/
#include<bits/stdc++.h>
using namespace std;
//#define map unordered_map
#define re register
#define ll long long
#define forl(i,a,b) for(re ll i=a;i<=b;i++)
#define forr(i,a,b) for(re ll i=a;i>=b;i--)
#define forll(i,a,b,c) for(re ll i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(re ll i=a;i>=b;i-=c)
#define pii pair<ll,ll>
#define mid ((l+r)>>1)
#define lowbit(x) (x&-x)
#define pb push_back
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define db long double
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define x first
#define y second
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
#define maxqueue priority_queue<ll>
#define minqueue priority_queue<ll,vector<ll>,greater<ll>>
//ll pw(ll x,ll y,ll mod){if(x==0)return 0;ll an=1,tmp=x;while(y){if(y&1)an=(an*tmp)%mod;tmp=(tmp*tmp)%mod;y>>=1;}return an;}
void Max(ll&x,ll y){x=max(x,y);}
void Min(ll&x,ll y){x=min(x,y);}
ll _t_;
void _clear(){}
/*
dp,
dp[i][j]表示前i个字符中长度为j的不同的子序列数量
*/
ll n,m;
ll dp[110][110],ans;
string s;
ll lst[310],pre[310];
ll f(char x){
return x-'a';
}
void solve()
{
_clear();
cin>>n>>m>>s;
s=' '+s;
forl(i,1,n)
pre[i]=lst[f(s[i])],lst[f(s[i])]=i;
forl(i,0,n)
dp[i][0]=1;
forl(i,1,n)
forl(j,1,i)
{
dp[i][j]=dp[i-1][j]+dp[i-1][j-1];
if(pre[i])
dp[i][j]-=dp[pre[i]-1][j-1];
}
forr(i,n,0)
{
Max(dp[n][i],0ll);
if(m>=dp[n][i])
ans+=(n-i)*dp[n][i],m-=dp[n][i];
else
ans+=(n-i)*m,m=0;
}
if(m==0)
cout<<ans<<endl;
else
cout<<-1<<endl;
}
int main()
{
IOS;
_t_=1;
// cin>>_t_;
while(_t_--)
solve();
QwQ;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤