C. Palindrome Transformation
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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?

Input

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.

Output

Print the minimum number of presses needed to change string into a palindrome.

Sample test(s)
input
  1. 8 3
  2. aeabcaez
output
  1. 6
Note

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.

分成两部分,一是字母的变换,一是位置的移动,仅仅考虑一半就可以。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<vector>
  6. typedef long long LL;
  7. using namespace std;
  8. #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
  9. #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
  10. #define CLEAR( a , x ) memset ( a , x , sizeof a )
  11. const int maxn=1e5+100;
  12. char str[maxn];
  13. int num[maxn];
  14. int n,pos;
  15.  
  16. int main()
  17. {
  18. std::ios::sync_with_stdio(false);
  19. while(cin>>n>>pos)
  20. {
  21. cin>>(str+1);
  22. CLEAR(num,0);
  23. int ans=0;
  24. REPF(i,1,n/2)
  25. {
  26. if(str[i]!=str[n-i+1])
  27. {
  28. int tt=abs(str[i]-str[n-i+1]);
  29. num[i]=min(tt,26-tt);
  30. num[n-i+1]=min(num[i],26-num[i]);
  31. ans+=num[i];
  32. }
  33. }
  34. int l=n,r=1;
  35. if(pos<=n/2)
  36. {
  37. REPF(i,1,n/2)
  38. {
  39. if(num[i])
  40. {
  41. l=min(l,i);
  42. r=max(r,i);
  43. }
  44. }
  45. }
  46. else
  47. {
  48. REPF(i,n/2+1,n)
  49. {
  50. if(num[i])
  51. {
  52. l=min(l,i);
  53. r=max(r,i);
  54. }
  55. }
  56. }
  57. if(l!=n)
  58. {
  59. if(pos<=l) ans+=r-pos;
  60. else if(pos>=r) ans+=pos-l;
  61. else ans+=min(r-l+r-pos,pos-l+r-l);
  62. }
  63. cout<<ans<<endl;
  64. }
  65. return 0;
  66. }
E. LIS of Sequence
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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:

  1. group of all i such that ai belongs
    to no longest increasing subsequences.
  2. group of all i such that ai belongs
    to at least one but not every longest increasing subsequence.
  3. 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.

Input

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

Output

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.

Sample test(s)
input
  1. 1
  2. 4
output
  1. 3
input
  1. 4
  2. 1 3 2 5
output
  1. 3223
input
  1. 4
  2. 1 5 2 3
output
  1. 3133
Note

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。进行推断:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<limits.h>
  6. typedef long long LL;
  7. using namespace std;
  8. #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
  9. #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
  10. #define CLEAR( a , x ) memset ( a , x , sizeof a )
  11. const int maxn=1e5+100;
  12. int t1[maxn],t2[maxn],a[maxn],s[maxn];
  13. int ans[maxn],h[maxn],mm;
  14. int main()
  15. {
  16. int n;
  17. std::ios::sync_with_stdio(false);
  18. while(cin>>n)
  19. {
  20. mm=0;
  21. REPF(i,1,n) cin>>a[i];
  22. REPF(i,1,n)
  23. {
  24. s[i]=INT_MAX;
  25. int tt=lower_bound(s+1,s+1+i,a[i])-s;//查找a[i]大于等于的元素的位置
  26. t1[i]=tt;
  27. s[tt]=a[i];
  28. mm=max(mm,tt);
  29. }
  30. for(int i=n;i>=1;i--)
  31. {
  32. s[n-i+1]=INT_MAX;
  33. int tt=lower_bound(s+1,s+n-i+2,-a[i])-s;
  34. t2[i]=tt;
  35. s[tt]=-a[i];
  36. }
  37. CLEAR(h,0);
  38. REPF(i,1,n)
  39. {
  40. if(t1[i]+t2[i]-1<mm) ans[i]=1;
  41. else { ans[i]=2; h[t1[i]]++;}
  42. }
  43. REPF(i,1,n)
  44. {
  45. if(ans[i]==2&&h[t1[i]]==1)
  46. ans[i]=3;
  47. }
  48. REPF(i,1,n)
  49. cout<<ans[i];
  50. cout<<endl;
  51. }
  52. return 0;
  53. }

