A. Saitama Destroys Hotel

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in one of its other hotels. The elevator is special — it starts on the top floor, can only move down, and has infinite capacity. Floors are numbered from 0 to s and elevator initially starts on floor s at time 0.

The elevator takes exactly 1 second to move down exactly 1 floor and negligible time to pick up passengers. Genos is given a list detailing when and on which floor passengers arrive. Please determine how long in seconds it will take Genos to bring all passengers to floor 0.

Input

The first line of input contains two integers n and s (1 ≤ n ≤ 100, 1 ≤ s ≤ 1000) — the number of passengers and the number of the top floor respectively.

The next n lines each contain two space-separated integers fi and ti (1 ≤ fi ≤ s, 1 ≤ ti ≤ 1000) — the floor and the time of arrival in seconds for the passenger number i.

Output

Print a single integer — the minimum amount of time in seconds needed to bring all the passengers to floor 0.

Examples
Input
3 7
2 1
3 8
5 2
Output
11
Input
5 10
2 77
3 33
8 21
9 12
10 64
Output
79
Note

In the first sample, it takes at least 11 seconds to bring all passengers to floor 0. Here is how this could be done:

1. Move to floor 5: takes 2 seconds.

2. Pick up passenger 3.

3. Move to floor 3: takes 2 seconds.

4. Wait for passenger 2 to arrive: takes 4 seconds.

5. Pick up passenger 2.

6. Go to floor 2: takes 1 second.

7. Pick up passenger 1.

8. Go to floor 0: takes 2 seconds.

This gives a total of 2 + 2 + 4 + 1 + 2 = 11 seconds.

题目链接:http://codeforces.com/contest/608/problem/A

【题意】一楼梯从最高层开始依次下降,每下一层花1s,如果该层有人,则等乘客到达后载客继续下降,乘客进电梯的时间忽略不计,给定有乘客的楼层数及乘客到达时间,求承载所有乘客下至第0层所花费的最小时间。

下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
int main()
{
int n,m,a,b;
cin>>n>>m;
int t=;
while(n--)
{
cin>>a>>b;
t=max(b-(m-a),t);
}
cout<<m+t<<endl;
}

B. Hamming Distance Sum

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

Genos needs your help. He was asked to solve the following programming problem by Saitama:

The length of some string s is denoted |s|. The Hamming distance between two strings s and t of equal length is defined as , where si is the i-th character of s and ti is the i-th character of t. For example, the Hamming distance between string "0011" and string "0110" is |0 - 0| + |0 - 1| + |1 - 1| + |1 - 0| = 0 + 1 + 0 + 1 = 2.

Given two binary strings a and b, find the sum of the Hamming distances between a and all contiguous substrings of b of length |a|.

Input

The first line of the input contains binary string a (1 ≤ |a| ≤ 200 000).

The second line of the input contains binary string b (|a| ≤ |b| ≤ 200 000).

Both strings are guaranteed to consist of characters '0' and '1' only.

Output

Print a single integer — the sum of Hamming distances between a and all contiguous substrings of b of length |a|.

Examples
Input
01
00111
Output
3
Input
0011
0110
Output
2
Note

For the first sample case, there are four contiguous substrings of b of length |a|: "00", "01", "11", and "11". The distance between "01" and "00" is |0 - 0| + |1 - 0| = 1. The distance between "01" and "01" is |0 - 0| + |1 - 1| = 0. The distance between "01" and "11" is |0 - 1| + |1 - 1| = 1. Last distance counts twice, as there are two occurrences of string "11". The sum of these edit distances is 1 + 0 + 1 + 1 = 3.

The second sample case is described in the statement.

题目链接:http://codeforces.com/contest/608/problem/B

【题意】给定两个字符串a,b,求b中所有跟a长度相同的子串中,各个字符与a中相应字符的差的绝对值的和。
【分析】对于字符串b中每个字符,记录该字符之前包含该字符在内的子串中0,1出现的次数。对于长度为lena的字符串a,长度为lenb的字符串b,a中第i个字符可以与b中第i到第lenb-lena+i个字符进行比较。若a中字符为0,则最后结果加上该区间内的字符为1的字符个数,若为1,则加上字符为0的个数。

下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int cnt[];
int main()
{
string a,b;
cin>>a>>b;
int len1=a.size();
int len2=b.size();
ll ans=;
for(int i=;i<len2;i++)
{
cnt[b[i]]++;
if(i>=len2-len1)
{
ans+=(len2-len1+)-cnt[a[i-(len2-len1)]];
cnt[b[i-(len2-len1)]]--;
}
}
cout<<ans<<endl;
return ;
}

