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的更多相关文章

  1. 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 < ...

  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 ...

  3. 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 开始 ...

  4. 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 取模也是一样的,就当多减几次. 在欧几里得最初的 ...

  5. 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 ...

  6. Educational Codeforces Round 39 Editorial B(Euclid算法,连续-=与%=的效率)

    You have two variables a and b. Consider the following sequence of actions performed with these vari ...

  7. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  8. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  9. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

随机推荐

  1. Java实现RS485串口通信

    前言 前段时间赶项目的过程中,遇到一个调用RS485串口通信的需求,赶完项目因为楼主处理私事,没来得及完成文章的更新,现在终于可以整理一下当时的demo,记录下来. 首先说一下大概需求:这个项目是机器 ...

  2. Python requirements.txt 语法

    前言 之前一直苦于一个问题,比如一些包在Win上安装不了,比如 uvloop 但是为了提高效率,代码中必须有这个模块 在运行中可以通过 os 模块判断是否使用, 那依赖文件呢? requirement ...

  3. 详解 TCP的三次握手四次挥手

    本文转载来自https://blog.csdn.net/qzcsu/article/details/72861891 背景描述 通过上一篇中网络模型中的IP层的介绍,我们知道网络层,可以实现两个主机之 ...

  4. URL重定向 - Pikachu

    概述: 不安全的url跳转问题可能发生在一切执行了url地址跳转的地方.如果后端采用了前端传进来的(可能是用户传参,或者之前预埋在前端页面的url地址)参数作为了跳转的目的地,而又没有做判断的话就可能 ...

  5. Vue使用Ref跨层级获取组件实例

    目录 Vue使用Ref跨层级获取组件实例 示例介绍 文档目录结构 安装vue-ref 根组件自定义方法[使用provide和inject] 分别说明各个页面 结果 Vue使用Ref跨层级获取组件实例 ...

  6. pycharm2021永久激活

    Pycharm破解版地址: 链接: https://pan.baidu.com/s/1dEkzKRFMaeNjWF4h7y2TdQ 提取码: eqr3  Anaconda地址:版本是python3.6 ...

  7. mybatis源码分析之走进缓存

    之前写了一篇关于mybatis缓存的读后感,想了想还是把缓存模块简单分析一下,附赠下载地址:https://github.com/MyBatis/MyBatis-3,github直接搜排名很靠前的. ...

  8. 前端面试之JavaScript的基本数据类型!

    前端面试之JavaScript的基本数据类型! JS的基本数据类型 数字 字符串 布尔值 JavaScript中有两个特殊的原始值: null (空) 和undefined (未定义), , 它们不是 ...

  9. ValueError: the environment variable is longer than 32767 characters On Windows, an environment variable string ("name=value" string) is limited to 32,767 characters

    https://github.com/python/cpython/blob/aa1b8a168d8b8dc1dfc426364b7b664501302958/Lib/test/test_os.py ...

  10. Pulsar Pub/Sub Messaging

    The Apache Software Foundation Announces Apache Pulsar as a Top-Level Project : The Apache Software ...