这次CF不是很难,我这种弱鸡都能在半个小时内连A四道……不过E题没想到还有这种折半+状压枚举+二分的骚操作,后面就挂G了……

A.Local Extrema

题目链接:https://cn.vjudge.net/problem/CodeForces-888A

You are given an array a. Some element of this array ai is a local minimum iff it is strictly less than both of its neighbours (that is, ai < ai - 1 and ai < ai + 1). Also the element can be called local maximum iff it is strictly greater than its neighbours (that is, ai > ai - 1 and ai > ai + 1). Since a1 and an have only one neighbour each, they are neither local minima nor local maxima.

An element is called a local extremum iff it is either local maximum or local minimum. Your task is to calculate the number of local extrema in the given array.

Input

The first line contains one integer n (1 ≤ n ≤ 1000) — the number of elements in array a.

The second line contains n integers a1a2, ..., an (1 ≤ ai ≤ 1000) — the elements of array a.

Output

Print the number of local extrema in the given array.

Example

Input
3
1 2 3
Output
0
Input
4
1 5 2 5
Output
2

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n,num[],cnt;
bool local_extremum(int a,int b,int c){return (a<b && b>c)||(a>b && b<c);}
int main()
{
cin>>n;
for(int i=;i<=n;i++) scanf("%d",&num[i]);
cnt=;
for(int i=;i<=n-;i++)
{
if(local_extremum(num[i-],num[i],num[i+])) cnt++;
}
cout<<cnt<<endl;
}

B.Buggy Robot

题目链接:https://cn.vjudge.net/problem/CodeForces-888B

Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0, 0). The robot can process commands. There are four types of commands it can perform:

  • U — move from the cell (x, y) to (x, y + 1);
  • D — move from (x, y) to (x, y - 1);
  • L — move from (x, y) to (x - 1, y);
  • R — move from (x, y) to (x + 1, y).

Ivan entered a sequence of n commands, and the robot processed it. After this sequence the robot ended up in the starting cell (0, 0), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations!

Input

The first line contains one number n — the length of sequence of commands entered by Ivan (1 ≤ n ≤ 100).

The second line contains the sequence itself — a string consisting of n characters. Each character can be U, D, L or R.

Output

Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell.

Example

Input
4
LDUR
Output
4
Input
5
RRRUU
Output
0
Input
6
LLRRRR
Output
4

题意:

机器人可以Up,Down,Left,Right四个方向移动一格;

现在给出一系列移动指令,因为规定了机器人肯定能在执行完这一系列指令后能回到原点,所以着一系列指令中必然有些是无效指令;

求最大有效指令是多少个;

题解:

一个U跟一个D抵消,一个L和一个R抵消,对它们计数一下在再计算一下即可。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n,cnt[];
char mov[];
int main()
{
cin>>n;
scanf("%s",mov+);
memset(cnt,,sizeof(cnt));
for(int i=;i<=n;i++)
{
if(mov[i]=='U') cnt[]++;
else if(mov[i]=='D') cnt[]++;
else if(mov[i]=='L') cnt[]++;
else if(mov[i]=='R') cnt[]++;
}
int ans=*min(cnt[],cnt[])+*min(cnt[],cnt[]);
cout<<ans<<endl;
}

C.K-Dominant Character

题目链接:https://cn.vjudge.net/problem/CodeForces-888C

You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant iff each substring of s with length at least k contains this character c.

You have to find minimum k such that there exists at least one k-dominant character.

Input

The first line contains string s consisting of lowercase Latin letters (1 ≤ |s| ≤ 100000).

Output

Print one number — the minimum value of k such that there exists at least one k-dominant character.

Example

Input
abacaba
Output
2
Input
zzzzz
Output
1
Input
abcde
Output
3

题意:

给定一个小写字母串s,对某个字母c,如果对于s的所有长度为k的子串,都含有c,就称字符c为k-Dominant;

对字符串中的所有字母,求其k,求其中最小的。

题解:

求出字符串s中某个字符c的前后差距的最大值,即k;

例如:abacaba中的字符a;

s[1]='a',与前面(位置为0)差距为1(1-0=0),与后面的a差距为2(3-1=2);

s[3]='a',与前面(位置为1)差距为2(3-1=2),与后面的a差距为2(5-3=2);

……

s[7]='a',与前面(位置为5)差距为2(7-5=2),与后面差距(len+1)为1(7+1-7=1);

所以字符a对应的k为2;