C. Chain Reaction

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is activated, it destroys all beacons to its left (direction of decreasing coordinates) within distance bi inclusive. The beacon itself is not destroyed however. Saitama will activate the beacons one at a time from right to left. If a beacon is destroyed, it cannot be activated.

Saitama wants Genos to add a beacon strictly to the right of all the existing beacons, with any position and any power level, such that the least possible number of beacons are destroyed. Note that Genos's placement of the beacon means it will be the first beacon activated. Help Genos by finding the minimum number of beacons that could be destroyed.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 100 000) — the initial number of beacons.

The i-th of next n lines contains two integers ai and bi (0 ≤ ai ≤ 1 000 000, 1 ≤ bi ≤ 1 000 000) — the position and power level of the i-th beacon respectively. No two beacons will have the same position, so ai ≠ aj if i ≠ j.

Output

Print a single integer — the minimum number of beacons that could be destroyed if exactly one beacon is added.

Examples
Input
4
1 9
3 1
6 1
7 4
Output
1
Input
7
1 1
2 1
3 1
4 1
5 1
6 1
7 1
Output
3
Note

For the first sample case, the minimum number of beacons destroyed is 1. One way to achieve this is to place a beacon at position 9 with power level 2.

For the second sample case, the minimum number of beacons destroyed is 3. One way to achieve this is to place a beacon at position 1337 with power level 42.

题目链接:http://codeforces.com/contest/608/problem/C

【题意】一连串灯塔,从左到右排列,位于位置a[i]的灯塔,激活后,可以破坏距离为b[i]范围内的其他灯塔,灯塔仅可向左边攻击,攻击他人自己不受损坏,受损的灯塔不能被再次激活。 现在整个排列的最右边放置一个灯塔,使得从右向左依次激活灯塔后,受破坏的灯塔个数最少。
【分析】激活最右边的灯塔之后,所有被破坏的灯塔中距离激活的灯塔最远的灯塔左边的灯塔得以存活,所以从左向右依次记录激活该灯塔后,左边剩余灯塔个数,算出最大值,用总灯塔数减去该最大值。

下面给出AC代码:

 #include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int m[];
int dp[];
int main (void)
{
int n;
cin>>n;
int x,y;
int mins=0x3ffffff;
int maxn=;
for(int i = ; i < n; i++){
cin>>x>>y;
m[x]=y;
mins=min(mins,x);
maxn=max(maxn,x);
}
for(int i = mins; i <= maxn; i++){
if(!m[i]) {dp[i]=dp[i-]; continue;}
if(i-m[i]->=) dp[i]=dp[i-m[i]-]+;
else dp[i]=;
}
long long ans=;
for(int i = mins; i <= maxn; i++){
if(ans<dp[i]) ans=dp[i];
}
cout<<n-ans<<endl;
return ;
}

有一种简写,反正我没太看懂QAQ

 #include<bits/stdc++.h>
using namespace std;
const int MAXA=;
int n,t[MAXA],a,b[MAXA],i,ans;
int main()
{
for(cin>>n;cin>>a;cin>>b[a+]);
for(i=;i<MAXA;ans=max(ans,t[i++]))t[i]=(b[i]?(t[max(i--b[i], )]+):t[i-]);
cout<<n-ans<<endl;
return ;
}

D. Zuma

time limit per test:2 seconds
memory limit per test:512 megabytes
input:standard input
output:standard output

Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, the i-th of which has color ci. The goal of the game is to destroy all the gemstones in the line as quickly as possible.

In one second, Genos is able to choose exactly one continuous substring of colored gemstones that is a palindrome and remove it from the line. After the substring is removed, the remaining gemstones shift to form a solid line again. What is the minimum number of seconds needed to destroy the entire line?

Let us remind, that the string (or substring) is called palindrome, if it reads same backwards or forward. In our case this means the color of the first gemstone is equal to the color of the last one, the color of the second gemstone is equal to the color of the next to last and so on.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 500) — the number of gemstones.

The second line contains n space-separated integers, the i-th of which is ci (1 ≤ ci ≤ n) — the color of the i-th gemstone in a line.

Output

Print a single integer — the minimum number of seconds needed to destroy the entire line.

Examples
Input
3
1 2 1
Output
1
Input
3
1 2 3
Output
3
Input
7
1 4 4 2 3 2 1
Output
2
Note

In the first sample, Genos can destroy the entire line in one second.

In the second sample, Genos can only destroy one gemstone at a time, so destroying three gemstones takes three seconds.

In the third sample, to achieve the optimal time of two seconds, destroy palindrome 4 4 first and then destroy palindrome 1 2 3 2 1.

题目链接:http://codeforces.com/contest/608/problem/D

题意:输入一个字符串;每次消去一个回文串,问最少消去的次数为多少?

