A - F


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100100 points

Problem Statement

You are given two integers aa and bb. Determine if a+b=15a+b=15 or a×b=15a×b=15 or neither holds.

Note that a+b=15a+b=15 and a×b=15a×b=15 do not hold at the same time.

Constraints

  • 1≤a,b≤151≤a,b≤15
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

aa bb

Output

If a+b=15a+b=15, print +; if a×b=15a×b=15, print *; if neither holds, print x.


Sample Input 1 Copy

Copy
4 11

Sample Output 1 Copy

Copy
+

4+11=154+11=15.


Sample Input 2 Copy

Copy
3 5

Sample Output 2 Copy

Copy
*

3×5=153×5=15.


Sample Input 3 Copy

Copy
1 1

Sample Output 3 Copy

Copy
x

1+1=21+1=2 and 1×1=11×1=1, neither of which is 15.

代码:

import java.util.*;

public class Main {

    public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
if(a * b == 15)System.out.println('*');
else if(a + b == 15)System.out.println('+');
else System.out.println('x');
}
}

B - Acrostic


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 200200 points

Problem Statement

You are given a string SS consisting of lowercase English letters. We will write down this string, starting a new line after every ww letters. Print the string obtained by concatenating the letters at the beginnings of these lines from top to bottom.

Constraints

  • 1≤w≤|S|≤10001≤w≤|S|≤1000
  • SS consists of lowercase English letters.
  • ww is an integer.

Input

Input is given from Standard Input in the following format:

SS
ww

Output

Print the desired string in one line.


Sample Input 1 Copy

Copy
abcdefgh
3

Sample Output 1 Copy

Copy
adg

When we write down abcdefgh, starting a new line after every three letters, we get the following:

abc
def
gh

Concatenating the letters at the beginnings of these lines, we obtain adg.


Sample Input 2 Copy

Copy
lllll
1

Sample Output 2 Copy

Copy
lllll

Sample Input 3 Copy

Copy
souuundhound
2

Sample Output 3 Copy

Copy
suudon
代码:
import java.util.*;

public class Main {

    public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
int n = in.nextInt();
for(int i = 0;i < s.length();i += n) {
System.out.print(s.charAt(i));
}
}
}

C - Ordinary Beauty


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 300300 points

Problem Statement

Let us define the beauty of a sequence (a1,...,an)(a1,...,an) as the number of pairs of two adjacent elements in it whose absolute differences are dd. For example, when d=1d=1, the beauty of the sequence (3,2,3,10,9)(3,2,3,10,9) is 33.

There are a total of nmnm sequences of length mm where each element is an integer between 11 and nn (inclusive). Find the beauty of each of these nmnm sequences, and print the average of those values.

Constraints

  • 0≤d<n≤1090≤d<n≤109
  • 2≤m≤1092≤m≤109
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

nn mm dd

Output

Print the average of the beauties of the sequences of length mm where each element is an integer between 11 and nn. The output will be judged correct if the absolute or relative error is at most 10−610−6.


Sample Input 1 Copy

Copy
2 3 1

Sample Output 1 Copy

Copy
1.0000000000

The beauty of (1,1,1)(1,1,1) is 00.
The beauty of (1,1,2)(1,1,2) is 11.
The beauty of (1,2,1)(1,2,1) is 22.
The beauty of (1,2,2)(1,2,2) is 11.
The beauty of (2,1,1)(2,1,1) is 11.
The beauty of (2,1,2)(2,1,2) is 22.
The beauty of (2,2,1)(2,2,1) is 11.
The beauty of (2,2,2)(2,2,2) is 00.
The answer is the average of these values: (0+1+2+1+1+2+1+0)/8=1(0+1+2+1+1+2+1+0)/8=1.


Sample Input 2 Copy

Copy
1000000000 180707 0

Sample Output 2 Copy

Copy
0.0001807060
肯定不能排着遍历各种情况求beauty了,如果第i个数和第i+1个数相差d那么这个组合(Ai,Ai+1)就使的beauty加1,显然如果d不为0,n个数可以组成(n-d)*2个这样的组合,每两个数不相同可以换位置,但是如果d为0,那么满足要求的是两个相同的数,显然就不用乘2了,而满足要求的组合又可以在m-1个位置,因为一共有m个数,当选定一个位置(Ai,Ai+1)后,其他m-2个位置可以放1~n的任何数。
所以总的beauty就是(n - d) * (d == 0 ? 1 : 2) * (m - 1) * n^(m - 2),然后除以n^m得到平均值。
代码:
import java.util.*;

public class Main {