对字符串s中每个字符都算出k,取其中最小的即可。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int inte[],last[];
char str[+];
int main()
{
scanf("%s",str+);
int len=strlen(str+);
memset(inte,,sizeof(inte));
memset(last,,sizeof(last));
for(int i=;i<=len;i++)
{
int id=str[i]-'a';
inte[id]=max(inte[id],i-last[id]);
last[id]=i;
}
for(int i=;i<;i++)
{
if(inte[i]==) continue;
inte[i]=max(inte[i],len+-last[i]);
} int ans=0x3f3f3f3f;
for(int i=;i<;i++)
{
if(inte[i]==) continue;
ans=min(ans,inte[i]);
}
cout<<ans<<endl;
}

D.Almost Identity Permutations

题目链接:https://cn.vjudge.net/problem/CodeForces-888D

A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in this array.

Let's call a permutation an almost identity permutation iff there exist at least n - k indices i (1 ≤ i ≤ n) such that pi = i.

Your task is to count the number of almost identity permutations for given numbers n and k.

Input

The first line contains two integers n and k (4 ≤ n ≤ 1000, 1 ≤ k ≤ 4).

Output

Print the number of almost identity permutations for given n and k.

Example

Input
4 1
Output
1
Input
4 2
Output
7
Input
5 3
Output
31
Input
5 4
Output
76

题意:

对于一个1~n的序列,给定一个k,如果它的全排列中某一个排列,它至少有n-k个pi=i,就称其为almost identity permutation;

求almost identity permutation数目。

题解:

观察到1<=k<=4,所以我们可以分而治之;

先比如求出 n-1个pi=i 的排列有几个,在求n-2的,直到n-k,在全部加起来即可;

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n,k;
long long ans;
long long C[][];
void calc_Cmn()//求组合数
{
for(int i=;i<;i++)
{
C[i][]=C[i][i]=;
for(int j=;j<i;j++) C[i][j]=C[i-][j-]+C[i-][j];
}
}
int main()
{
calc_Cmn();
cin>>n>>k;
for(int i=n-;i>=n-k;i--)
{
if(i==n-) ans+=;
if(i==n-) ans+=C[n][i];
if(i==n-) ans+=C[n][i]*;
if(i==n-) ans+=C[n][i]*;
}
cout<<ans<<endl;
}

E.Maximum Subsequence

题目链接:https://cn.vjudge.net/problem/CodeForces-888E

You are given an array a consisting of n integers, and additionally an integer m. You have to choose some sequence of indices b1, b2, ..., bk (1 ≤ b1 < b2 < ... < bk ≤ n) in such a way that the value of  is maximized. Chosen sequence can be empty.

Print the maximum possible value of .

Input

The first line contains two integers n and m (1 ≤ n ≤ 35, 1 ≤ m ≤ 109).

The second line contains n integers a1a2, ..., an (1 ≤ ai ≤ 109).

Output

Print the maximum possible value of .

Example

Input
4 4
5 2 4 1
Output
3
Input
3 20
199 41 299
Output
19

题解:

拆成两半,枚举前半部分的所有状态,算出sum,存起来,记为l_sum[];

枚举后半部分状态,每算出一个sum,去l_sum[]里二分查找能和它加起来最大的,又不会超过m-1的;

AC代码:

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int n,mid;
ll num[],MOD;
ll l_sum[]; int _size=; ll bisearch(ll limit)//在l_sum[]中查找小于等于limit的最大值
{
//printf("limit is %lld\n",limit);
int l=,r=_size-,mid;
while(l<=r)
{
mid=(l+r)/;
//printf("now l=%d r=%d %lld\n",l,r,l_sum[mid]);
if(l_sum[mid]==limit) return limit; if(l_sum[mid]>limit) r=mid-;
else l=mid+;
}
return l_sum[l-];
}
int main()
{
cin>>n>>MOD;
mid=n/;
for(int i=;i<=n;i++) cin>>num[i],num[i]%=MOD; for(int state=;state<(<<mid);state++)
{
ll sum=;
for(int cnt=,sta=state;sta>;cnt++)
{
sum+=(sta&)*num[cnt];
sta=(sta>>);
}
l_sum[_size++]=sum%MOD;
}
sort(l_sum,l_sum+_size);
_size=unique(l_sum,l_sum+_size)-l_sum;
//for(int i=0;i<_size;i++) cout<<l_sum[i]<<endl;cout<<endl; ll ans=-;
for(int state=;state<(<<(n-mid));state++)
{
ll sum=;
for(int cnt=mid+,sta=state;sta>;cnt++)
{
sum+=(sta&)*num[cnt];
sta=(sta>>);
}
sum%=MOD;
ans=max(ans,sum+bisearch(MOD--sum)); if(ans==MOD-) break;
} cout<<ans<<endl;
}