思路:一般对于可以从中间操作的,一般看成是从头开始(因为只需要考虑一边),当考虑最左边的数时,有多少中消去方法?每种消去方法对结果的贡献又是多少?同时结果的区间又是怎么变化?这就是dp式子;

1.当单独消去这个元素时,dp[l][r] = 1 + dp[l+1][r];
2.当存在一个k,l< k <= r,使得c[l] ==
c[k]时,可以认为在中间消除到只剩下一个回文串时,“顺带”把这一对元素消除;因为只少消到只剩下一个元素,那这个元素也是一个回文串,这时dp式子就是
dp[l][r] = dp[l+1][k-1] + dp[k+1][r];
那么当这里面的元素本来就为0呢?这就是我前面k的取值范围没有包括l的原因,这就是把[l , l + 1]当成回文串来消去,dp[l][r] = 1+dp[l+2][r];

 #include<bits/stdc++.h>
using namespace std;
const int maxn=;
int num[maxn];
int dp[maxn][maxn];
int main()
{
int n;cin>>n;
memset(dp,0x3f,sizeof(dp));
for(int i=;i<=n;++i)
{
scanf("%d",&num[i]);
dp[i][i]=;
}
for(int len=;len<=n;++len)
{//枚举区间长度
for(int l=;l<=n-len+;++l)
{//枚举区间左端点
int r=l+len-;
for(int k=l;k<r;++k)
{
dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+][r]);
}
if(num[l]==num[r])
{
if(l+==r){dp[l][r]=;}
dp[l][r]=min(dp[l][r],dp[l+][r-]);
}
}
}
printf("%d\n",dp[][n]);
return ;
}

E. Marbles

time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

In the spirit of the holidays, Saitama has given Genos two grid paths of length n (a weird gift even by Saitama's standards). A grid path is an ordered sequence of neighbouring squares in an infinite grid. Two squares are neighbouring if they share a side.

One example of a grid path is (0, 0) → (0, 1) → (0, 2) → (1, 2) → (1, 1) → (0, 1) → ( - 1, 1). Note that squares in this sequence might be repeated, i.e. path has self intersections.

Movement within a grid path is restricted to adjacent squares within the sequence. That is, from the i-th square, one can only move to the (i - 1)-th or (i + 1)-th squares of this path. Note that there is only a single valid move from the first and last squares of a grid path. Also note, that even if there is some j-th square of the path that coincides with the i-th square, only moves to (i - 1)-th and (i + 1)-th squares are available. For example, from the second square in the above sequence, one can only move to either the first or third squares.

To ensure that movement is not ambiguous, the two grid paths will not have an alternating sequence of three squares. For example, a contiguous subsequence (0, 0) → (0, 1) → (0, 0) cannot occur in a valid grid path.

One marble is placed on the first square of each grid path. Genos wants to get both marbles to the last square of each grid path. However, there is a catch. Whenever he moves one marble, the other marble will copy its movement if possible. For instance, if one marble moves east, then the other marble will try and move east as well. By try, we mean if moving east is a valid move, then the marble will move east.

Moving north increases the second coordinate by 1, while moving south decreases it by 1. Similarly, moving east increases first coordinate by 1, while moving west decreases it.

Given these two valid grid paths, Genos wants to know if it is possible to move both marbles to the ends of their respective paths. That is, if it is possible to move the marbles such that both marbles rest on the last square of their respective paths.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 1 000 000) — the length of the paths.

The second line of the input contains a string consisting of n - 1 characters (each of which is either 'N', 'E', 'S', or 'W') — the first grid path. The characters can be thought of as the sequence of moves needed to traverse the grid path. For example, the example path in the problem statement can be expressed by the string "NNESWW".

The third line of the input contains a string of n - 1 characters (each of which is either 'N', 'E', 'S', or 'W') — the second grid path.

Output

Print "YES" (without quotes) if it is possible for both marbles to be at the end position at the same time. Print "NO" (without quotes) otherwise. In both cases, the answer is case-insensitive.

Examples
Input
7
NNESWW
SWSWSW
Output
YES
Input
3
NN
SS
Output
NO
Note

In the first sample, the first grid path is the one described in the statement. Moreover, the following sequence of moves will get both marbles to the end: NNESWWSWSW.

In the second sample, no sequence of moves can get both marbles to the end.

题目链接:http://codeforces.com/contest/608/problem/E

分析:先贴代码,用reverse做!

下面给出AC代码:

 #include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=;
