Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/gym/100114
Description
When selecting files in an application dialog, Vasya noted that he can get the same selection in different ways. A simple mouse click selects a single file (the existing selection is discarded). A shift-click is used to select a range of files from the file clicked last time to the current file (the existing selection is discarded). Finally, a control-click is used to invert the selection state of a single file. Consider a sequence of actions. First we select file #5 simply by clicking it. Then, shift-clicking file #10 we get the following selection: #5, #6, #7, #8, #9, #10. If after that we control-click files #7 and #3 then we will have files #3, #5, #6, #8, #9, and #10 selected. Shift-clicking file #1 we select files #1, #2, and #3 (last time we clicked file #3, and the previous selection is gone). Vasya is wondering, what the minimum number of clicks will be, to make a certain selection from the list of files. Write a program to determine the optimal way of making the required selection. If there are several minimal solutions, any of them is considered correct. Example. Suppose we are to select files #2, #5, #6, #8, #9 from a list of 10 files. A possible optimal solution will include the following clicks: 5, Shift+9, Ctrl+2, Ctrl+7.
Input
Output
Sample Input
10 .*..**.**.
Sample Output
HINT
题意
有3种操作
1.光标移动到X
2.shift X,直接选择从光标到X的位置
3.选择X,如果X已经被选中,那就取消X的选中状态
问你最少多少步,可以选择所有的*
并且把步骤输出
题解:
注意,只能shift 1次,所以直接扫一遍就好了
跑一遍线段树维护的DP,表示到这儿,所需要的最小代价是多少
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
#include <bitset>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1) using namespace std;
const int maxn = 1e5 + ;
char str[maxn];
int length , sum[maxn], dp[maxn];
vector<int>Q,Q2; struct operation
{
int x;
int type;
}; operation nxt[maxn]; struct QueryData
{
int minv , minpos;
QueryData(int minv , int minpos)
{
this->minv = minv , this->minpos = minpos;
}
}; typedef int SgTreeDataType;
struct treenode
{
int L , R ;
SgTreeDataType minv , minpos;
void updata(SgTreeDataType v)
{
minv = v;
}
}; treenode tree[maxn * ]; inline void push_up(int o)
{
if(tree[o*].minv > tree[o*+].minv)
{
tree[o].minv = tree[o*+].minv;
tree[o].minpos = tree[o*+].minpos;
}
else
{
tree[o].minv = tree[o*].minv;
tree[o].minpos = tree[o*].minpos;
}
} inline void build_tree(int L , int R , int o)
{
tree[o].L = L , tree[o].R = R,tree[o].minv = << , tree[o].minpos = ;
if(L == R) tree[o].minpos = L;
if (R > L)
{
int mid = (L+R) >> ;
build_tree(L,mid,o*);
build_tree(mid+,R,o*+);
}
} inline void updata(int QL,int QR,SgTreeDataType v,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) tree[o].updata(v);
else
{
int mid = (L+R)>>;
if (QL <= mid) updata(QL,QR,v,o*);
if (QR > mid) updata(QL,QR,v,o*+);
push_up(o);
}
} inline QueryData query(int QL,int QR,int o)
{
int L = tree[o].L , R = tree[o].R;
if (QL <= L && R <= QR) return QueryData(tree[o].minv,tree[o].minpos);
else
{
int mid = (L+R)>>;
if (QL <= mid && QR > mid)
{
QueryData a = query(QL,QR,*o);
QueryData b = query(QL,QR,*o+);
if(a.minv < b.minv) return a;
else return b;
}
else if (QL <= mid) return query(QL,QR,*o);
else return query(QL,QR,*o+);
}
} void initiation()
{
memset( dp , , sizeof(dp));
scanf("%d%s",&length,str+);sum[] = ;
for(int i = ; i <= length ; ++ i)
{
sum[i] = sum[i-];
if(str[i] == '*')
{
Q.push_back(i);
}
else
{
sum[i] ++ ;
Q2.push_back(i);
}
}
} void solve()
{
int sz = Q.size();
int ansL = Q[],ansR = Q[],ans=sz;
build_tree( , sz - , );
for(int i = ; i < sz ; ++ i) updata( i , i , sum[Q[i]] - i , );
for(int i = ; i < sz - ; ++ i)
{
QueryData y = query( i + , sz - , );
int newans = i + + sz + y.minv - sum[Q[i]];
if(newans < ans)
{
ans = newans;
ansL = i;
ansR = y.minpos;
}
}
printf("%d\n",ans);
if(ansL != ansR)
{
printf("%d\n",Q[ansL]);
printf("Shift+%d\n",Q[ansR]);
for(int i = ; i < sz ; ++ i) if(i < ansL || i > ansR) printf("Ctrl+%d\n",Q[i]);
for(int i = ; i < Q2.size() ; ++ i) if( Q2[i] < Q[ansR] && Q2[i] > Q[ansL]) printf("Ctrl+%d\n",Q2[i]);
}
else
{
printf("%d\n",Q[]);
for(int i = ; i < sz ; ++ i) printf("Ctrl+%d\n",Q[i]);
}
} int main(int argc,char *argv[])
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
initiation();
if(Q.size() == ) printf("0\n");
else if(Q.size() == ) printf("1\n%d\n",Q[]);
else if(Q.size() == ) printf("2\n%d\nCtrl+%d\n",Q[],Q[]);
else solve();
return ;
}
Codeforces GYM 100114 D. Selection 线段树维护DP的更多相关文章
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp
D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...
- Codeforces 834D The Bakery 【线段树优化DP】*
Codeforces 834D The Bakery LINK 题目大意是给你一个长度为n的序列分成k段,每一段的贡献是这一段中不同的数的个数,求最大贡献 是第一次做线段树维护DP值的题 感觉还可以, ...
- [Codeforces]817F. MEX Queries 离散化+线段树维护
[Codeforces]817F. MEX Queries You are given a set of integer numbers, initially it is empty. You sho ...
- Codeforces 1383E - Strange Operation(线段树优化 DP or 单调栈+DP)
Codeforces 题目传送门 & 洛谷题目传送门 Yet another 自己搞出来的难度 \(\ge 2800\) 的题 介绍一个奇奇怪怪的 \(n\log n\) 的做法.首先特判掉字 ...
- 【BZOJ2164】采矿 树链剖分+线段树维护DP
[BZOJ2164]采矿 Description 浩浩荡荡的cg大军发现了一座矿产资源极其丰富的城市,他们打算在这座城市实施新的采矿战略.这个城市可以看成一棵有n个节点的有根树,我们把每个节点用1到n ...
- 【8.26校内测试】【重构树求直径】【BFS模拟】【线段树维护DP】
题目性质比较显然,相同颜色联通块可以合并成一个点,重新建树后,发现相邻两个点的颜色一定是不一样的. 然后发现,对于一条链来说,每次把一个点反色,实际上使点数少了2个.如下图 而如果一条链上面有分支,也 ...
- 2019牛客暑期多校训练营(第二场)E 线段树维护dp转移矩阵
题意 给一个\(n\times m\)的01矩阵,1代表有墙,否则没有,每一步可以从\(b[i][j]\)走到\(b[i+1][j]\),\(b[i][j-1]\),\(b[i][j+1]\),有两种 ...
随机推荐
- .NET Framework个版本说明
.NET Framework .NET版本 1.0 1.1 2.0 3.0 3.5 4.0 4.5 完整版本 1.0.3705.0 1.1.4322.573 2.0.50727.42 3.0.4506 ...
- Android-给另一个Activity传递HashMap
I have a HashMap which I would pass to another Activity class. I simply use this code: Intent intent ...
- fastdb中的位图应用
位图内存管理: 每块内存用一个二进制位表示它的使用状态,如果该块内存被占用,则把对应位图中的对应位置1,如果空闲则置0,原理十分简单.计算机里面处理的位数最少的变量是字节(byte),所以也就是8位做 ...
- JAX-WS
JAX-WS(Java API for XML Web Services)规范是一组XML web services的JAVA API,JAX-WS允许开发者可以选择RPC-oriented或者mes ...
- Winform使用DevExpress的WaitDialogForm画面 z
使用了DevExpress的WaitDialogForm 在应用程序加载开始时新建一个线程,并将loading画面show起来,在应用程序画面弹出前将该线程终止. 代码: private DevExp ...
- Slalom
题意: 有n个宽度为w的门,给出门的左端点的水平位置x和高度y,和恒定的垂直速度,现有s个速度,求能通过这n个门的最大速度. 分析: 二分速度判断 #include <map> #incl ...
- 【剑指offer 面试题13】在 O(1) 时间删除链表结点
#include <iostream> using namespace std; //构造链表结点 struct ListNode { int val; ListNode *next; L ...
- 仿酷狗音乐播放器开发日志二十六 duilib在标题栏弹出菜单的方法
转载请说明原出处,谢谢~~ 上篇日志说明了怎么让自定义控件响应右键消息.之后我给主窗体的标题栏增加右键响应,观察原酷狗后可以发现,在整个标题栏都是可以响应右键并弹出菜单的.应该的效果如下: 本以为像上 ...
- Leave Morningstar
Today, closed 4++ years developer life (2009.11.17-2014.02.28) in MorningStar Shenzhen Office Austra ...
- 【开发流程】土耳其CDD工具
土耳其的CDD工具开发到现在已经三个星期了,进展非常不顺利,从最开始的遇到很多难题没法解决到现在工具可以运行但是有一个bug. 起初遇到的ExternalUtranCell这一页的信息很难填,没有解决 ...