Educational Codeforces Round 39
Educational Codeforces Round 39
D. Timetable
令\(dp[i][j]\)表示前\(i\)天逃课了\(j\)节课的情况下,在学校的最少时间
转移就是枚举第\(i\)天逃了\(x\)节课,然后取当天逃\(x\)节课情况下在学校的最小值即可
view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "\n"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define scs(s) scanf("%s",s)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x) cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 2e5+7;
int n, m, k;
char s[MAXN];
void solve(){
sci(n); sci(m); sci(k);
vi f(k+1,n*m);
f[k] = 0;
for(int d = 1; d <= n; d++){
vi next_f(k+1,n*m);
scs(s+1);
vi A; for(int i = 1; i <= m; i++) if(s[i]=='1') A << i;
vi cost(A.size()+1,n*m);
for(int i = 0; i <= A.size(); i++){
if(i==A.size()) cost[i] = 0;
else for(int j = 0; j <= i; j++) cmin(cost[i],A[A.size() - 1 - i + j] - A[j] + 1);
}
for(int pre = 0; pre <= k; pre++) for(int cur = 0; cur <= min(pre,(int)A.size()); cur++) cmin(next_f[pre-cur],f[pre]+cost[cur]);
f.swap(next_f);
}
cout << *min_element(all(f)) << endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
solve();
return 0;
}
E. Largest Beautiful Number
考虑从后往前枚举每个位置,判断在这个位置之前全部相同,这个位置比原来小的情况下是否存在合法解即可
特判长度为奇数的情况和小于等于\(1xxxxxx1\)的情况
view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "\n"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define scs(s) scanf("%s",s)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x) cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 2e5+7;
int n;
string s;
void solve(){
cin >> s;
function<bool(void)>check = [&](){
string str(s.size(),'0');
str.front() = str.back() = '1';
return str >= s;
};
if(s.size()&1 or check()){
for(int i = 1; i <= (s.size()&1 ? s.size() - 1 : s.size() - 2); i++) cout << 9;
cout << endl; return;
}
vi cnt(10,0);
for(int i = 0; i < s.size(); i++) cnt[s[i]-'0'] ^= 1;
for(int i = s.size() - 1; ~i; i--){
cnt[s[i]-'0'] ^= 1;
for(char c = s[i] - 1; c >= '0'; c--){
cnt[c-'0'] ^= 1;
int tot = accumulate(all(cnt),0);
if(tot>s.size()-i-1){
cnt[c-'0'] ^= 1;
continue;
}
string str(s);
s[i] = c;
int cur = i;
for(int j = 0; j < s.size()-i-1-tot; j++) s[++cur] = '9';
for(int j = 9; ~j; j--) if(cnt[j]) s[++cur] = j + '0';
cout << s << endl;
return;
}
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
int tt; for(cin >> tt; tt--; solve());
return 0;
}
F. Fibonacci String Subsequences
\(dp[i][l][r]\)表示\(f(i)\)中子串\(s[l:r]\)以子序列的方式出现的次数,考虑转移
对于一般情况,可以考虑把\(s[l:r]\)分成\(s[l:k]\)和\(s[k+1:r]\),然后分别在\(f(i-1)\)和\(f(i-2)\)中找
其次考虑\(s[l:r]\)全在\(f(i-1)\)或\(f(i-2)\)中,如果\(r=n-1\),那么在\(f(i-1)中找到\)\(s[l:r]\)出现次数之后由于后面可以随便选,乘上\(2^{fib(i-2)}\)即可,否则只算左边一部分,对于\(l=0\)的情况类似处理即可
view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "\n"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define scs(s) scanf("%s",s)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x) cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 2e2+7;
const int MOD = 1e9+7;
LL ksm(LL a, LL b){
LL ret = 1;
while(b){
if(b&1) ret = ret * a % MOD;
b >>= 1;
a = a * a % MOD;
}
return ret;
}
int n, x, fib[MAXN], f[MAXN][MAXN][MAXN];
string s;
int dp(int x, int l, int r){
int &ret = f[x][l][r];
if(~ret) return ret;
if(x==0) return (ret = ((l==r) and s[l]=='0'));
if(x==1) return (ret = ((l==r) and s[l]=='1'));
ret = 0;
if(r==n-1) ret = (ret + 1ll * dp(x-1,l,r) * ksm(2,fib[x-2])) % MOD;
else ret = (ret + 1ll * dp(x-1,l,r)) % MOD;
if(l==0) ret = (ret + 1ll * dp(x-2,l,r) * ksm(2,fib[x-1])) % MOD;
else ret = (ret + 1ll * dp(x-2,l,r)) % MOD;
for(int i = l; i < r; i++) ret = (ret + 1ll * dp(x-1,l,i) * dp(x-2,i+1,r)) % MOD;
return ret;
}
void solve(){
cin >> n >> x >> s;
fib[0] = fib[1] = 1;
for(int i = 2; i < MAXN; i++) fib[i] = (fib[i-1] + fib[i-2]) % (MOD - 1);
memset(f,255,sizeof(f));
cout << dp(x,0,n-1) << endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
solve();
return 0;
}
G. Almost Increasing Array
假设没有删一个数的条件的话,就是把原序列\(A\)中的所有\(A[i]\)变成\(A[i]-i\),然后算最长非降子序列
现在考虑枚举删掉的数为\(A[k]\),那么对于\(k\)之前的数\(A[i]\)来说,需要减去\(i\),对于\(k\)之后的数\(A[j]\)来说,需要减去\(j-1\)
那么之需要知道以\(k-1\)为结尾的最长非降子序列的长度和大于\(k\)的部分中起始值大于等于\(A[k-1]\)的最长非降子序列即可
那么可以用线段树来维护
view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define endl "\n"
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define scs(s) scanf("%s",s)
#define pii pair<int,int>
#define pll pair<LL,LL>
#ifndef ONLINE_JUDGE
#define cout cerr
#endif
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x) cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
const int MAXN = 4e5+7;
int n, A[MAXN];
struct SegmentTree{
int l[MAXN<<2], r[MAXN<<2], maxx[MAXN<<2];
#define ls(rt) rt << 1
#define rs(rt) rt << 1 | 1
#define pushup(rt) maxx[rt] = max(maxx[ls(rt)],maxx[rs(rt)])
void build(int L, int R, int rt = 1){
l[rt] = L; r[rt] = R;
maxx[rt] = 0;
if(L+1==R) return;
int mid = (L + R) >> 1;
build(L,mid,ls(rt)); build(mid,R,rs(rt));
}
void modify(int pos, int x, int rt = 1){
if(l[rt] + 1 == r[rt]){
maxx[rt] = x;
return;
}
int mid = (l[rt] + r[rt]) >> 1;
if(pos<mid) modify(pos,x,ls(rt));
else modify(pos,x,rs(rt));
pushup(rt);
}
int qmax(int L, int R, int rt = 1){
if(L>=r[rt] or l[rt]>=R) return 0;
if(L<=l[rt] and r[rt]<=R) return maxx[rt];
return max(qmax(L,R,ls(rt)),qmax(L,R,rs(rt)));
}
}ST;
int f[MAXN];
void solve(){
sci(n);
vi vec;
for(int i = 1; i <= n; i++) sci(A[i]), A[i] -= i, vec << A[i] << A[i] + 1;
sort(all(vec)); vec.erase(unique(all(vec)),vec.end());
auto ID = [&](int x){ return lower_bound(all(vec),x) - vec.begin() + 1; };
ST.build(0,vec.size()+1);
for(int i = 1; i <= n; i++){
int x = ID(A[i]);
f[i] = ST.qmax(0,x+1) + 1;
ST.modify(x,f[i]);
}
int ret = f[n-1];
ST.build(0,vec.size()+1);
for(int i = n; i >= 2; i--){
ret = max(ret,f[i-1]+ST.qmax(ID(A[i-1]),vec.size()+1));
int x = ID(A[i]+1);
ST.modify(x,ST.qmax(x,vec.size()+1)+1);
}
cmax(ret,ST.qmax(0,vec.size()+1));
cout << n - 1 - ret << endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
solve();
return 0;
}
Educational Codeforces Round 39的更多相关文章
- Educational Codeforces Round 39 (Rated for Div. 2) G
Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...
- #分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable
2018-03-11 http://codeforces.com/contest/946/problem/D D. Timetable time limit per test 2 seconds me ...
- Educational Codeforces Round 39 (Rated for Div. 2) 946E E. Largest Beautiful Number
题: OvO http://codeforces.com/contest/946/problem/E CF 946E 解: 记读入串为 s ,答案串为 ans,记读入串长度为 len,下标从 1 开始 ...
- Educational Codeforces Round 39 (Rated for Div. 2) B. Weird Subtraction Process[数论/欧几里得算法]
https://zh.wikipedia.org/wiki/%E8%BC%BE%E8%BD%89%E7%9B%B8%E9%99%A4%E6%B3%95 取模也是一样的,就当多减几次. 在欧几里得最初的 ...
- codeforces Educational Codeforces Round 39 (Rated for Div. 2) D
D. Timetable time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Educational Codeforces Round 39 Editorial B(Euclid算法,连续-=与%=的效率)
You have two variables a and b. Consider the following sequence of actions performed with these vari ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
随机推荐
- Solon rpc 之 SocketD 协议 - RPC鉴权模式
Solon rpc 之 SocketD 协议系列 Solon rpc 之 SocketD 协议 - 概述 Solon rpc 之 SocketD 协议 - 消息上报模式 Solon rpc 之 Soc ...
- Linux下安装svn教程
前言 最近买了新服务器,准备开始弄一些个人的开源项目.有了服务器当然是搞一波svn啦.方便自己的资料上传和下载.于是在此记录搭建svn的方式,方便以后直接使用. 安装 使用yum源进行安装,十分的方便 ...
- 十五:SQL注入之oracle,Mangodb注入
Access,Mysql,mssql,mangoDB,postgresql,sqlite,oracle,sybase JSON类型的数据注入: 键名:键值 {"a":"1 ...
- ASP.NET Core错误处理中间件[1]: 呈现错误信息
NuGet包"Microsoft.AspNetCore.Diagnostics"中提供了几个与异常处理相关的中间件.当ASP.NET Core应用在处理请求过程中出现错误时,我们可 ...
- 【Linux】fio测试读写速度
需要安装fio yum install fio -y 有很多依赖包 FIO用法: 随机读:(可直接用,向磁盘写一个2G文件,10线程,随机读1分钟,给出结果) fio -filename=/h ...
- 利用sklearn进行字典&文本的特征提取
写在前面 这篇博客主要内容: 应用DictVectorizer实现对类别特征进行数值化.离散化 应用CountVectorizer实现对文本特征进行数值化 特征提取API sklearn.featur ...
- Angular学习资料大全和常用语法汇总(让后端程序员轻松上手)
前言: 首先为什么要写这样的一篇文章呢?主要是因为前段时间写过一些关于Angualr的相关实战文章,有些爱学习的小伙伴对这方面比较感兴趣,但是又不知道该怎么入手(因为认识我的大多数小伙伴都是后端的同学 ...
- IOC技术在前端项目中的应用
目录 背景 什么是IOC 如何实现一个IOC 第一步:实现一个容器 第二步:用好装饰器 第三步:使用容器 扩展和展望 最后 背景 前端发展至今已经过去30余年,前端应用领域在不断壮大的过程中,也变得越 ...
- Gradle使用及配置
构建工具:Gradle(6.8) 下载地址:https://gradle.org/releases/ 下载最新版的二进制文件即可,下载"gradle-6.8.1-bin.zip文件,下载后完 ...
- 获取网页url中的参数
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...