SoundHound Inc. Programming Contest 2018
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
4 11
Sample Output 1 Copy
+
4+11=154+11=15.
Sample Input 2 Copy
3 5
Sample Output 2 Copy
*
3×5=153×5=15.
Sample Input 3 Copy
1 1
Sample Output 3 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
abcdefgh
3
Sample Output 1 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
lllll
1
Sample Output 2 Copy
lllll
Sample Input 3 Copy
souuundhound
2
Sample Output 3 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
2 3 1
Sample Output 1 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
1000000000 180707 0
Sample Output 2 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
4 3 2 3
1 4 1 100
1 2 1 10
1 3 20 1
Sample Output 1 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
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
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
3 3
1 2 3
2 3 5
1 3 4
Sample Output 1 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
4 3
1 2 6
2 3 7
3 4 5
Sample Output 2 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
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
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的更多相关文章
- 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 题目 ...
- 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 ...
- German Collegiate Programming Contest 2018 B. Battle Royale
Battle Royale games are the current trend in video games and Gamers Concealed Punching Circles (GCPC ...
- 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 ...
- 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 \ ...
- 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 ...
- 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 ...
- North American Invitational Programming Contest 2018
A. Cut it Out! 枚举第一刀,那么之后每切一刀都会将原问题划分成两个子问题. 考虑DP,设$f[l][r]$表示$l$点顺时针一直到$r$点还未切割的最小代价,预处理出每条边的代价转移即可 ...
- [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 ...
随机推荐
- 简单的js表单验证框架
/** * 通常在我们的HTML页面表单中有大量的数据验证工作, * 免不了要写很多验证表单的js代码,这是一项非常繁琐 * 枯燥的工作.很多程序员也会经常遗漏这项工作.当然 * 一些JavaEE框架 ...
- 渐变背景(background)效果
chrom and Safari浏览器: webkit核心的浏览器.使用CSS3渐变方法(css-gradient) -webkit-gradient(type, start_point, end_p ...
- 深入Asyncio(十二)Asyncio与单元测试
Testing with asyncio 之前有说过应用开发者不需要将loop当作参数在函数间传递,只需要调用asyncio.get_event_loop()即可获得.但是在写单元测试时,可能会需要用 ...
- SAP Sybase SQLAnywhere[ASA]数据库中数据行的存储机制
SQLAnywhere[ASA]数据库(以下简称ASA)中的数据库文件,是如何存储普通的表的记录行呢?插入.更新.删除时,记录行的存储会有什么变化? 了解了这些,才能更好的理解如何对ASA数据库进行调 ...
- Executors几种常用的线程池性能比较
java编程中,经常会利用Executors的newXXXThreasPool生成各种线程池,今天写了一小段代码,简单测试了下三种常用的线程池: import com.google.common.ut ...
- 用变量a给出下面的定义。[中国台湾某著名CPU生产公司2005年面试题]
(1)一个整型数(An integer)(2)一个指向整型数的指针(A pointer to an integer)(3)一个指向指针的指针,它指向的指针是指向一个整型数(A pointer to a ...
- 【题解】[P3557 POI2013]GRA-Tower Defense Game
[题解][P3557 POI2013]GRA-Tower Defense Game 这道题是真的** 根据题目给的\(k\),可以知道,我们随便放塔,只要不全放一起,一定是一种合法的方案. 直接枚举就 ...
- 近年来世界各地ICO的花式骗局盘点
很多人说区块链是骗局,其实不然,区块链是一种安全的互联网技术,可以解决当下很多行业的痛点,但也确实存在一些不法分子利用区块链进行行骗,下面整理了世界各地的一些ICO骗局,一起来看看吧. 案例一:越南I ...
- Table control 相关
转:晚上回去有时间看看 http://help.sap.com/saphelp_nw04/helpdata/en/9f/dbac1d35c111d1829f0000e829fbfe/frameset. ...
- 堆中的路径 【Heap】
7-2 堆中的路径(25 分) 将一系列给定数字插入一个初始为空的小顶堆H[].随后对任意给定的下标i,打印从H[i]到根结点的路径. 输入格式: 每组测试第1行包含2个正整数N和M(≤1000),分 ...