int n;
char s1[maxn],s2[maxn];
int main(){
scanf("%d%s%s",&n,s1+,s2+);
reverse(s1+,s1+n);
for(int i=;i<n;++i)
{
if(s1[i]=='N')s1[i]='S';
else if(s1[i]=='S')s1[i]='N';
else if(s1[i]=='W')s1[i]='E';
else s1[i]='W';
}
unsigned long long h1=,h2=,fa=;
for(int i=;i<n;++i)
{
h1=h1+s1[i]*fa;
h2=h2*233u+s2[n-i];
fa*=233u;
if(h1==h2)return puts("NO"),;
}
puts("YES");
return ;
}

Codeforces Round #336 (Div. 2)【A.思维,暴力,B.字符串,暴搜,前缀和,C.暴力,D,区间dp,E,字符串,数学】的更多相关文章

  1. Codeforces Round #336 (Div. 2) B. Hamming Distance Sum 计算答案贡献+前缀和

    B. Hamming Distance Sum   Genos needs your help. He was asked to solve the following programming pro ...

  2. Codeforces Round #336 (Div. 2) D. Zuma

    Codeforces Round #336 (Div. 2) D. Zuma 题意:输入一个字符串:每次消去一个回文串,问最少消去的次数为多少? 思路:一般对于可以从中间操作的,一般看成是从头开始(因 ...

  3. Codeforces Round #336 (Div. 2)A. Saitama Destroys Hotel 水题

    A. Saitama Destroys Hotel 题目连接: http://www.codeforces.com/contest/608/problem/A Description Saitama ...

  4. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

  5. Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)

    A. Vladik and Courtesy time limit per test:2 seconds memory limit per test:256 megabytes input:stand ...

  6. Codeforces Round #336 (Div. 2)B 暴力 C dp D 区间dp

    B. Hamming Distance Sum time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  7. Codeforces Round #542(Div. 2) CDE 思维场

    C https://codeforces.com/contest/1130/problem/C 题意 给你一个\(n*m\)(n,m<=50)的矩阵,每个格子代表海或者陆地,给出在陆地上的起点终 ...

  8. Codeforces Round #304 (Div. 2) D 思维/数学/质因子/打表/前缀和/记忆化

    D. Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  9. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

随机推荐

  1. 【Linux】Linux学习笔记(完结)

    前言 在工作中发现Linux系统的重要性,于是计划重温下Linux,顺便记录笔记方便之后查阅. 磁盘分区 在Linux系统中,每个设备都被当成一个文件来对待:如IDE接口的硬盘文件名为/dev/hd[ ...

  2. wincc flexable变量组态

    1.变量分类 2.变量组态练习 3.变量组态之前新建一个设备连接取名connect 3.tag1组态bool类型,双击变量弹出下面窗口,具体如下图所示connect表示外部变量 4.组态tag2 5. ...

  3. BGP网络学习总结

    1.前言     云计算在中国发展越来越快,企业逐步开始将业务迁移到公有云中,方便运维,节省成本.公有云最复杂的地方是网络,客户对网络的需求千奇百怪,造成网络环境极其复杂,稍有不慎,就会出现网络连通性 ...

  4. C 函数参数 char **s与char *s[]

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/126 先来看一个小例子 : 编写函数遍历一个整型数组的元素,数组 ...

  5. 【ASP.NET MVC系列】浅谈NuGet在VS中的运用

    一     概述 在我们讲解NuGet前,我们先来看看一个例子. 1.例子: 假设现在开发一套系统,其中前端框架我们选择Bootstrap,由于选择Bootstrap作为前端框架,因此,在项目中,我们 ...

  6. MySQL 加锁处理分析-转载

    来自何登成的技术博客     1.1    MVCC:Snapshot Read vs Current Read    2 1.2    Cluster Index:聚簇索引    3 1.3     ...

  7. ConcurrentHashMap 从Java7 到 Java8的改变

    一.关于分段锁 集合框架很大程度减少了java程序员的重复劳动,然而,在Java多线程环境中,以线程安全的方式使用集合类是一个首先考虑的问题. 越来越多的程序员了解到了ConcurrentHashMa ...

  8. Java后端程序员都做些什么?

    这个问题来自于QQ网友,一句两句说不清楚,索性写个文章. 我刚开始做Web开发的时候,根本没有前端,后端之说. 原因很简单,那个时候服务器端的代码就是一切:接受浏览器的请求,实现业务逻辑,访问数据库, ...

  9. C#语言和SQL Server第十章笔记

    第十章 :使用关键字模糊查询 笔记 一:使用关键字 :LIKE  BETWEEN  IN进行模糊查询 通配符:  一类字符,代替一个或多个真正的字符 与LIKE关键字一起使用 通配符: 解释 实例 符 ...

  10. Vue-cli 创建的项目如何跨域请求

    感谢BeArchitect的技术支持 问题描述: 使用 Vue-cli 创建的项目,开发地址是 localhost:8023,需要访问 localhost:9000 上的接口 分析原因: 不同域名之间 ...