    public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double n = in.nextInt();
double m = in.nextInt();
double d = in.nextInt();
System.out.printf("%.10f",(n - d) * (d == 0 ? 1 : 2) * (m - 1) / (n * n));
}
}

D - Saving Snuuk


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 400400 points

Problem Statement

Kenkoooo is planning a trip in Republic of Snuke. In this country, there are nn cities and mm trains running. The cities are numbered 11 through nn, and the ii-th train connects City uiui and vivi bidirectionally. Any city can be reached from any city by changing trains.

Two currencies are used in the country: yen and snuuk. Any train fare can be paid by both yen and snuuk. The fare of the ii-th train is aiai yen if paid in yen, and bibi snuuk if paid in snuuk.

In a city with a money exchange office, you can change 11 yen into 11 snuuk. However, when you do a money exchange, you have to change all your yen into snuuk. That is, if Kenkoooo does a money exchange when he has XX yen, he will then have XX snuuk. Currently, there is a money exchange office in every city, but the office in City ii will shut down in ii years and can never be used in and after that year.

Kenkoooo is planning to depart City ss with 10151015 yen in his pocket and head for City tt, and change his yen into snuuk in some city while traveling. It is acceptable to do the exchange in City ss or City tt.

Kenkoooo would like to have as much snuuk as possible when he reaches City tt by making the optimal choices for the route to travel and the city to do the exchange. For each i=0,...,n−1i=0,...,n−1, find the maximum amount of snuuk that Kenkoooo has when he reaches City tt if he goes on a trip from City ss to City ttafter ii years. You can assume that the trip finishes within the year.

Constraints

  • 2≤n≤1052≤n≤105
  • 1≤m≤1051≤m≤105
  • 1≤s,t≤n1≤s,t≤n
  • s≠ts≠t
  • 1≤ui<vi≤n1≤ui<vi≤n
  • 1≤ai,bi≤1091≤ai,bi≤109
  • If i≠ji≠j, then ui≠ujui≠uj or vi≠vjvi≠vj.
  • Any city can be reached from any city by changing trains.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

nn mm ss tt
u1u1 v1v1 a1a1 b1b1
::
umum vmvm amam bmbm

Output

Print nn lines. In the ii-th line, print the maximum amount of snuuk that Kenkoooo has when he reaches City tt if he goes on a trip from City ss to City tt after i−1i−1 years.


Sample Input 1 Copy

Copy
4 3 2 3
1 4 1 100
1 2 1 10
1 3 20 1

Sample Output 1 Copy

Copy
999999999999998
999999999999989
999999999999979
999999999999897

After 00 years, it is optimal to do the exchange in City 11.
After 11 years, it is optimal to do the exchange in City 22.
Note that City 11 can still be visited even after the exchange office is closed. Also note that, if it was allowed to keep 11 yen when do the exchange in City 22 and change the remaining yen into snuuk, we could reach City 33 with 999999999999998999999999999998 snuuk, but this is NOT allowed.
After 22 years, it is optimal to do the exchange in City 33.
After 33 years, it is optimal to do the exchange in City 44. Note that the same train can be used multiple times.


Sample Input 2 Copy

Copy
8 12 3 8
2 8 685087149 857180777
6 7 298270585 209942236
2 4 346080035 234079976
2 5 131857300 22507157
4 8 30723332 173476334
2 6 480845267 448565596
1 4 181424400 548830121
4 5 57429995 195056405
7 8 160277628 479932440
1 6 475692952 203530153
3 5 336869679 160714712
2 7 389775999 199123879

Sample Output 2 Copy

Copy
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994
999999574976994 题意是说求出在第i年后从s城到t城剩下多少snuuk,每个城都可以进行交换,把所有的yen换成等量snuuk,但是限制是在i年之前才可以在第i城进行交换,否则就不能再在第i城交换了,那么就需要求出到各个城进行交换的话各自会最多剩下多少snuuk,其实就是求最短路,在这个点之前只能用yen,要求yen的最小消耗路径,在这个点之后只能用snuuk,要求snuuk的最小消耗,需要求两个单源最短路径,即s到各个城的yen最短路和t到各个城的snuuk最短路径,
然后用总的10^15减去每个城对应的yen和snuuk最小消耗,就是在这个城做交换后到达终点所剩下的snuuk,根据题目数据范围,肯定会有剩余。
用邻接表存城市间的yen和snuuk消耗,然后用spfa算法求得两个最短路。然后倒着更新第i年的剩余(10^15减去第i+1城到n城中的最小消耗)。
代码:
import java.util.*;