版权声明:本文博客原创文章,博客,未经同意,不得转载。

Codeforces#277 C,E的更多相关文章

  1. codeforces 277.5 div2 F:组合计数类dp

    题目大意: 求一个 n*n的 (0,1)矩阵,每行每列都只有两个1 的方案数 且该矩阵的前m行已知 分析: 这个题跟牡丹江区域赛的D题有些类似,都是有关矩阵的行列的覆盖问题 牡丹江D是求概率,这个题是 ...

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

  3. codeforces 277 A Learning Languages 【DFS 】

    n个人,每个人会一些语言,两个人只要有会一门相同的语言就可以交流,问为了让这n个人都交流,至少还得学多少门语言 先根据n个人之间他们会的语言,建边 再dfs找出有多少个联通块ans,再加ans-1条边 ...

  4. Codeforces Round #277 (Div. 2) 题解

    Codeforces Round #277 (Div. 2) A. Calculating Function time limit per test 1 second memory limit per ...

  5. 贪心+构造 Codeforces Round #277 (Div. 2) C. Palindrome Transformation

    题目传送门 /* 贪心+构造:因为是对称的,可以全都左一半考虑,过程很简单,但是能想到就很难了 */ /************************************************ ...

  6. 【codeforces】Codeforces Round #277 (Div. 2) 解读

    门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...

  7. Codeforces Round #277.5 (Div. 2) ABCDF

    http://codeforces.com/contest/489 Problems     # Name     A SwapSort standard input/output 1 s, 256 ...

  8. Codeforces Round #277.5 (Div. 2)

    题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...

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

随机推荐

  1. SQL Server 连接问题-TCP/IP

    原文:SQL Server 连接问题-TCP/IP 出自:http://blogs.msdn.com/b/apgcdsd/archive/2012/02/24/ms-sql-server-tcp-ip ...

  2. Finding awesome developers in programming interviews(转)

    英文原文:Finding awesome developers in programming interviews 我曾在一次面试中要求一个很有经验的嵌入式软件开发人员写出一个反转一段字符串并输出到屏 ...

  3. Computer Science 学习第四章--CPU 指令集和指令处理

    Instruction set Y86 指令集 运算符:addl, subl, andl, and xorl 跳转符:jmp,jle,jl,je,jne,jge, andjg 条件符:cmovle, ...

  4. C#程序(含多个Dll)合并成一个Exe

    把C#程序(含多个Dll)合并成一个Exe的超简单方法   开发程序的时候经常会引用一些第三方的DLL,然后编译生成的exe文件就不能脱离这些DLL独立运行了. 但是,很多时候我们本想开发一款只需要一 ...

  5. Java 新特性(2) - JDK6 新特性

    http://freesea.iteye.com/blog/160133 JDK6的新特性之一_Desktop类和SystemTray类 JDK6的新特性之二_使用JAXB2来实现对象与XML之间的映 ...

  6. socket-详细分析No buffer space available(转)

    新年上班第一天,突然遇到一个socket连接No buffer space available的问题,导致接口大面积调用(webservice,httpclient)失败的问题,重启服务器后又恢复了正 ...

  7. Linux Kernel系列 - 黄牛X内核代码凝视

    Hanks.Wang - 专注于操作系统与移动安全研究.Linux-Kernel/SELinux/SEAndroid/TrustZone/Encription/MDM    Mail - byhank ...

  8. Oracle 数据恢复指导具体解释

    1.数据恢复指导 : 高速检測.分析和修复故障 最大程度地降低停机故障和执行时故障 将对用户的干扰降到最低 用户界面:    --EM GUI 界面 (多个路径)    --RMAN 命令行 支持的数 ...

  9. 写自己的第二级处理器(3)——Verilog HDL行为语句

    我们会继续上传新书<自己动手写处理器>(未公布),今天是第七章,我每星期试试4 2.6 Verilog HDL行为语句 2.6.1 过程语句 Verilog定义的模块一般包含有过程语句,过 ...

  10. react.js 从零开始(七)React (虚拟)DOM

    React 元素 React 中最主要的类型就是 ReactElement.它有四个属性:type,props,key 和ref.它没有方法,并且原型上什么都没有. 可以通过 React.create ...