A. Oleg and shares
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Oleg the bank client checks share prices every day. There are n share prices he is interested in. Today he observed that each second exactly one of these prices decreases by k rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all n prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question?

Input

The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 109) — the number of share prices, and the amount of rubles some price decreases each second.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the initial prices.

Output

Print the only line containing the minimum number of seconds needed for prices to become equal, of «-1» if it is impossible.

Examples
input
3 3
12 9 15
output
3
input
2 2
10 9
output
-1
input
4 1
1 1000000000 1000000000 1000000000
output
2999999997
Note

Consider the first example.

Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds.

There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3.

In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal.

In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999 * 3 = 2999999997 seconds. We can note that this is the minimum possible time.

你有n支股票,分别有不同的价格,每次会下降k,问下降到最小值所需最小时间。注意要用long long,否则溢出,样例给你还是很良心的

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
int a[N];
typedef long long LL;
int main()
{
int n,k;
cin>>n>>k;
int mi=<<;
for(int i=;i<=n;i++){
cin>>a[i];
mi=min(mi,a[i]);}
int f=;
long long s=;
for(int i=;i<=n;i++){
int w=a[i]-mi;
if(w%k){
f=;break;}
s+=w/k;
}
if(f)
printf("%lld\n",s);
else
puts("-1"); return ;
}
B. Igor and his way to work
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.

Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the grid.

Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:

  • "." — an empty cell;
  • "*" — a cell with road works;
  • "S" — the cell where Igor's home is located;
  • "T" — the cell where Igor's office is located.

It is guaranteed that "S" and "T" appear exactly once each.

Output

In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise.

Examples
input
5 5
..S..
****.
T....
****.
.....
output
YES
input
5 5
S....
****.
.....
.****
..T..
output
NO
Note

The first sample is shown on the following picture:

In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:

暴力搜索,从S到T拐两个弯内就输出YES,否则NO,所以需要记忆下现在拐的弯

#include <bits/stdc++.h>
using namespace std;
char str[][];
int vis[][];
int n, m;
int dx[] = {,-,,};
int dy[] = {,,,-}; void dfs(int x, int y, int d, int pre)
{
if(x<||x>=n||y<||y>=m) return ;
if(str[x][y] == '*') return ;
vis[x][y] = ;
if(d==) {
for(int i = ; i < ; i++) dfs(x+dx[i],y+dy[i],,i);
}
dfs(x+dx[pre],y+dy[pre],d,pre);
}
bool ans_dfs(int x,int y,int pre)
{
if(x<||x>=n||y<||y>=m) return false;
if(str[x][y]=='*') return false;
if(vis[x][y]==) return true;
return ans_dfs(x+dx[pre],y+dy[pre],pre);
}
int main()
{
memset(vis,,sizeof(vis));
int x1,x2,y1,y2;
scanf("%d%d",&n,&m);
for(int i = ;i < n; i++)
scanf("%s",str[i]);
for(int i = ;i < n; i++) {
for(int j = ;j < m; j++) {
if(str[i][j]=='S') {
x1 = i;
y1 = j;
}
if(str[i][j]=='T') {
x2 = i;
y2 = j;
}
}
}
for(int i = ;i < ; i++) {
dfs(x1,y1,,i);
}
bool f = false;
for(int i = ;i < ; i++) {
if(ans_dfs(x2,y2,i)) {
f = true;
break;
}
}
if(f) puts("YES");
else puts("NO");
return ;
}