public class Main {
static final long all = (long)1e15;
static int n,m,s,t;
static int u[],v[],a[],b[],first[],next[];
static boolean vis[];
static long yen[],snuuk[],cost[],mincost = all;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
s = in.nextInt();
t = in.nextInt();
u = new int[m * 2 + 1];
v = new int[m * 2 + 1];
a = new int[m * 2 + 1];
b = new int[m * 2 + 1];
first = new int[m * 2 + 1];
next = new int[m * 2 + 1];
vis = new boolean[n + 1];
yen = new long[n + 1];
snuuk = new long[n + 1];
cost = new long[n + 1];
Arrays.fill(first, -1);
Arrays.fill(yen,all);
Arrays.fill(snuuk, all);
for(int i = 0;i < m;i ++) {
u[i] = in.nextInt();
v[i] = in.nextInt();
a[i] = in.nextInt();
b[i] = in.nextInt();
u[i + m] = v[i];
v[i + m] = u[i];
a[i + m] = a[i];
b[i + m] = b[i];
next[i] = first[u[i]];
first[u[i]] = i;
next[i + m] = first[u[i + m]];
first[u[i + m]] = i + m;
}
yen[s] = 0;
int head = 0,tail = 0;
int queue[] = new int[n];
queue[tail ++] = s;
vis[s] = true;
while(head != tail) {
int k = first[queue[head]];
while(k != -1) {
if(yen[u[k]] + a[k] < yen[v[k]]) {
yen[v[k]] = yen[u[k]] + a[k];
if(!vis[v[k]]) {
queue[tail] = v[k];
vis[v[k]] = true;
tail = (tail + 1) % n;
}
}
k = next[k];
}
vis[queue[head]] = false;
head = (head + 1) % n;
}
snuuk[t] = 0;
head = tail = 0;
queue[tail ++] = t;
vis[t] = true;
while(head != tail) {
int k = first[queue[head]];
while(k != -1) {
if(snuuk[u[k]] + b[k] < snuuk[v[k]]) {
snuuk[v[k]] = snuuk[u[k]] + b[k];
if(!vis[v[k]]) {
queue[tail] = v[k];
vis[v[k]] = true;
tail = (tail + 1) % n;
}
}
k = next[k];
}
vis[queue[head]] = false;
head = (head + 1) % n;
}
for(int i = n;i > 0;i --) {
cost[i - 1] = Math.min(mincost, yen[i] + snuuk[i]);
mincost = Math.min(mincost, cost[i - 1]);
cost[i - 1] = all - cost[i - 1];
}
for(int i = 0;i < n;i ++) {
System.out.println(cost[i]);
}
}
}

E - + Graph


Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 600600 points

Problem Statement

Kenkoooo found a simple connected graph. The vertices are numbered 11 through nn. The ii-th edge connects Vertex uiui and vivi, and has a fixed integer sisi.

Kenkoooo is trying to write a positive integer in each vertex so that the following condition is satisfied:

  • For every edge ii, the sum of the positive integers written in Vertex uiui and vivi is equal to sisi.

Find the number of such ways to write positive integers in the vertices.

Constraints

  • 2≤n≤1052≤n≤105
  • 1≤m≤1051≤m≤105
  • 1≤ui<vi≤n1≤ui<vi≤n
  • 2≤si≤1092≤si≤109
  • If i≠ji≠j, then ui≠ujui≠uj or vi≠vjvi≠vj.
  • The graph is connected.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

nn mm
u1u1 v1v1 s1s1
::
umum vmvm smsm

Output

Print the number of ways to write positive integers in the vertices so that the condition is satisfied.


Sample Input 1 Copy

Copy
3 3
1 2 3
2 3 5
1 3 4

Sample Output 1 Copy

Copy
1

The condition will be satisfied if we write 1,21,2 and 33 in vertices 1,21,2 and 33, respectively. There is no other way to satisfy the condition, so the answer is 11.


Sample Input 2 Copy

Copy
4 3
1 2 6
2 3 7
3 4 5

Sample Output 2 Copy

Copy
3

Let a,b,ca,b,c and dd be the numbers to write in vertices 1,2,31,2,3 and 44, respectively. There are three quadruples (a,b,c,d)(a,b,c,d) that satisfy the condition:

  • (a,b,c,d)=(1,5,2,3)(a,b,c,d)=(1,5,2,3)
  • (a,b,c,d)=(2,4,3,2)(a,b,c,d)=(2,4,3,2)
  • (a,b,c,d)=(3,3,4,1)(a,b,c,d)=(3,3,4,1)

