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)\),满足 ...
随机推荐
- C# FileSystemWatcher 在监控文件夹和文件时的用法
概述 最近学习FileSystemWatcher的用法,它主要是监控一个文件夹,当文件夹内的文件要是有更改就要记录下来,我就整理下我对FileSystemWatcher 的理解和用法. FileSys ...
- php中实现记住密码下次自动登录的例子
这篇文章主要介绍了php中实现记住密码下次自动登录的例子,本文使用cookie实现记住密码和自动登录功能,需要的朋友可以参考下 做网站的时候经常会碰到要实现记住密码,下次自动登录,一周内免登陆,一个月 ...
- C#------如何深度克隆一个对象
普通版: public static object CloneObject( object obj ) { using ( MemoryStream memStream = new MemoryStr ...
- Dubbo -- 系统学习 笔记 -- 示例 -- 直连提供者
Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 直连提供者 在开发及测试环境下,经常需要绕过注册中心,只测试指定服务提供者,这时候 ...
- WinForm中实现HotKey
最近在写一个游戏辅助工具,来点Win变成的总结 主要用了RegisterHotKey:UnregisterHotKey:两个winAPI 以下代码来自stackoverflow新增了一个HotKeyM ...
- 深入理解磁盘文件系统之inode
一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB). 操作系统 ...
- Robot Framework进行web ui自动化测试,浏览器配置说明
转载请注明出处,谢谢: chrome浏览器: 1.从如下地址下载与本地浏览器版本号一致的chromedriver.exe驱动文件: http://chromedriver.storage.google ...
- nsi脚本中执行.bat文件要隐藏dos窗口问题
问题原因:工作中,在一个nsi的安装脚本文件中需要安装虚拟摄像头驱动,安装驱动脚本是.bat文件.使用nsi的execwait函数执行.bat文件时会显示dos窗口.但是领导要求不能显示dos窗口. ...
- Android学习之Handler消息
Android系统规定,一些耗时的操作不能放在UI线程中去执行,这样会报一个ANR错误.所以为了避免该问题,我们需要开启一个新的线程去执行一些耗时操作:开启新的线程,将耗时的操作在新线程里面去执行, ...
- shell 脚本调试
1.第一行加 -xv #!/bin/bash –xv 2. bash -x shellName 3.如果只想调试其中几行脚本的话可以用 set -x 和 set +x 把要调试的部分包含进来: 比如: ...