这次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. PHP基本连接数据库

    最简单的代码 connect.php <?php $host="localhost"; $db_user="root"; $db_pass="& ...

  2. Robot Framework进行web ui自动化测试,浏览器配置说明

    转载请注明出处,谢谢: chrome浏览器: 1.从如下地址下载与本地浏览器版本号一致的chromedriver.exe驱动文件: http://chromedriver.storage.google ...

  3. U3D功能脚本备忘

    编译器属性 属性 介绍 用例 AddComponentMenu 在Component菜单中添加新的菜单项 [AddComponentMenu("Duan/Script/TestScript& ...

  4. Twitter 高并发高可用架构

    解决 Twitter的“问题”就像玩玩具一样,这是一个很有趣的扩展性比喻.每个人都觉得 Twitter很简单,一个菜鸟架构师随便摆弄一下个可伸缩的 Twitter就有了,就这么简单.然而事实不是这样, ...

  5. Repeater的j简单使用

    嘿嘿,今天没有任务,所以突然想起来我之前记得笔 记说要把repeater的使用以及获取值的详细使用总结一下,所以这就闲来无聊总结一下,虽然现在不会使用这些小知识点的,但是我感觉自己的学习还是要 不断地 ...

  6. ip防刷脚本

    #!/bin/sh #防刷脚本 #env ACCESS_PATH=/home/wwwlogs ACCESS_LOG=y.log IPTABLES_TOP_LOG=iptables_top.log DR ...

  7. javascript实现的网页打印

    打印全部页面: <body onload="javascript:window.print()"> 实现局部打印,可以将不参加打印的元素设置“display=none” ...

  8. iOS应用内支付(IAP)的那些坑

    本文转载至 http://blog.devtang.com/2013/04/07/tricks-in-iap/ 前言 udacity 中的在线课程 <How to build a startup ...

  9. PHP域名解析(一个IP绑多域名)----看看可以,并不值得借鉴

    PHP域名解析(一个IP绑多域名)----看看可以,并不值得借鉴 好处当然是不用买多网卡.不用设置其它端口为WEB端口了,一张网卡上.都用同一个80端口建很多网站. 假设有三个域名:     [url ...

  10. Qt——添加动作及对话框

    1. 添加动作 教程:https://www.devbean.net/2012/08/qt-study-road-2-action/ 运行教程中的第一个程序,报错如下: 原因:没有将main.cpp改 ...