Sample Input 3 Copy

Copy
8 7
1 2 1000000000
2 3 2
3 4 1000000000
4 5 2
5 6 1000000000
6 7 2
7 8 1000000000

Sample Output 3 Copy

Copy
0
一个简单连通图,给出图中每条边的权值,要求为每个点赋正值,使得每条边两端点的值的和要等于边的权值,求有几种方法可以满足要求。暴力绝对是不可行的,一般是先考虑暴力,但是并不是说就应该先考虑暴力,其实题目要求满足的是ai + aj = sij,sij是已知i点和j点所连边的权值,ai和aj是未知的,这就是一个二元一次方程,而所有给出的关系,实际上构成了一个n元一次方程组。
现在要求这个n元一次方程组的解的个数,并不需要把解一一求出,可以把所有的方程都转化为一点和其他各点的关系,比如转化为编号为1的点的关系。
比如说样例1:
a1 + a2 = 3 -> a2 = 3 - a1 > 0
a2 + a3 = 4
a3 + a1 = 5 -> a3 = 5 - a1 > 0
这是题目给出的,通过相减可转化为
a3 - a1 = 1 -> a3 = a1 + 1 > 0
a2 - a1 = -1 -> a2 = a1 - 1 > 0
所有点的值必须为正,综合以上得到这几个条件:a1 < 3,a1 < 5,a1 > -1,a1 > 1,a1 > 0.
所以1 < a1 < 3,a1 = 2,一个解。这个样例是一个直角三角形,是个环。
再看样例2:
得到a1 + a2 = 6,a3 - a1 = 1,a4 + a1 = 4,综合0 < a1 < 4,三个解,这个样例没有环,遍历一般就可以出答案,范围如果矛盾那肯定是无解。
由于题目要求是整数,方程计算的话难免会有小数吧。。但是int计算的出来的是int,不会是小数,会不会蒙混过关呢。。
看这组样例:
a1 + a2 = 4
a2 + a3 = 4
a3 + a4 = 4
a4 + a1 = 4
a1 + a3 = 5
无解,可以得出的范围是1 < a1 < 4,还需要进一步观察。有环的图可能会得出关于一个点的两个关系,比如这组样例可以得出a2 - a1 = -1,a2 + a1 = 4,求得a1 = 2.5,不是整数。
在范围不冲突的前提下,还要判断a1是否可以确定,如果可以确定,则解是小于等于1的。
用邻接表记录图,dfs遍历从一个点开始遍历图,记录所有点和这个点的关系,用一个二维数组rela记录减和加的关系,即ai - a1 = rela[0][i],ai + a1 = rela[1][i],
然后可以确立a1的关系。如果rela[0][i]和rela[1][i]都确定了,可以确定a1的值,至于值是否是解,另加判断。
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define Max 100001
#define inf 1000000000000000
using namespace std;
int n,m,flag;
int first[Max * ],nextv[Max * ],u[Max * ],v[Max * ],s[Max * ];
long long rela[][Max],l = ,r = inf,ans;
void dfs(int from,int tag,long long va) {
if(flag)return;
int k = first[from];
while(k != -) {
if(flag)return;
int vtag = (tag == );
int to = v[k];
long long lva = s[k] - va;
if(rela[vtag][to] != inf) {
if(rela[vtag][to] == lva) {
k = nextv[k];
continue;
}
else {
flag = ;
return;
}
}
rela[vtag][to] = lva;
if(vtag)r = min(r,lva);
else l = max(l,-lva);
if(r - l <= ){///范围冲突 没有解
flag = ;
return;
}
dfs(to,-tag,lva);
k = nextv[k];
}
}
int main() {
scanf("%d%d",&n,&m);
memset(first,-,sizeof(first));
for(int i = ;i <= n;i ++) {
rela[][i] = rela[][i] = inf;
}
for(int i = ;i < m;i ++) {
scanf("%d%d%d",&u[i],&v[i],&s[i]);
u[i + m] = v[i];
v[i + m] = u[i];
s[i + m] = s[i];
nextv[i] = first[u[i]];
first[u[i]] = i;
nextv[i + m] = first[u[i + m]];
first[u[i + m]] = i + m;
}
dfs(,,);///dfs遍历所有点和第一个点的关系,如果有冲突flag为1 冲突就是说得到于同一点的同一关系的值不同比如a1 + a2 = 3 和 a1 + a2 = 4
if(!flag) {///遍历后无冲突
long long temp = -;
for(int i = ;i <= n;i ++) {
if(rela[][i] != inf && rela[][i] != inf) { ///可以确定a1
long long d = rela[][i] - rela[][i];
if(d % )flag = ;
else temp = d / ;
break;
}
}
if(!flag) {
if(temp != -) {///确定a1 解唯一
ans = ;
}
else ans = r - l - ;
}
}
printf("%lld",ans);
return ;
}

