Codeforces#277 C,E
1 second
256 megabytes
standard input
standard output
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more
beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n,
the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or
to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n,
the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z'
follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
The first line contains two space-separated integers n (1 ≤ n ≤ 105)
and p (1 ≤ p ≤ n), the length of Nam's
string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
Print the minimum number of presses needed to change string into a palindrome.
8 3
aeabcaez
6
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is:
(cursor
position is shown bold).
In optimal solution, Nam may do 6 following steps:

The result,
,
is now a palindrome.
分成两部分,一是字母的变换,一是位置的移动,仅仅考虑一半就可以。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int maxn=1e5+100;
char str[maxn];
int num[maxn];
int n,pos; int main()
{
std::ios::sync_with_stdio(false);
while(cin>>n>>pos)
{
cin>>(str+1);
CLEAR(num,0);
int ans=0;
REPF(i,1,n/2)
{
if(str[i]!=str[n-i+1])
{
int tt=abs(str[i]-str[n-i+1]);
num[i]=min(tt,26-tt);
num[n-i+1]=min(num[i],26-num[i]);
ans+=num[i];
}
}
int l=n,r=1;
if(pos<=n/2)
{
REPF(i,1,n/2)
{
if(num[i])
{
l=min(l,i);
r=max(r,i);
}
}
}
else
{
REPF(i,n/2+1,n)
{
if(num[i])
{
l=min(l,i);
r=max(r,i);
}
}
}
if(l!=n)
{
if(pos<=l) ans+=r-pos;
else if(pos>=r) ans+=pos-l;
else ans+=min(r-l+r-pos,pos-l+r-l);
}
cout<<ans<<endl;
}
return 0;
}
2 seconds
256 megabytes
standard input
standard output
The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson.
Nam created a sequence a consisting of n (1 ≤ n ≤ 105)
elements a1, a2, ..., an (1 ≤ ai ≤ 105).
A subsequence ai1, ai2, ..., aik where 1 ≤ i1 < i2 < ... < ik ≤ n is
called increasing if ai1 < ai2 < ai3 < ... < aik.
An increasing subsequence is called longest if it has maximum length among all increasing subsequences.
Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 ≤ i ≤ n),
into three groups:
- group of all i such that ai belongs
to no longest increasing subsequences. - group of all i such that ai belongs
to at least one but not every longest increasing subsequence. - group of all i such that ai belongs
to every longest increasing subsequence.
Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this
job.
The first line contains the single integer n (1 ≤ n ≤ 105)
denoting the number of elements of sequence a.
The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 105).
Print a string consisting of n characters. i-th character
should be '1', '2' or '3'
depending on which group among listed above index ibelongs to.
1
4
3
4
1 3 2 5
3223
4
1 5 2 3
3133
In the second sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 3, 2, 5}.
Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a1, a2, a4} = {1, 3, 5} and {a1, a3, a4} = {1, 2, 5}.
In the third sample, sequence a consists of 4 elements: {a1, a2, a3, a4} = {1, 5, 2, 3}.
Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a1, a3, a4} = {1, 2, 3}.
两段LIS。进行推断:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int maxn=1e5+100;
int t1[maxn],t2[maxn],a[maxn],s[maxn];
int ans[maxn],h[maxn],mm;
int main()
{
int n;
std::ios::sync_with_stdio(false);
while(cin>>n)
{
mm=0;
REPF(i,1,n) cin>>a[i];
REPF(i,1,n)
{
s[i]=INT_MAX;
int tt=lower_bound(s+1,s+1+i,a[i])-s;//查找a[i]大于等于的元素的位置
t1[i]=tt;
s[tt]=a[i];
mm=max(mm,tt);
}
for(int i=n;i>=1;i--)
{
s[n-i+1]=INT_MAX;
int tt=lower_bound(s+1,s+n-i+2,-a[i])-s;
t2[i]=tt;
s[tt]=-a[i];
}
CLEAR(h,0);
REPF(i,1,n)
{
if(t1[i]+t2[i]-1<mm) ans[i]=1;
else { ans[i]=2; h[t1[i]]++;}
}
REPF(i,1,n)
{
if(ans[i]==2&&h[t1[i]]==1)
ans[i]=3;
}
REPF(i,1,n)
cout<<ans[i];
cout<<endl;
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Codeforces#277 C,E的更多相关文章
- codeforces 277.5 div2 F:组合计数类dp
题目大意: 求一个 n*n的 (0,1)矩阵,每行每列都只有两个1 的方案数 且该矩阵的前m行已知 分析: 这个题跟牡丹江区域赛的D题有些类似,都是有关矩阵的行列的覆盖问题 牡丹江D是求概率,这个题是 ...
- codeforces#277.5 C. Given Length and Sum of Digits
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- codeforces 277 A Learning Languages 【DFS 】
n个人,每个人会一些语言,两个人只要有会一门相同的语言就可以交流,问为了让这n个人都交流,至少还得学多少门语言 先根据n个人之间他们会的语言,建边 再dfs找出有多少个联通块ans,再加ans-1条边 ...
- Codeforces Round #277 (Div. 2) 题解
Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...
- 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation
题目传送门 /* 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 */ /************************************************ ...
- 【codeforces】Codeforces Round #277 (Div. 2) 解读
门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...
- Codeforces Round #277.5 (Div. 2) ABCDF
http://codeforces.com/contest/489 Problems # Name A SwapSort standard input/output 1 s, 256 ...
- Codeforces Round #277.5 (Div. 2)
题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...
- Codeforces Round #277 (Div. 2) E. LIS of Sequence DP
E. LIS of Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/pr ...
随机推荐
- C#之关于时间的整理
今天在整理C#的异步编程的时候,看到一个Stopwatch类.让我想起了,时候整理一下C#关于时间的类,望补充.斧正. DataTime类 表示时间上的一刻,即某个时间节点,通常以日期和当天的时间表示 ...
- cocos2d-x3.0 解释具体的新的物理引擎setCategoryBitmask()、setContactTestBitmask()、setCollisionBitmask()
转载请注明出处:游戏开发实验室http://blog.csdn.net/u010019717/article/details/32942641 我在编写游戏的时候遇到了这个问题. 物理引擎其它的内容 ...
- Oracle SQL操作计划基线总结(SQL Plan Baseline)
一.基础概念 Oracle 11g開始,提供了一种新的固定运行计划的方法,即SQL plan baseline,中文名SQL运行计划基线(简称基线),能够觉得是OUTLINE(大纲)或者SQL PRO ...
- android内置存储器memory和第三方外部存储disk管理
缓存管理这里 http://blog.csdn.net/intbird/article/details/38338713 图片处理在这里 http://blog.csdn.net/intbird/ar ...
- socket示例代码演示程序(螺纹)
client码,如以下: import java.io.*; import java.net.*; public class DailyAdviceClient { public void go(){ ...
- 《STL源代码分析》---stl_heap.h读书笔记
Heap堆的数据结构是经常使用,Heap它还能够存储元件的.但STL并且不提供Heap集装箱.仅仅提供信息Heap算术运算.只支持RandomAccessIterator该容器可以被用作Heap集装箱 ...
- 批处理删除IIS的everyone、users的访问权限
原文 批处理删除IIS的everyone.users的访问权限 以下批处理代码功能,实现的是,删除C盘的everyone.users用户对IIS的权限. 一.删除C盘的everyone的权限 cd/ ...
- git fetch, merge, pull, push需要注意的地方(转)
在git操作中,我们经常会用到fetch, merge, pull和push等命令,以下是一些我们需要注意的地方. 给大家准备了参考资料: 1. Whatʼs a Fast Forward Merge ...
- adb这点小事——远程adb调试
欢迎转载.转载请注明:http://blog.csdn.net/zhgxhuaa 1. 前言 1.1. 写在前面的话 在之前的一篇文章<360电视助手实现研究>中介绍了在局域网内直接 ...
- Git合并多个Commit
当前有四个commit,现在要将四个commit合并为一个,可以使用git rebase -i HEAD~{这里是要合并的commit数量} 如 git rebase -i HEAD~4 ,即为合并最 ...