这次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. JQuery------各种版本下载

    转载: http://www.jq22.com/jquery-info122

  2. 解决Android 6.0(api 23) SDK,不再提供org.apache.http.*

    Eclipse 解决办法 libs中加入 org.apache.http.legacy.jar 上面的jar包在:**\android-sdk\platforms\android-23\optiona ...

  3. iOS UTI(统一类型标识)

    同一类型标识符(Uniform Type Identifier,UTI)代表IOS信息共享的中心组件.可以把它看成下一代的MIME类型.UTI是标识资源类型(比如图像和文本)的字符串,他们制定哪些类型 ...

  4. Tomcat漏洞利用与安全加固实例分析

    Tomcat中间件经常遇到的漏洞: 1.Tomcat默认存在一个管理后台,默认的管理地址是http://IP或域名:端口号/manager/html 2.Axis2默认口令安全漏洞,默认的管理地址是h ...

  5. NSIS安装vcredist_64.exe

    ; ExecWait ‘vcredist_x86.exe’ # 一般的安装ExecWait ‘”vcredist_x86.exe” /q’ # silent install 静默安装; ExecWai ...

  6. flask操作mongo两种方式--常规

    #manage.py #coding=utf-8 #Flask-Script是一个可以在flask应用外部编写脚本的扩展 #常用功能: #运行一个开发的服务器 #python shell中操作数据库看 ...

  7. UITableView-FDTemplateLayoutCell 学习笔记

    本文转载至 http://www.tuicool.com/articles/I7ji2uM 原文  http://everettjf.github.io/2016/03/24/learn-uitabl ...

  8. Hibernate系列之基本配置

    一.概述 Hibernate是一个开放源码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使我们可以使用对象的编程思维来操作数据库. 二.配置准备 IDE:Eclipse 下载Jar包: ...

  9. js - 预加载+监听图片资源加载制作进度条

    这两天遇到一个新需求:一个一镜到底的h5动画.因为功能的特殊性,就要求我们提前监听页面的静态图片是否全部加载完毕.即处理预加载. 总结下来,下次这种需求需要提前注意以下几点: 一.图片而不是背景图 本 ...

  10. LeetCode——Nim Game

    Description: You are playing the following Nim Game with your friend: There is a heap of stones on t ...