SoundHound Inc. Programming Contest 2018的更多相关文章

  1. AtCoder SoundHound Inc. Programming Contest 2018 E + Graph (soundhound2018_summer_qual_e)

    原文链接https://www.cnblogs.com/zhouzhendong/p/AtCoder-SoundHound-Inc-Programming-Contest-2018-E.html 题目 ...

  2. ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syria, Lattakia, Tishreen University, April, 30, 2018

    ACM International Collegiate Programming Contest, Tishreen Collegiate Programming Contest (2018) Syr ...

  3. German Collegiate Programming Contest 2018​ B. Battle Royale

    Battle Royale games are the current trend in video games and Gamers Concealed Punching Circles (GCPC ...

  4. The North American Invitational Programming Contest 2018 D. Missing Gnomes

    A family of nn gnomes likes to line up for a group picture. Each gnome can be uniquely identified by ...

  5. The North American Invitational Programming Contest 2018 H. Recovery

    Consider an n \times mn×m matrix of ones and zeros. For example, this 4 \times 44×4: \displaystyle \ ...

  6. The North American Invitational Programming Contest 2018 E. Prefix Free Code

    Consider nn initial strings of lower case letters, where no initial string is a prefix of any other ...

  7. German Collegiate Programming Contest 2018​ C. Coolest Ski Route

    John loves winter. Every skiing season he goes heli-skiing with his friends. To do so, they rent a h ...

  8. North American Invitational Programming Contest 2018

    A. Cut it Out! 枚举第一刀,那么之后每切一刀都会将原问题划分成两个子问题. 考虑DP,设$f[l][r]$表示$l$点顺时针一直到$r$点还未切割的最小代价,预处理出每条边的代价转移即可 ...

  9. [ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018)]

    https://codeforces.com/gym/101810 A. Careful Thief time limit per test 2.5 s memory limit per test 2 ...

随机推荐

  1. [转]Linux shell中的那些小把戏

    我日常使用Linux shell(Bash),但是我经常忘记一些有用的命令或者shell技巧.是的,我能记住一些命令,但是肯定不会只在特定的任务上使用一次,所以我就开始在我的Dropbox账号里用文本 ...

  2. python解释器分类

    当我们编写Python代码时,我们得到的是一个包含Python代码的以.py为扩展名的文本文件.要运行代码,就需要Python解释器去执行.py文件. 由于整个Python语言从规范到解释器都是开源的 ...

  3. android 怎样单独下载一个项目

    起因,"网络"不太好."比方铁通的就是不如联通的" 每次运行一边repo sync,十分蛋疼,假设不做full build无需所有下载,着急看某个项目的修改但是 ...

  4. java变参

    java变参是通过数组来实现的 Object[] addAll(Object[] array1, Object... array2)和Object[] addAll(Object[] array1, ...

  5. js错误: Unexpected number in JSON at position 2792 value里面有双引号怎么解决

    源头  出现这个报错提示,大家从错误就可以看的出来,这就是json的错误,一般来说都是json格式出现了错误,本人遇到比较多的情况就是json字符串里面出现了一些会影响json格式的符号,这次出现这个 ...

  6. 使用Erlang和Thrift,与Hbase通信(转)

    操作系统是Ubuntu Server 12.10 先安装Thrift sudo apt-get install libboost-dev libboost-test-dev \ libboost-pr ...

  7. css选择器参考手册

    选择器 例子 例子描述 CSS .class .intro 选择 class="intro" 的所有元素. 1 #id #firstname 选择 id="firstna ...

  8. JavaScript学习3:原型和继承

    原型 我们创建的每个函数都有一个prototype(原型)属性,这个属性是一个对象,它的用途是包括能够由特定类型的全部实例共享的属性和方法.逻辑上能够这么理解:prototype是通过调用构造函数而创 ...

  9. TCP的四种定时器简单记录

    TCP管理的4个不同的定时器: 1.重传定时器:用于当希望收到另一端的确认. 2.坚持定时器:使窗口大小信息保持不断流动. 3.保活定时器:检测TCP空闲连接的另一端何时崩溃或重启. 4.2MSL定时 ...

  10. java array

    1 array变量 Type[] array_virable_name; 2 array对象 2.1 new Type[] array_virable_name = new Type[NUM]; 2. ...