First Training
B
B - Local Extrema
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.
Examples
3
1 2 3
0
4
1 5 2 5
2
签到题1
#include<bits/stdc++.h>
using namespace std;
const int N=1E4+;
int arr[N];
int main(){
int n;
cin>>n;
for(int i=;i<=n;i++) scanf("%d",&arr[i]);
int ans=;
for(int i=;i<=n-;i++){
if(arr[i]>arr[i-]&&arr[i]>arr[i+]||arr[i]<arr[i-]&&arr[i]<arr[i+]) ans++;
}
cout<<ans<<endl;
return ;
}
签到题2 由于机器人最终在起点,所以L=R, U=D ,答案为2*min(L,R)+2*min(U,D)
#include<bits/stdc++.h>
using namespace std;
const int N=1E3+;
int L=,R=,U=,D=;
int main(){
int n;
cin>>n;
string x;
cin>>x;
for(int i=;i<n;i++){
if(x[i]=='L') L++;
else if(x[i]=='R') R++;
else if(x[i]=='U') U++;
else if(x[i]=='D') D++;
}
int ans1=min(L,R);
int ans2=min(U,D);
cout<<*ans1+*ans2<<endl;
return ;
}
题解:记录字符的出现位置,对于相同的字符,间隔去最大值(不要忘了边界),不同字符去最小
#include<bits/stdc++.h>
using namespace std;
const int N=1E5+;
const int M=1e9+; char arr[N];
int brr[N];
vector<int >ve[N];
bool mark[N]; int main(){
memset(mark,,sizeof(mark));
scanf("%s",arr+);
int n=strlen(arr+);
int pos=;
for(int i=;i<=n;i++){
int y=arr[i];
if(!mark[y]) {
mark[y]=;
brr[pos++]=y;
}
ve[y].push_back(i);
}
int ans=M,k;
for(int i=;i<pos;i++){
int y=brr[i];
int x=ve[y].size();
if(x==){
k=max(ve[y][],n-ve[y][]+);
}
else {
for(int j=;j<x;j++){
if(j==) k=ve[y][j];
else {
k=max(k,ve[y][j]-ve[y][j-]);
}
}
k=max(k,n-ve[y][x-]+);
}
ans=min(ans,k);
}
cout<<ans<<endl;
return ;
}
G - Almost Identity Permutations
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.
Examples
4 1
1
4 2
7
5 3
31
5 4
76
当K==1 的时候,直接输出1,
当K=2的时候哦 答案为c(n,2);
当k=3的时候答案为c(n,3)*2;(这里的2是怎么来的?由于我们选了3个数,即这3个数不能再原位置,比如1,2,3这三个数,要是其下标不等于其值,只能是3 1 2或者2 3 1共两种)
当k==4的时候答案为c(n,4)*(9)(这里的9与上同理)
然后在累加,即 k=2的最终结果等于k=1+k=2
k=3的最终结果等于k=3 + k=2 + k=1
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll n,k;
cin>>n>>k;
ll ans=;
for(int i=;i<=k;i++){
if(i==){
ans+=n*(n-)/;
}
else if(i==){
ans+=n*(n-)*(n-)/ *;
}
else if(i==){
ans+=n*(n-)*(n-)*(n-)/ *;
}
}
ans++;
cout<<ans<<endl;
return ;
}
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.
Now she has a table filled with integers. The table consists of n rows and m columns. By ai, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if ai, j ≤ ai + 1, j for all i from 1 to n - 1.
Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that ai, j ≤ ai + 1, j for all i from l to r - 1 inclusive.
Alyona is too small to deal with this task and asks you to help!
Input
The first line of the input contains two positive integers n and m (1 ≤ n·m ≤ 100 000) — the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.
Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for ai, j (1 ≤ ai, j ≤ 109).
The next line of the input contains an integer k (1 ≤ k ≤ 100 000) — the number of task that teacher gave to Alyona.
The i-th of the next k lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n).
Output
Print "Yes" to the i-th line of the output if the table consisting of rows from li to ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No".
Example
5 4
1 2 3 5
3 1 3 2
4 5 2 3
5 5 3 2
4 4 3 4
6
1 1
2 5
4 5
3 5
1 3
1 5
Yes
No
Yes
Yes
Yes
No
Note
In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
题解:我们开一个数组d,用来记录存在某一列使得第从l行到 r 行,该列元素非递减排序,比如说d[a]=b即从b行到a行存在某列为非递减列,这里的b一定是满足条件里最小的那一个
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
const int INF=1e9+;
int d[N],maxx[N],ans[N];
int main(){
int n,m;
cin>>n>>m;
int a; for(int i=;i<=n;i++){
int minn=INF;
for(int j=;j<=m;j++){
scanf("%d",&a);
if(i==) {
maxx[j]=a;
d[j]=i;
}
else {
if(a<maxx[j]) d[j]=i;
maxx[j]=a;
}
minn=min(minn,d[j]);
}
ans[i]=minn;
} int k;
cin>>k;
while(k--){
int l,r;
scanf("%d%d",&l,&r);
if(l>=ans[r]) puts("Yes");
else puts("No");
}
return ;
}
I - Shell Game
Bomboslav likes to look out of the window in his room and watch lads outside playing famous shell game. The game is played by two persons: operator and player. Operator takes three similar opaque shells and places a ball beneath one of them. Then he shuffles the shells by swapping some pairs and the player has to guess the current position of the ball.
Bomboslav noticed that guys are not very inventive, so the operator always swaps the left shell with the middle one during odd moves (first, third, fifth, etc.) and always swaps the middle shell with the right one during even moves (second, fourth, etc.).
Let's number shells from 0 to 2 from left to right. Thus the left shell is assigned number 0, the middle shell is 1 and the right shell is 2. Bomboslav has missed the moment when the ball was placed beneath the shell, but he knows that exactly n movements were made by the operator and the ball was under shell x at the end. Now he wonders, what was the initial position of the ball?
Input
The first line of the input contains an integer n (1 ≤ n ≤ 2·109) — the number of movements made by the operator.
The second line contains a single integer x (0 ≤ x ≤ 2) — the index of the shell where the ball was found after n movements.
Output
Print one integer from 0 to 2 — the index of the shell where the ball was initially placed.
Examples
4
2
1
1
1
0
Note
In the first sample, the ball was initially placed beneath the middle shell and the operator completed four movements.
- During the first move operator swapped the left shell and the middle shell. The ball is now under the left shell.
- During the second move operator swapped the middle shell and the right one. The ball is still under the left shell.
- During the third move operator swapped the left shell and the middle shell again. The ball is again in the middle.
- Finally, the operators swapped the middle shell and the right shell. The ball is now beneath the right shell
找规律,,6组一循环
#include<bits/stdc++.h>
using namespace std; int main(){
int n;
cin>>n;
int x;
cin>>x;
n=n%;
if(n==){
if(x==) cout<<<<endl;
else if(x==) cout<<<<endl;
else if(x==) cout<<<<endl;
}
else if(n==){
if(x==) cout<<<<endl;
else if(x==) cout<<<<endl;
else if(x==) cout<<<<endl;
}
else if(n==){
if(x==) cout<<<<endl;
else if(x==) cout<<<<endl;
else if(x==) cout<<<<endl; }
else if(n==){
if(x==) cout<<<<endl;
if(x==) cout<<<<endl;
if(x==) cout<<<<endl;
}
else if(n==){
if(x==) cout<<<<endl;
if(x==) cout<<<<endl;
if(x==) cout<<<<endl;
}
else if(n==){
cout<<x<<endl;
}
return ;
}
Vasya is an administrator of a public page of organization "Mouse and keyboard" and his everyday duty is to publish news from the world of competitive programming. For each news he also creates a list of hashtags to make searching for a particular topic more comfortable. For the purpose of this problem we define hashtag as a string consisting of lowercase English letters and exactly one symbol '#' located at the beginning of the string. The length of the hashtag is defined as the number of symbols in it without the symbol '#'.
The head administrator of the page told Vasya that hashtags should go in lexicographical order (take a look at the notes section for the definition).
Vasya is lazy so he doesn't want to actually change the order of hashtags in already published news. Instead, he decided to delete some suffixes (consecutive characters at the end of the string) of some of the hashtags. He is allowed to delete any number of characters, even the whole string except for the symbol '#'. Vasya wants to pick such a way to delete suffixes that the total number of deleted symbols is minimum possible. If there are several optimal solutions, he is fine with any of them.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 500 000) — the number of hashtags being edited now.
Each of the next n lines contains exactly one hashtag of positive length.
It is guaranteed that the total length of all hashtags (i.e. the total length of the string except for characters '#') won't exceed 500 000.
Output
Print the resulting hashtags in any of the optimal solutions.
Examples
3
#book
#bigtown
#big
#b
#big
#big
3
#book
#cool
#cold
#book
#co
#cold
4
#car
#cart
#art
#at
#
#
#art
#at
3
#apple
#apple
#fruit
#apple
#apple
#fruit
Note
Word a1, a2, ..., am of length m is lexicographically not greater than word b1, b2, ..., bk of length k, if one of two conditions hold:
- at first position i, such that ai ≠ bi, the character ai goes earlier in the alphabet than character bi, i.e. a has smaller character than b in the first position where they differ;
- if there is no such position i and m ≤ k, i.e. the first word is a prefix of the second or two words are equal.
The sequence of words is said to be sorted in lexicographical order if each word (except the last one) is lexicographically not greater than the next word.
For the words consisting of lowercase English letters the lexicographical order coincides with the alphabet word order in the dictionary.
According to the above definition, if a hashtag consisting of one character '#' it is lexicographically not greater than any other valid hashtag. That's why in the third sample we can't keep first two hashtags unchanged and shorten the other two.
到着来,用一个string 记录上一个字符串,然后比较,如果比y小的话,直接入栈,否则就 拆分后入栈
#include<bits/stdc++.h>
using namespace std; vector<string >ve;
stack<string >st; int main(){ int n;
cin>>n; for(int i=;i<=n;i++){
string x;
cin>>x;
ve.push_back(x);
}
string y; for(int i=n-;i>=;i--){
if(i==n-) {
st.push(ve[i]);
y=ve[i];
}
else { if(ve[i]<=y){
st.push(ve[i]);
y=ve[i];
} else{
string xx=ve[i];
string xxx="";
int ll=y.size(); for(int j=;j<ll;j++){
if(xx[j]>y[j]) {
break;
}
else {
xxx+=xx[j];
}
}
st.push(xxx);
y=xxx;
}
}
} while(st.size()){
cout<<st.top()<<endl;
st.pop();
}
return ;
}
After the fourth season Sherlock and Moriary have realized the whole foolishness of the battle between them and decided to continue their competitions in peaceful game of Credit Cards.
Rules of this game are simple: each player bring his favourite n-digit credit card. Then both players name the digits written on their cards one by one. If two digits are not equal, then the player, whose digit is smaller gets a flick (knock in the forehead usually made with a forefinger) from the other player. For example, if n = 3, Sherlock's card is 123 and Moriarty's card has number 321, first Sherlock names 1 and Moriarty names 3 so Sherlock gets a flick. Then they both digit 2 so no one gets a flick. Finally, Sherlock names 3, while Moriarty names 1 and gets a flick.
Of course, Sherlock will play honestly naming digits one by one in the order they are given, while Moriary, as a true villain, plans to cheat. He is going to name his digits in some other order (however, he is not going to change the overall number of occurences of each digit). For example, in case above Moriarty could name 1, 2, 3 and get no flicks at all, or he can name 2, 3 and 1 to give Sherlock two flicks.
Your goal is to find out the minimum possible number of flicks Moriarty will get (no one likes flicks) and the maximum possible number of flicks Sherlock can get from Moriarty. Note, that these two goals are different and the optimal result may be obtained by using different strategies.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of digits in the cards Sherlock and Moriarty are going to use.
The second line contains n digits — Sherlock's credit card number.
The third line contains n digits — Moriarty's credit card number.
Output
First print the minimum possible number of flicks Moriarty will get. Then print the maximum possible number of flicks that Sherlock can get from Moriarty.
Examples
3
123
321
0
2
2
88
00
2
0
Note
First sample is elaborated in the problem statement. In the second sample, there is no way Moriarty can avoid getting two flicks.
//谁小谁获获得得轻弹
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
string s,m;
cin>>s;
cin>>m;
sort(s.begin(),s.end());
sort(m.begin(),m.end());
int pos=;
int a1=,ans1=n;
for(int i=;i<n;i++){
for(int j=pos;j<n;j++){
if(s[i]<=m[j]){
pos=j+;
a1++;
break;
}
}
}
ans1-=a1;
int a2=,ans2=n;
pos=;
for(int i=;i<n;i++){
for(int j=pos;j<n;j++){
if(s[i]<m[j]){
pos=j+;
a2++;
break;
}
}
}
cout<<ans1<<endl;
cout<<a2<<endl;
return ;
}
First Training的更多相关文章
- hdu 4946 2014 Multi-University Training Contest 8
Area of Mushroom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 2016 Multi-University Training Contests
2016 Multi-University Training Contest 1 2016 Multi-University Training Contest 2 2016 Multi-Univers ...
- 2016 Multi-University Training Contest 2 D. Differencia
Differencia Time Limit: 10000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 2016 Multi-University Training Contest 1 G. Rigid Frameworks
Rigid Frameworks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- The Solution of UESTC 2016 Summer Training #1 Div.2 Problem C
Link http://acm.hust.edu.cn/vjudge/contest/121539#problem/C Description standard input/output After ...
- 2012 Multi-University Training Contest 9 / hdu4389
2012 Multi-University Training Contest 9 / hdu4389 打巨表,实为数位dp 还不太懂 先这样放着.. 对于打表,当然我们不能直接打,这里有技巧.我们可以 ...
- 2014 Multi-University Training Contest 9#11
2014 Multi-University Training Contest 9#11 Killing MonstersTime Limit: 2000/1000 MS (Java/Others) ...
- 2014 Multi-University Training Contest 9#6
2014 Multi-University Training Contest 9#6 Fast Matrix CalculationTime Limit: 2000/1000 MS (Java/Oth ...
- 2016 Multi-University Training Contest 1
8/11 2016 Multi-University Training Contest 1 官方题解 老年选手历险记 最小生成树+线性期望 A Abandoned country(BH) 题意: 1. ...
随机推荐
- 【spring springmvc】这里有你想要的SpringMVC的REST风格的四种请求方式
概述 之前的文章springmvc使用注解声明控制器与请求映射有简单提到过控制器与请求映射,这一次就详细讲解一下SpringMVC的REST风格的四种请求方式及其使用方法. 你能get的知识点 1.什 ...
- 题解 P5835 【 USACO19DEC Meetings S】
前言 这道题目是道好题,想通了之后就可以把轻松这道题做出来. 正文 结论 先把一个结论写出来. 无论所有奶牛怎么走,它们的体重从左往右组成的序列是不会发生改变的. 这个结论简单地说明一下. 首先我们可 ...
- Contest 156
2019-09-29 14:56:09 总体感受:30min解决了前三题,最后一题其实也很简单,但是题目没有看清.赛后5min解决了.总体来说,本次周赛是比较简单的一次. 注意点:首先是hard问题的 ...
- 从源码和doc揭秘——Java中的Char究竟几个字节,Java与Unicode的关系
#编码与字符编码 (懂编码的建议直接跳过) 在计算机世界中,任何事物都是用二进制图片数字表示的,图片可以编码为JPG,PNG格式的字节流,音频,视频有MP3,MP4格式的字节流.这些JPG,MP3等都 ...
- Apache服务的主要目录和配置文件详解
Apache服务的主要目录和配置文件详解 2014-01-14 19:05:14 标签:httpd配置文件详解 apache配置文件 httpd配置文件 apache文件目录 原创作品,允许转载,转载 ...
- 《自拍教程52》Python_adb运行Shell脚本
Android作为一款Linux终端,肯定是支持.sh后缀的Shell脚本的运行的, 有时候测试环境准备或者长时间截取复杂的日志等,开发会给到一些Shell脚本. Shell脚本的执行的优势: 快捷高 ...
- Ali_Cloud++:阿里云Centos配置iptables防火墙教程
虽说阿里云推出了云盾服务,但是自己再加一层防火墙总归是更安全些,下面是我在阿里云vps上配置防火墙的过程,目前只配置INPUT.OUTPUT和FORWORD都是ACCEPT的规则 1):检查iptab ...
- Thread---重排序
重排序 数据依赖性 如果两个操作访问同一个变量,且这两个操作中有一个为写操作,此时这两个操作之间就存在数据依赖性.数据依赖分下列三种类型: 名称 代码示例 说明 写后读 a = 1;b = a; 写一 ...
- Thread --三大特性 原子性,可见性,有序性
原子性.可见性.有序性 什么是原子性 即一个操作或者多个操作 要么全部执行并且执行的过程不会被任何因素打断,要么就都不执行. 一个很经典的例子就是银行账户转账问题: 比如从账户A向账户B转1000元, ...
- 字符串中的count()方法
描述 Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位置. 语法 count()方法语法: str.count(sub, start= 0,e ...