codeforces 888A/B/C/D/E - [数学题の小合集]的更多相关文章

  1. C#的winform小合集

    C#的winform小合集 博主很懒,又想记录一下自己的所做所为,仅此而已,供自己日后所看.这个是博主自主学习C#所写的一些小程序,有好玩的,也有一些无聊闲得蛋疼所作的. 内容介绍 C#入门窗口输出h ...

  2. Codeforces Round #195 A B C 三题合集 (Div. 2)

    A 题 Vasily the Bear and Triangle 题目大意 一个等腰直角三角形 ABC,角 ACB 是直角,AC=BC,点 C 在原点,让确定 A 和 B 的坐标,使得三角形包含一个矩 ...

  3. Linux入门搭建可视化桌面环境小合集virtual box centOS7.10

    常用命令: 关联主机与虚拟机(方便文件共享): # sudo mount -t vboxsf share(主机文件夹名) /usr/share(虚拟机内自创) Linux shell进入root模式: ...

  4. 关于Hive中常用函数需要注意的点小合集

    1.COALESCE( value1,value2,... ) The COALESCE function returns the fist not NULL value from the list ...

  5. DP小合集

    1.Uva1625颜色的长度 dp[i][j]表示前一个串选到第i个 后一个串选到第j个 的最小价值 记一下还有多少个没有结束即dp2 记一下每个数开始和结束的位置 #include<cstdi ...

  6. 微信小程序解决方案合集

    微信小程序解决方案合集:http://www.wxapp-union.com/special/solution.html 跳坑系列:http://www.wxapp-union.com/forum.p ...

  7. 前端,Java,产品经理,微信小程序,Python等资源合集大放送

    为了感恩大家长久以来的关注和支持,小编准备了一些福利,整理了包含前端,Java,产品经理,微信小程序,Python,网站源码,Android应用视频教程,微信公众平台开发教程及材料等资源合集大放送. ...

  8. Cell Phone Networ (树形dp-最小支配集)

    目录 Cell Phone Networ (树形dp-最小支配集) 题意 思路 题解 Cell Phone Networ (树形dp-最小支配集) Farmer John has decided to ...

  9. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

随机推荐

  1. Oracle批量执行SQL语句

    SQLServer的场合,用";"分割SQL语句即可正常执行. Oracle的场合,会报ORA-00911错误.Oracle中需要加上begin end才正确. Dim Sql A ...

  2. MongoDB聚合管道

    通过上一篇文章中,认识了MongoDB中四个聚合操作,提供基本功能的count.distinct和group,还有可以提供强大功能的mapReduce. 在MongoDB的2.2版本以后,聚合框架中多 ...

  3. zabbix添加对tomcat线程池的监控

    在zabbix模板中添加以下监控项: 可以参考文档:http://www.fblinux.com/?p=616

  4. 通过RF数据库查询中文字段结果正常显示的转换方法

    方法1:统一显示为中文 1.通过RF数据库查询中文字段结果格式:'\xba\xcb\xbc\xf5\xcd\xa8\xb9\xfd' 2.通过Decode Bytes To String进行gbk解码 ...

  5. 使用 urllib 设置代理服务

    (1) 如果我们一直用同一个IP去请求同一个网站上的网页,久了之后可能会被该网站服务器屏蔽,因此我们可以使用代理IP来发起请求,代理实际上指的就是代理服务器(2) 当我们使用代理IP发起请求时,服务器 ...

  6. Kafka Java API操作topic

    Kafka官方提供了两个脚本来管理topic,包括topic的增删改查.其中kafka-topics.sh负责topic的创建与删除:kafka-configs.sh脚本负责topic的修改和查询,但 ...

  7. win10找回Windows照片查看器

  8. (原)一句mpAudioPolicy->get_input引发的血案

    今天分析Android的Audio系统时,对mpAudioPolicy->get_input进行了分析,没想到这一句话的背后如此复杂,简直是一句话引出的血案啊! 分析结果如下:(关于排版:各个变 ...

  9. PHP 中文字符串截取

    $str = "abcdef啊啊吧啊"; function my_sub($str, $st ,$len){ $ret = ""; for( $st; $len ...

  10. read by other session 等待事件。

    今天是2014-01-06,从今天开始,打算春节之前每天学习一个等待事件,今天就记录一下read by other session这个等待事件笔记. 什么是read by other session? ...