codeforces 888A/B/C/D/E - [数学题の小合集]
这次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 a1, a2, ..., an (1 ≤ ai ≤ 1000) — the elements of array a.
Output
Print the number of local extrema in the given array.
Example
3
1 2 3
0
4
1 5 2 5
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
4
LDUR
4
5
RRRUU
0
6
LLRRRR
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
abacaba
2
zzzzz
1
abcde
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
4 1
1
4 2
7
5 3
31
5 4
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 a1, a2, ..., an (1 ≤ ai ≤ 109).
Output
Print the maximum possible value of .
Example
4 4
5 2 4 1
3
3 20
199 41 299
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 - [数学题の小合集]的更多相关文章
- C#的winform小合集
C#的winform小合集 博主很懒,又想记录一下自己的所做所为,仅此而已,供自己日后所看.这个是博主自主学习C#所写的一些小程序,有好玩的,也有一些无聊闲得蛋疼所作的. 内容介绍 C#入门窗口输出h ...
- Codeforces Round #195 A B C 三题合集 (Div. 2)
A 题 Vasily the Bear and Triangle 题目大意 一个等腰直角三角形 ABC,角 ACB 是直角,AC=BC,点 C 在原点,让确定 A 和 B 的坐标,使得三角形包含一个矩 ...
- Linux入门搭建可视化桌面环境小合集virtual box centOS7.10
常用命令: 关联主机与虚拟机(方便文件共享): # sudo mount -t vboxsf share(主机文件夹名) /usr/share(虚拟机内自创) Linux shell进入root模式: ...
- 关于Hive中常用函数需要注意的点小合集
1.COALESCE( value1,value2,... ) The COALESCE function returns the fist not NULL value from the list ...
- DP小合集
1.Uva1625颜色的长度 dp[i][j]表示前一个串选到第i个 后一个串选到第j个 的最小价值 记一下还有多少个没有结束即dp2 记一下每个数开始和结束的位置 #include<cstdi ...
- 微信小程序解决方案合集
微信小程序解决方案合集:http://www.wxapp-union.com/special/solution.html 跳坑系列:http://www.wxapp-union.com/forum.p ...
- 前端,Java,产品经理,微信小程序,Python等资源合集大放送
为了感恩大家长久以来的关注和支持,小编准备了一些福利,整理了包含前端,Java,产品经理,微信小程序,Python,网站源码,Android应用视频教程,微信公众平台开发教程及材料等资源合集大放送. ...
- Cell Phone Networ (树形dp-最小支配集)
目录 Cell Phone Networ (树形dp-最小支配集) 题意 思路 题解 Cell Phone Networ (树形dp-最小支配集) Farmer John has decided to ...
- Codeforces Round #582 (Div. 3)-G. Path Queries-并查集
Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...
随机推荐
- SpringBoot------JPA连接数据库
步骤: 1.在pom.xml文件下添加相应依赖包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=& ...
- 8 -- 深入使用Spring -- 1...4 重写占位符配置器
8.1.5 重写占位符配置器 (PropertyOverrideConfigurer) PropertyOverrideConfigurer是Spring提供的另一个容器后处理器.PropertyOv ...
- mac 卸载idea
卸载MAC中的IDEA Intellij 首先在应用里面右键移动到垃圾桶 然后使用命令行: cd Users/xxx/Library/ 上面的xxx对应你的用户名,然后输入 rm -rf Logs/I ...
- STL——空间配置器(SGI-STL)
一. 空间配置器标准接口 参见<STL源码剖析>第二章-2.1.<memory>文件. 二.具备次配置力的SGI空间配置器 1. SGI STL的配置器与众不同,也与标准规范不 ...
- 【delphi】delphi的TAdoQuery读取Excel数据
1. 连接 需要设置TAdoQuery的连接串Connection,将其指向excel文件: 'Provider=Microsoft.ACE.OLEDB.12.0;Data Source=' + ex ...
- input回车问题
今天有一个问题,就是input对象没有加任何事件自己回车导致跳到了404页面.处理的时候,并发现没找到回车事件的控制. 那么只有一种情况,就是自带的回车控制. 百度了一下,如下面博文里面的写法.我这边 ...
- .net 取得类的属性、方法、成员及通过属性名取得属性值
//自定义的类 model m = new model(); //取得类的Type实例 //Type t = typeof(model); //取得m的Type实例 Type t = m.GetTyp ...
- 【宝塔】 安装扩展Memcached redis 教程
宝塔官网: www.bt.cn 开始安装 1 进入ssh 输入以下指令, wget -O ext.sh http://125.88.182.172:5880/ext/ext.sh && ...
- cxGrid使用汇总4
1. CxGrid汇总功能 ① OptionsView-Footer设置为True,显示页脚 ② CxGrid的Summary选项卡定义要汇总的列和字段名及汇总方式,Footer选项卡定义 ...
- 学了Python可以做什么工作
学了Python可以做什么工作 用 Python 写爬虫 据我所知很多初学 Python 的人都是使用它编写爬虫程序.小到抓取一个小黄图网站,大到一个互联网公司的商业应用.通过 Python 入门爬虫 ...