Tinkoff Challenge - Elimination Round 开始补题的更多相关文章

  1. Tinkoff Challenge - Elimination Round D. Presents in Bankopolis(区间DP)

    http://codeforces.com/contest/793/problem/D 题意:给出一些点和他们之间的距离,是有向的,这些点从1~n顺序排列,现在选出k个点组成一条路径,使他们之间的距离 ...

  2. Tinkoff Challenge - Elimination Round B. Igor and his way to work(dfs+优化)

    http://codeforces.com/contest/793/problem/B 题意:一个地图,有起点和终点还有障碍点,求从起点出发到达终点,经过的路径上转弯次数是否能不超过2. 思路: 直接 ...

  3. Tinkoff Challenge - Elimination Round C. Mice problem(模拟)

    传送门 题意 给出一个矩形的左下角和右上角的坐标,给出n个点的初始坐标和运动速度和方向,询问是否存在一个时间使得所有点都在矩形内,有则输出最短时间,否则输出-1 分析 对于每个点如果运动过程中都不在矩 ...

  4. Technocup 2020 - Elimination Round 1补题

    慢慢来. 题目册 题目 A B C D tag math strings greedy dp 状态 √ √ √ √ //∅,√,× 想法 A. CME res tp A 题意:有\(n\)根火柴,额外 ...

  5. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) B. Verse Pattern 水题

    B. Verse Pattern 题目连接: http://codeforces.com/contest/722/problem/B Description You are given a text ...

  6. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) A. Broken Clock 水题

    A. Broken Clock 题目连接: http://codeforces.com/contest/722/problem/A Description You are given a broken ...

  7. Educational Codeforces Round 23 补题小结

    昨晚听说有教做人场,去补了下玩. 大概我的水平能做个5/6的样子? (不会二进制Trie啊,我真菜) A. 傻逼题.大概可以看成向量加法,判断下就好了. #include<iostream> ...

  8. Tinkoff Challenge - Final Round (Codeforces Round #414, rated, Div. 1 + Div. 2) 继续跪一把

    这次的前三题挺简单的,可是我做的不快也不对. A. Bank Robbery time limit per test 2 seconds memory limit per test 256 megab ...

  9. Educational Codeforces Round 27 补题

    题目链接:http://codeforces.com/contest/845 A. Chess Tourney 水题,排序之后判断第n个元素和n+1个元素是不是想等就可以了. #include < ...

随机推荐

  1. [转]如何降低SQL Server 内存使用率

    我的数据库服务器内存为8G,现在资源管理器显示内存用到5G,可以肯定是sql server数据库吃内存原因. MSSQL占用了太多的内存,而且还不断的增长:或者说已经设置了使用内存,可是它没有用到那么 ...

  2. 一文带你读懂 Mysql 和 InnoDB存储引擎

    作为一名开发人员,在日常的工作中会难以避免地接触到数据库,无论是基于文件的 sqlite 还是工程上使用非常广泛的 MySQL.PostgreSQL,但是一直以来也没有对数据库有一个非常清晰并且成体系 ...

  3. CF1079C Playing Piano

    思路: dp. 实现: #include <bits/stdc++.h> using namespace std; ], dp[][]; int main() { int n; while ...

  4. 实现dedecms(PC端)全站动态浏览 并实现伪静态

    dedecms默认是生成静态文件,如何实现织梦(PC端)全站动态浏览呢? 织梦全站动态浏览方法 1. 修改首页为动态浏览 后台-生成-更新首页-勾选“仅动态浏览” 2. 修改栏目页为动态浏览 ①添加或 ...

  5. Jenkins环境搭建(6)-修改自动化测试报告的样式

    写在最前: 我遇到一个问题,就是导出数据时,接口返回的数据是乱码,乱码如下图所示.问了开发,说是byte数据.这种情况,将response Data数据写入到报告中的话,在jenkins上运行时,提示 ...

  6. apache下设置域名多站点访问及禁止apache访问80端口

    apache下设置域名多站点访问 当前系统:macOS High Sierra 域名访问配置指定端口后,不同域名只能配置不同的端口 apache配置目录: sudo vim /etc/apache2/ ...

  7. C# 语句 分支语句

    语句是指程序命令,按照顺序执行.可以分为   顺序语句  分支语句  循环语句 之前学习的内容都是按照顺序程序执行的,称之为顺序语句. 今天学的的内容是分支语句. 语句可以嵌套,可以是以分号结尾的单行 ...

  8. 编写Robotium测试程序

    6.编写Robotium测试程序 1)导包 //导入需要测试的工程 import com.example.android.notepad.NotesList; //robotium提供的测试用类 im ...

  9. LINUX 安装JDK (rpm格式和tar.gz格式)

    谷歌博客地址:http://tsaiquinn.blogspot.com/2014/10/linux-jdk-rpmtargz.html JDK rpm方式: 我使用的是SecureCRT,先下载了然 ...

  10. C语言二维数组作为函数参数

    设有整型二维数组a[3][4]如下:0   1   2   34   5   6   78   9  10  11 它的定义为:    int a[3][4]={{0,1,2,3},{4,5,6,7} ...