Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
double x, y, l, w;
double cal(double ang) {
double s, h;
s = l * cos(ang) + w * sin(ang) - y;
h = s * tan(ang) + w * cos(ang);
return h;
}
bool solve() {
double ang1, ang2, m, mm, mid, midmid;
ang1 = 0.0;
ang2 = acos(-1.0) / 2;
if (x < w || y < w) {
return 0;
}
while (ang2 - 0.0000001 > ang1) {
mid = (ang1 + ang2) / 2;
midmid = (mid + ang2) / 2;
m = cal(mid);
mm = cal(midmid);
if (mm > m)
ang1 = mid;
else
ang2 = midmid;
}
if (m > x)
return 0;
else
return 1;
}
double MIN(double a, double b) { return a < b ? a : b; }
double MAX(double a, double b) { return a > b ? a : b; }
int main() {
double ang1, ang2, m, mm, mid, midmid, left, right;
int i;
while (scanf("%lf%lf%lf", &x, &y, &l) != EOF) {
left = 0.0;
right = x;
if (x > y) right = y;
if (right > l) right = l;
for (i = 0; i < 2000; i++) {
w = (left + right) / 2;
if (solve()) {
left = w;
} else
right = w;
}
if (w > l) w = l;
if (l <= MAX(x, y)) {
if (w < MIN(x, y)) w = MIN(x, y);
}
if (w > l) w = l;
if (w < 1.0e-12)
printf("My poor head =(\n");
else
printf("%.7lf\n", w);
}
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
double a, b, l;
double w;
inline double f(double x) {
return (a * cos(x) + b * sin(x) - l * cos(x) * sin(x));
}
void solve() {
double r1 = 0, r2 = acos(0.0);
for (int i = 0; i < 1000; i++) {
double r11 = (2.0 / 3.0) * r1 + (1.0 / 3.0) * r2;
double r22 = (1.0 / 3.0) * r1 + (2.0 / 3.0) * r2;
if (f(r11) > f(r22))
r1 = r11;
else
r2 = r22;
}
w = max(w, f((r1 + r2) / 2));
w = min(w, l);
if (w < 1e-7)
printf("My poor head =(\n");
else
printf("%.7lf\n", w);
}
int main() {
scanf("%lf%lf%lf", &a, &b, &l);
w = 0;
if (a > b) swap(a, b);
if (l <= a) {
printf("%.7lf\n", l);
return 0;
}
if (l <= b) {
printf("%.7lf\n", a);
return 0;
}
solve();
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 |
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
/**
* @author Ivan Pryvalov ([email protected])
*/
public class Codeforces78_Div2_ProblemE implements Runnable{
double A,B,L;
double EPS = 1e-10;
private void solve() throws IOException {
A = scanner.nextInt();
B = scanner.nextInt();
L = scanner.nextInt();
double w = 0;
if (B-L > -EPS || A-L > -EPS){
w = Math.min(L, Math.min(A, B));
out.println(w);
return;
}
if (!check(w)){
out.println("My poor head =(");
}else{
double w2 = 1e-8;
while (check(w2)){
w2 *= 2;
}
double w1 = w2/2.0;
w = binarySearch(w1,w2);
out.println(w);
}
}
private double binarySearch(double w1, double w2) {
while (Math.abs(w2-w1)>EPS){
double w = (w1+w2)/2.0;
if (check(w)){
w1 = w;
}else{
w2 = w;
}
}
return (w1+w2)/2.0;
}
private boolean check(double w) {
double minL = binarySearchMinL(w, 0, Math.PI/2);
return minL - L > -EPS && L-w > -EPS;
}
private double binarySearchMinL(double w, double l, double r) {
while (Math.abs(l-r)>EPS){
double c = (l+r)/2.0;
double dc = 1e-8;
double fC = getL(c,w);
double fC2 = getL(c+dc,w);
if (fC2 > fC){
r = c;
}else{
l = c;
}
}
return getL(l, w);
}
public double getL(double a, double w){
return A/Math.cos(a) + B/Math.sin(a) - 2*w/Math.sin(2*a);
}
/////////////////////////////////////////////////
final int BUF_SIZE = 1024 * 1024 * 8;//important to read long-string tokens properly
final int INPUT_BUFFER_SIZE = 1024 * 1024 * 8 ;
final int BUF_SIZE_INPUT = 1024;
final int BUF_SIZE_OUT = 1024;
boolean inputFromFile = false;
String filenamePrefix = "A-small-attempt0";
String inSuffix = ".in";
String outSuffix = ".out";
//InputStream bis;
//OutputStream bos;
PrintStream out;
ByteScanner scanner;
ByteWriter writer;
@Override
public void run() {
try{
InputStream bis = null;
OutputStream bos = null;
//PrintStream out = null;
if (inputFromFile){
File baseFile = new File(getClass().getResource("/").getFile());
bis = new BufferedInputStream(
new FileInputStream(new File(
baseFile, filenamePrefix+inSuffix)),
INPUT_BUFFER_SIZE);
bos = new BufferedOutputStream(
new FileOutputStream(
new File(baseFile, filenamePrefix+outSuffix)));
out = new PrintStream(bos);
}else{
bis = new BufferedInputStream(System.in, INPUT_BUFFER_SIZE);
bos = new BufferedOutputStream(System.out);
out = new PrintStream(bos);
}
scanner = new ByteScanner(bis, BUF_SIZE_INPUT, BUF_SIZE);
writer = new ByteWriter(bos, BUF_SIZE_OUT);
solve();
out.flush();
}catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public interface Constants{
final static byte ZERO = '0';//48 or 0x30
final static byte NINE = '9';
final static byte SPACEBAR = ' '; //32 or 0x20
final static byte MINUS = '-'; //45 or 0x2d
final static char FLOAT_POINT = '.';
}
public static class EofException extends IOException{
}
public static class ByteWriter implements Constants {
int bufSize = -1;//1024;
byte[] byteBuf = null;//new byte[bufSize];
OutputStream os;
public ByteWriter(OutputStream os, int bufSize){
this.os = os;
this.bufSize = bufSize;
byteBuf = new byte[bufSize];
}
public void writeInt(int num) throws IOException{
int byteWriteOffset = byteBuf.length;
if (num==0){
byteBuf[--byteWriteOffset] = ZERO;
}else{
int numAbs = Math.abs(num);
while (numAbs>0){
byteBuf[--byteWriteOffset] = (byte)((numAbs % 10) + ZERO);
numAbs /= 10;
}
if (num<0) byteBuf[--byteWriteOffset] = MINUS;
}
os.write(byteBuf, byteWriteOffset, byteBuf.length - byteWriteOffset);
}
public void writeLong(long num) throws IOException{
int byteWriteOffset = byteBuf.length;
if (num==0){
byteBuf[--byteWriteOffset] = ZERO;
}else{
long numAbs = Math.abs(num);
while (numAbs>0){
byteBuf[--byteWriteOffset] = (byte)((numAbs % 10) + ZERO);
numAbs /= 10;
}
if (num<0) byteBuf[--byteWriteOffset] = MINUS;
}
os.write(byteBuf, byteWriteOffset, byteBuf.length - byteWriteOffset);
}
/* No need
public void writeDouble(double num) throws IOException{
byte[] data = Double.toString(num).getBytes();
os.write(data,0,data.length);
}*/
//NO NEED: Please ensure ar.length <= byteBuf.length!
/**
*
* @param ar
* @throws IOException
*/
public void writeByteAr(byte[] ar) throws IOException{
/*
for (int i = 0; i < ar.length; i++) {
byteBuf[i] = ar[i];
}
os.write(byteBuf,0,ar.length);*/
os.write(ar,0,ar.length);
}
public void writeSpaceBar() throws IOException{
os.write(SPACEBAR);
}
}
public static class ByteScanner implements Constants{
InputStream is;
public ByteScanner(InputStream is, int bufSizeInput, int bufSize){
this.is = is;
this.bufSizeInput = bufSizeInput;
this.bufSize = bufSize;
byteBufInput = new byte[this.bufSizeInput];
byteBuf = new byte[this.bufSize];
}
public ByteScanner(byte[] data){
byteBufInput = data;
bufSizeInput = data.length;
bufSize = data.length;
byteBuf = new byte[bufSize];
byteRead = data.length;
bytePos = 0;
}
private int bufSizeInput;
private int bufSize;
byte[] byteBufInput;
byte by=-1;
int byteRead=-1;
int bytePos=-1;
static byte[] byteBuf;
int totalBytes;
boolean eofMet = false;
private byte nextByte() throws IOException{
if (bytePos<0 || bytePos>=byteRead){
byteRead = is==null? -1: is.read(byteBufInput);
bytePos=0;
if (byteRead<0){
byteBufInput[bytePos]=-1;//!!!
if (eofMet)
throw new EofException();
eofMet = true;
}
}
return byteBufInput[bytePos++];
}
/**
* Returns next meaningful character as a byte.<br>
*
* @return
* @throws IOException
*/
public byte nextChar() throws IOException{
while ((by=nextByte())<=0x20);
return by;
}
/**
* Returns next meaningful character OR space as a byte.<br>
*
* @return
* @throws IOException
*/
public byte nextCharOrSpacebar() throws IOException{
while ((by=nextByte())<0x20);
return by;
}
/**
* Reads line.
*
* @return
* @throws IOException
*/
public String nextLine() throws IOException {
readToken((byte)0x20);
return new String(byteBuf,0,totalBytes);
}
public byte[] nextLineAsArray() throws IOException {
readToken((byte)0x20);
byte[] out = new byte[totalBytes];
System.arraycopy(byteBuf, 0, out, 0, totalBytes);
return out;
}
/**
* Reads token. Spacebar is separator char.
*
* @return
* @throws IOException
*/
public String nextToken() throws IOException {
readToken((byte)0x21);
return new String(byteBuf,0,totalBytes);
}
/**
* Spacebar is included as separator char
*
* @throws IOException
*/
private void readToken() throws IOException {
readToken((byte)0x21);
}
private void readToken(byte acceptFrom) throws IOException {
totalBytes = 0;
while ((by=nextByte())<acceptFrom);
byteBuf[totalBytes++] = by;
while ((by=nextByte())>=acceptFrom){
byteBuf[totalBytes++] = by;
}
}
public int nextInt() throws IOException{
readToken();
int num=0, i=0;
boolean sign=false;
if (byteBuf[i]==MINUS){
sign = true;
i++;
}
for (; i<totalBytes; i++){
num*=10;
num+=byteBuf[i]-ZERO;
}
return sign?-num:num;
}
public long nextLong() throws IOException{
readToken();
long num=0;
int i=0;
boolean sign=false;
if (byteBuf[i]==MINUS){
sign = true;
i++;
}
for (; i<totalBytes; i++){
num*=10;
num+=byteBuf[i]-ZERO;
}
return sign?-num:num;
}
/*
//TODO test Unix/Windows formats
public void toNextLine() throws IOException{
while ((ch=nextChar())!='\n');
}
*/
public double nextDouble() throws IOException{
readToken();
char[] token = new char[totalBytes];
for (int i = 0; i < totalBytes; i++) {
token[i] = (char)byteBuf[i];
}
return Double.parseDouble(new String(token));
}
}
public static void main(String[] args) {
new Codeforces78_Div2_ProblemE().run();
}
}
| JAVA |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
double a, b, l;
double solve(double x) {
return (a + b * tan(x)) * (b + a / tan(x)) / (b / cos(x) + a / (sin(x))) -
l * sin(x) * cos(x);
}
int main() {
cin >> a >> b >> l;
if (l <= max(a, b))
printf("%.12f\n", min(min(a, b), l));
else {
double L = 0;
double R = acos(-1.0) * 0.5;
while (R - L > 1e-14) {
double mid = (L + R) * 0.5;
double midmid = (mid + R) * 0.5;
if (solve(mid) < solve(midmid))
R = midmid;
else
L = mid;
}
double ans = min(solve(L), l);
if (ans < 1e-14)
printf("My poor head =(\n");
else
printf("%.12f\n", ans);
}
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
/**
* @author Ivan Pryvalov ([email protected])
*/
public class Codeforces78_Div2_ProblemE implements Runnable{
// scanner.nextInt()
// new ByteScanner(scanner.nextLineAsArray()).nextInt()
double A,B,L;
double EPS = 1e-10;
private void solve() throws IOException {
A = scanner.nextInt();
B = scanner.nextInt();
L = scanner.nextInt();
double w = 0;
if (!check(w)){
out.println("My poor head =(");
}else{
double w2 = 1e-8;
while (check(w2)){
w2 *= 2;
}
double w1 = w2/2.0;
w = binarySearch(w1,w2);
//check direct
if (B-L > -EPS){
w = Math.max(w, Math.min(L, A));
}
if (A-L > -EPS){
w = Math.max(w, Math.min(L, B));
}
out.println(w);
}
}
private double binarySearch(double w1, double w2) {
while (Math.abs(w2-w1)>EPS){
double w = (w1+w2)/2.0;
if (check(w)){
w1 = w;
}else{
w2 = w;
}
}
return (w1+w2)/2.0;
}
private boolean check(double w) {
double a = Math.PI / 4;
//double minL = getL(a, w);
double minL1 = binarySearchMinL(w, a, Math.PI/2);
double minL2 = binarySearchMinL(w, 0, a);
double minL = Math.min(minL1, minL2);
return minL - L > -EPS && L-w > -EPS && A-w > -EPS && B-w > -EPS; //-EPS;
}
private double binarySearchMinL(double w, double l, double r) {
while (Math.abs(l-r)>EPS){
double dx = (r-l)/3.0;
double a1 = l+dx;
double a2 = a1 + dx;
double fA1 = getL(a1,w);
double fA2 = getL(a2,w);
if (fA1 < fA2){
r = a2;
}else{
l = a1;
}
}
return getL(l, w);
}
public double getL(double a, double w){
return A/Math.cos(a) + B/Math.sin(a) - 2*w/Math.sin(2*a);
}
/////////////////////////////////////////////////
final int BUF_SIZE = 1024 * 1024 * 8;//important to read long-string tokens properly
final int INPUT_BUFFER_SIZE = 1024 * 1024 * 8 ;
final int BUF_SIZE_INPUT = 1024;
final int BUF_SIZE_OUT = 1024;
boolean inputFromFile = false;
String filenamePrefix = "A-small-attempt0";
String inSuffix = ".in";
String outSuffix = ".out";
//InputStream bis;
//OutputStream bos;
PrintStream out;
ByteScanner scanner;
ByteWriter writer;
@Override
public void run() {
try{
InputStream bis = null;
OutputStream bos = null;
//PrintStream out = null;
if (inputFromFile){
File baseFile = new File(getClass().getResource("/").getFile());
bis = new BufferedInputStream(
new FileInputStream(new File(
baseFile, filenamePrefix+inSuffix)),
INPUT_BUFFER_SIZE);
bos = new BufferedOutputStream(
new FileOutputStream(
new File(baseFile, filenamePrefix+outSuffix)));
out = new PrintStream(bos);
}else{
bis = new BufferedInputStream(System.in, INPUT_BUFFER_SIZE);
bos = new BufferedOutputStream(System.out);
out = new PrintStream(bos);
}
scanner = new ByteScanner(bis, BUF_SIZE_INPUT, BUF_SIZE);
writer = new ByteWriter(bos, BUF_SIZE_OUT);
solve();
out.flush();
}catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public interface Constants{
final static byte ZERO = '0';//48 or 0x30
final static byte NINE = '9';
final static byte SPACEBAR = ' '; //32 or 0x20
final static byte MINUS = '-'; //45 or 0x2d
final static char FLOAT_POINT = '.';
}
public static class EofException extends IOException{
}
public static class ByteWriter implements Constants {
int bufSize = -1;//1024;
byte[] byteBuf = null;//new byte[bufSize];
OutputStream os;
public ByteWriter(OutputStream os, int bufSize){
this.os = os;
this.bufSize = bufSize;
byteBuf = new byte[bufSize];
}
public void writeInt(int num) throws IOException{
int byteWriteOffset = byteBuf.length;
if (num==0){
byteBuf[--byteWriteOffset] = ZERO;
}else{
int numAbs = Math.abs(num);
while (numAbs>0){
byteBuf[--byteWriteOffset] = (byte)((numAbs % 10) + ZERO);
numAbs /= 10;
}
if (num<0) byteBuf[--byteWriteOffset] = MINUS;
}
os.write(byteBuf, byteWriteOffset, byteBuf.length - byteWriteOffset);
}
public void writeLong(long num) throws IOException{
int byteWriteOffset = byteBuf.length;
if (num==0){
byteBuf[--byteWriteOffset] = ZERO;
}else{
long numAbs = Math.abs(num);
while (numAbs>0){
byteBuf[--byteWriteOffset] = (byte)((numAbs % 10) + ZERO);
numAbs /= 10;
}
if (num<0) byteBuf[--byteWriteOffset] = MINUS;
}
os.write(byteBuf, byteWriteOffset, byteBuf.length - byteWriteOffset);
}
/* No need
public void writeDouble(double num) throws IOException{
byte[] data = Double.toString(num).getBytes();
os.write(data,0,data.length);
}*/
//NO NEED: Please ensure ar.length <= byteBuf.length!
/**
*
* @param ar
* @throws IOException
*/
public void writeByteAr(byte[] ar) throws IOException{
/*
for (int i = 0; i < ar.length; i++) {
byteBuf[i] = ar[i];
}
os.write(byteBuf,0,ar.length);*/
os.write(ar,0,ar.length);
}
public void writeSpaceBar() throws IOException{
os.write(SPACEBAR);
}
}
public static class ByteScanner implements Constants{
InputStream is;
public ByteScanner(InputStream is, int bufSizeInput, int bufSize){
this.is = is;
this.bufSizeInput = bufSizeInput;
this.bufSize = bufSize;
byteBufInput = new byte[this.bufSizeInput];
byteBuf = new byte[this.bufSize];
}
public ByteScanner(byte[] data){
byteBufInput = data;
bufSizeInput = data.length;
bufSize = data.length;
byteBuf = new byte[bufSize];
byteRead = data.length;
bytePos = 0;
}
private int bufSizeInput;
private int bufSize;
byte[] byteBufInput;
byte by=-1;
int byteRead=-1;
int bytePos=-1;
static byte[] byteBuf;
int totalBytes;
boolean eofMet = false;
private byte nextByte() throws IOException{
if (bytePos<0 || bytePos>=byteRead){
byteRead = is==null? -1: is.read(byteBufInput);
bytePos=0;
if (byteRead<0){
byteBufInput[bytePos]=-1;//!!!
if (eofMet)
throw new EofException();
eofMet = true;
}
}
return byteBufInput[bytePos++];
}
/**
* Returns next meaningful character as a byte.<br>
*
* @return
* @throws IOException
*/
public byte nextChar() throws IOException{
while ((by=nextByte())<=0x20);
return by;
}
/**
* Returns next meaningful character OR space as a byte.<br>
*
* @return
* @throws IOException
*/
public byte nextCharOrSpacebar() throws IOException{
while ((by=nextByte())<0x20);
return by;
}
/**
* Reads line.
*
* @return
* @throws IOException
*/
public String nextLine() throws IOException {
readToken((byte)0x20);
return new String(byteBuf,0,totalBytes);
}
public byte[] nextLineAsArray() throws IOException {
readToken((byte)0x20);
byte[] out = new byte[totalBytes];
System.arraycopy(byteBuf, 0, out, 0, totalBytes);
return out;
}
/**
* Reads token. Spacebar is separator char.
*
* @return
* @throws IOException
*/
public String nextToken() throws IOException {
readToken((byte)0x21);
return new String(byteBuf,0,totalBytes);
}
/**
* Spacebar is included as separator char
*
* @throws IOException
*/
private void readToken() throws IOException {
readToken((byte)0x21);
}
private void readToken(byte acceptFrom) throws IOException {
totalBytes = 0;
while ((by=nextByte())<acceptFrom);
byteBuf[totalBytes++] = by;
while ((by=nextByte())>=acceptFrom){
byteBuf[totalBytes++] = by;
}
}
public int nextInt() throws IOException{
readToken();
int num=0, i=0;
boolean sign=false;
if (byteBuf[i]==MINUS){
sign = true;
i++;
}
for (; i<totalBytes; i++){
num*=10;
num+=byteBuf[i]-ZERO;
}
return sign?-num:num;
}
public long nextLong() throws IOException{
readToken();
long num=0;
int i=0;
boolean sign=false;
if (byteBuf[i]==MINUS){
sign = true;
i++;
}
for (; i<totalBytes; i++){
num*=10;
num+=byteBuf[i]-ZERO;
}
return sign?-num:num;
}
/*
//TODO test Unix/Windows formats
public void toNextLine() throws IOException{
while ((ch=nextChar())!='\n');
}
*/
public double nextDouble() throws IOException{
readToken();
char[] token = new char[totalBytes];
for (int i = 0; i < totalBytes; i++) {
token[i] = (char)byteBuf[i];
}
return Double.parseDouble(new String(token));
}
}
public static void main(String[] args) {
new Codeforces78_Div2_ProblemE().run();
}
}
| JAVA |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
ifstream fin("AAtest.in.txt");
double a, b, p, first, c = 0.000000001, vas = -1;
double arv(double alfa) {
return ((b - (p * sin(alfa) - a) / tan(alfa)) * sin(alfa));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr.tie(0);
cin >> a >> b >> p;
double L = 0, R = asin(1), al, ar;
double l = L + (R - L) / 3, r = L + 2 * (R - L) / 3;
int m = 0;
while (r - l > c) {
al = arv(l), ar = arv(r);
if (al < ar)
R = r;
else
L = l;
l = L + (R - L) / 3, r = L + 2 * (R - L) / 3;
m++;
}
if (p <= a) {
vas = min(p, b);
}
double vas1 = -1;
if (p <= b) {
vas1 = min(p, a);
}
vas = max(vas, vas1);
vas1 = min(p, al);
vas = max(vas, vas1);
if (vas <= 0)
cout << "My poor head =(";
else
cout << setprecision(10) << vas;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long a, b, l;
const double pi = acos(-1);
const double delta = 0.000001;
double d(double theta) {
return (a + b * tan(theta) - l * sin(theta)) * cos(theta);
}
double solve() {
double theta = delta;
double D = 1e18;
while (theta < pi / 2) {
D = min(D, d(theta));
theta += delta;
}
return D;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
cin >> a >> b >> l;
double ans;
if (l <= b)
ans = min(l, a);
else if (l <= a)
ans = min(l, b);
else {
ans = solve();
if (ans < 0) return cout << "My poor head =(", 0;
}
cout << ans;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
template <typename T>
inline T sqr(T a) {
return a * a;
}
template <typename T>
inline void relaxMin(T &a, T b) {
if (b < a) a = b;
}
template <typename T>
inline void relaxMax(T &a, T b) {
if (b > a) a = b;
}
const int INF = (int)1E9;
const long double EPS = 1E-12;
const long double PI = 3.1415926535897932384626433832795;
int a, b;
long double dist(long double len, long double w, long double start) {
long double dx = sqrt(sqr(len) - sqr(start));
long double A = start;
long double B = dx;
long double C = -start * dx;
return (A * a + B * b + C) / sqrt(A * A + B * B);
}
bool check(long double len, long double w) {
if (w > a) return false;
if (len - EPS < b && w - EPS < a) return true;
long double l = 0, r = len;
for (int i = 0, _n(250); i < _n; i++) {
long double m1 = l + (r - l) / 3, m2 = r - (r - l) / 3;
long double d1, d2;
d1 = dist(len, w, m1);
d2 = dist(len, w, m2);
if (dist(len, w, m1) > dist(len, w, m2))
l = m1;
else
r = m2;
}
long double d0 = dist(len, w, l) - w;
return d0 > 0;
l = 0;
for (int i = 0, _n(250); i < _n; i++) {
long double m1 = l + (r - l) / 3, m2 = r - (r - l) / 3;
long double d1, d2;
d1 = dist(len, w, m1) - w;
d2 = dist(len, w, m2) - w;
if (fabs(dist(len, w, m1) - w) > fabs(dist(len, w, m2) - w))
l = m1;
else
r = m2;
}
return l < r;
}
void solve() {
int len;
cin >> a >> b >> len;
long double l = 0.0, r = min(len + 0.0, sqrt(a * a + b * b + 0.0));
for (int i = 0, _n(300); i < _n; i++) {
long double mid = (l + r) / 2.0;
if (check(len, mid) || check(mid, len))
l = mid;
else
r = mid;
}
if (l < 1e-10)
puts("My poor head =(");
else
printf("%.10lf", l);
}
int main() {
solve();
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
double a, b, l;
double pi = acos((double)-1);
inline double solve() {
if (a > b) swap(a, b);
if (l <= a)
return l;
else if (l <= b)
return a;
else {
double th = atan(cbrt(a / b));
double maxl = b / cos(th) + a / sin(th);
if (l > maxl + 1e-7) return -1000;
double ll = 0, rr = pi / 2;
for (int k = 0; k < 50; k++) {
double mm = (ll + rr) / 2;
double d = -a * sin(mm) + b * cos(mm) - l * cos(2 * mm);
if (d < 0)
ll = mm;
else
rr = mm;
}
return a * cos(ll) + b * sin(ll) - l * sin(2 * ll) / 2;
}
}
int main() {
scanf("%lf%lf%lf", &a, &b, &l);
double t = solve();
if (t >= 0)
printf("%.7lf\n", solve());
else
puts("My poor head =(");
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int l, a, b, e, L;
double nw, xw, mw, A, B, p, u, ty;
inline double d(double y) {
A = 1. / sqrt(L - y * y);
B = 1. / y;
u = sqrt(A * A + B * B);
ty = (1. + mw * u - A * b) / B;
if (ty > a + 1e-9) e = 1;
return a - ty;
}
inline bool cd() {
double ny = 0, xy = l, m1, m2;
e = 0;
while (fabs(xy - ny) > 1e-9) {
m1 = ny + (xy - ny) / 3;
m2 = ny + 2 * (xy - ny) / 3;
if (d(m1) < d(m2))
xy = m2;
else
ny = m1;
if (e) return 0;
}
return 1;
}
int main() {
scanf("%d %d %d", &a, &b, &l);
L = l * l;
if (a > b) swap(a, b);
if (l <= b) {
printf("%d\n", min(l, a));
return 0;
}
nw = 0;
xw = l;
while (fabs(xw - nw) > 1e-9) {
mw = (nw + xw) / 2;
if (cd())
nw = mw;
else
xw = mw;
}
if (xw < 1e-9)
puts("My poor head =(");
else
printf("%.9lf\n", nw);
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
#pragma comment(linker, "/STACK:33554432")
using namespace std;
const double PI = 2 * acos(0.0);
const double EPS = 1e-12;
const double BEPS = 1e-8;
const int INF = (1 << 30) - 1;
double a, b, w, lo, hi, mi1, mi2;
double f(double x) {
double y = ((w) * (w) - (x) * (x) < 0 ? 0 : sqrt((w) * (w) - (x) * (x)));
return ((b - x) * y + x * a) / w;
};
double solve() {
if (w < b + BEPS) {
if (w < a + BEPS)
return w;
else
return a;
};
lo = 0;
hi = w;
int cnt = 0;
while (cnt < 100) {
cnt++;
mi1 = (2 * lo + hi) / 3;
mi2 = (lo + 2 * hi) / 3;
if (lo == mi1 || mi1 == mi2 || mi2 == hi) break;
if (f(mi1) < f(mi2))
hi = mi2;
else
lo = mi1;
};
mi1 = (2 * lo + hi) / 3;
mi2 = (lo + 2 * hi) / 3;
double res = (f(lo) < (f(mi1) < (f(mi2) < f(hi) ? f(mi2) : f(hi))
? f(mi1)
: (f(mi2) < f(hi) ? f(mi2) : f(hi)))
? f(lo)
: (f(mi1) < (f(mi2) < f(hi) ? f(mi2) : f(hi))
? f(mi1)
: (f(mi2) < f(hi) ? f(mi2) : f(hi))));
res = (res < (f((lo + mi1) / 2) < (f((mi1 + mi2) / 2) < f((mi2 + hi) / 2)
? f((mi1 + mi2) / 2)
: f((mi2 + hi) / 2))
? f((lo + mi1) / 2)
: (f((mi1 + mi2) / 2) < f((mi2 + hi) / 2)
? f((mi1 + mi2) / 2)
: f((mi2 + hi) / 2)))
? res
: (f((lo + mi1) / 2) < (f((mi1 + mi2) / 2) < f((mi2 + hi) / 2)
? f((mi1 + mi2) / 2)
: f((mi2 + hi) / 2))
? f((lo + mi1) / 2)
: (f((mi1 + mi2) / 2) < f((mi2 + hi) / 2)
? f((mi1 + mi2) / 2)
: f((mi2 + hi) / 2))));
return res;
};
int main() {
cin >> a >> b >> w;
if (a > b) swap(a, b);
double ans = solve();
if (ans < EPS)
printf("My poor head =(", ans);
else
printf("%.18lf", ans);
return 0;
};
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename F>
double ternary_search(double lb, double ub, double prec, F fun) {
for (;;) {
if (ub - lb < prec) return (ub + lb) / 2;
double lmid = (2 * lb + ub) / 3, umid = (lb + 2 * ub) / 3;
if (fun(lmid) > fun(umid))
lb = lmid;
else
ub = umid;
}
}
struct dist {
double l, a, b;
dist(double l, double a, double b) : l(l), a(a), b(b) {}
double operator()(double x) {
double h = sqrt(l * l - x * x);
return (h * a + b * x - x * h) / l;
}
};
int main() {
int a, b, l;
cin >> a >> b >> l;
if (a > b) swap(a, b);
double ret;
if (l <= a)
ret = l;
else if (l <= b)
ret = a;
else {
dist fun(l, a, b);
double x0 = ternary_search(0, l, 1e-8, fun);
ret = fun(x0);
}
if (ret < 1e-8)
puts("My poor head =(");
else
printf("%.7f\n", ret);
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const double EPS = 1e-10;
int a, b, l;
double calc(double j) { return b * cos(j) - l * sin(j) * cos(j) + a * sin(j); }
double cal(double j) { return a / cos(j) + b / sin(j); }
int main() {
scanf("%d%d%d", &a, &b, &l);
double x = 0;
double ll = 0, rr = PI / 2, mid1, mid2;
while (ll + EPS < rr) {
mid1 = (ll + rr) / 2;
mid2 = (mid1 + rr) / 2;
double ans1 = cal(mid1);
double ans2 = cal(mid2);
if (ans2 > ans1)
rr = mid2;
else
ll = mid1;
}
double L = cal(ll);
if (l >= L) {
printf("My poor head =(\n");
return 0;
}
if (a < b) swap(a, b);
if (l <= a) {
printf("%d\n", min(b, l));
return 0;
}
ll = 0;
rr = PI / 2;
while (ll + EPS < rr) {
mid1 = (ll + rr) / 2;
mid2 = (mid1 + rr) / 2;
double ans1 = calc(mid1);
double ans2 = calc(mid2);
if (ans2 > ans1)
rr = mid2;
else
ll = mid1;
}
printf("%.7lf\n", calc(mid2));
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
double a, b, l, w, wl, wr, ans, u, ul, ur, u1, u2;
double v(double ax, double ay, double bx, double by, double cx, double cy) {
return (bx - ax) * (cy - ay) - (by - ay) * (cx - ax);
}
double f(double w, double u) {
double vx, vy, ax, ay, bx, by, xi, xj, x, ox, oy;
vx = cos(u);
vy = sin(u);
ax = vx * w;
ay = vy * w;
bx = ax - vy * l;
by = ay + vx * l;
ox = -vy * l;
oy = vx * l;
if (v(0, a, bx, by, b, a) >= 0) return -u;
xi = 0;
xj = 1e6;
for (int it = 0; it < 75; ++it) {
x = (xi + xj) / 2;
if (v(ax + x, ay, bx + x, by, b, a) >= 0)
xj = x;
else
xi = x;
}
x = (xi + xj) / 2;
return b - (ox + x);
}
int main() {
cin >> a >> b >> l;
ans = 0.0;
if (l <= b)
ans = ((ans) > (a) ? (ans) : (a));
else {
if (l <= a)
ans = ((ans) > (b) ? (ans) : (b));
else {
wl = 0;
wr = ((a) < (b) ? (a) : (b)) + 1;
for (int it1 = 0; it1 < 75; ++it1) {
w = (wl + wr) / 2;
ul = 0;
ur = 3.14159265358979323846 / 2;
for (int it2 = 0; it2 < 150; ++it2) {
u1 = ul + (ur - ul) / 3;
u2 = ur - (ur - ul) / 3;
if (f(w, u1) > f(w, u2))
ur = u2;
else
ul = u1;
}
u = (u1 + u2) / 2;
if (f(w, u) > b)
wr = w;
else
wl = w;
}
ans = ((ans) > (w) ? (ans) : (w));
}
}
ans = ((ans) < (l) ? (ans) : (l));
if (((ans) > 0 ? (ans) : -(ans)) < 1e-9)
printf("My poor head =(\n");
else
printf("%.15lf\n", ans);
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
double a, b, len;
double judge(double x) {
return (b * sqrt(len * len - x * x) + a * x - x * sqrt(len * len - x * x)) /
len;
}
double ternary(double l, double r) {
if (fabs(l - r) < EPS) return l;
double L = l + (r - l) / 3;
double R = r - (r - l) / 3;
if (judge(L) < judge(R)) return ternary(l, R);
return ternary(L, r);
}
void solve() {
scanf("%lf%lf%lf", &a, &b, &len);
if (len <= min(a, b)) {
printf("%.8lf\n", len);
return;
}
if (len <= max(a, b)) {
printf("%.8lf\n", min(a, b));
return;
}
double ans = judge(ternary(0, len));
if (ans > 0)
printf("%.8lf\n", ans);
else
printf("My poor head =(\n");
}
int main() {
solve();
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-8;
double a, b, l, lo, hi, lx, hx, lv, hv, sol;
double dot(const complex<double> &a, const complex<double> &b) {
return real(conj(a) * b);
}
struct L : public vector<complex<double> > {
L(const complex<double> &a, const complex<double> &b) {
push_back(a);
push_back(b);
}
L() {}
};
complex<double> projection(const L &l, const complex<double> &p) {
double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]);
return l[0] + t * (l[0] - l[1]);
}
double distanceLP(const L &l, const complex<double> &p) {
return abs(p - projection(l, p));
}
double val(double x) {
complex<double> corner = complex<double>(a, b);
complex<double> pl = complex<double>(0, x);
complex<double> pr = complex<double>(sqrt(l * l - x * x), 0);
return distanceLP(L(pl, pr), corner);
}
int main() {
cout << setprecision(10) << fixed;
cin >> a >> b >> l;
if (a > b) swap(a, b);
if (l <= a) {
cout << l << endl;
} else if (l <= b) {
cout << a << endl;
} else {
lo = 0;
hi = l;
for (int it = 0; it < 1000; it++) {
lx = lo + 1 * (hi - lo) / 3;
hx = lo + 2 * (hi - lo) / 3;
lv = val(lx);
hv = val(hx);
if (lv < hv)
hi = hx;
else
lo = lx;
}
sol = val(lo);
if (sol < EPS)
cout << "My poor head =(" << endl;
else
cout << sol << endl;
}
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int a, b, l;
double f(double p) {
return (p * a + sqrt(l * l - p * p) * b - p * sqrt(l * l - p * p)) / l;
}
int main() {
scanf("%d%d%d", &a, &b, &l);
if (a > b) swap(a, b);
if (l <= a) {
printf("%d\n", l);
return 0;
}
if (l <= b) {
printf("%d\n", a);
return 0;
}
double l1 = 0, l2 = l, m1, m2;
for (int _ = 300; _--;) {
m1 = (l1 * 2 + l2) / 3, m2 = (l1 + l2 * 2) / 3;
if (f(m1) > f(m2))
l1 = m1;
else
l2 = m2;
}
if (f(l1) < 1e-8)
puts("My poor head =(");
else
printf("%.9lf\n", f(l1));
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | import java.io.*;
import java.util.*;
public class E {
private static Reader in;
private static PrintWriter out;
private static final double EPS = 1e-7;
private static double a, b, l;
public static void main(String[] args) throws IOException {
in = new Reader();
out = new PrintWriter(System.out, true);
a = in.nextInt(); b = in.nextInt(); l = in.nextInt();
if (l <= b) {
out.printf ("%.7f\n", Math.min (a, l));
out.close();
System.exit(0);
} else if (l <= a) {
out.printf ("%.7f\n", Math.min (b, l));
out.close();
System.exit(0);
}
double lo = 0, hi = Math.PI / 2.;
while (hi - lo >= 1e-9) {
double mid1 = (2 * lo + hi) / 3., mid2 = (lo + 2 * hi) / 3.;
double length1 = f(mid1), length2 = f(mid2);
if (length1 < length2) hi = mid2;
else lo = mid1;
}
if (f(lo) < EPS) {
out.printf ("My poor head =(\n");
out.close();
System.exit(0);
}
out.printf ("%.7f\n", f(lo));
out.close();
System.exit(0);
}
private static double f (double angle) {
double sin = Math.sin (angle), cos = Math.cos (angle);
return a * cos + b * sin - l * cos * sin;
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[1024];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.')
while ((c = read()) >= '0' && c <= '9')
ret += (c - '0') / (div *= 10);
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
} | JAVA |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long double pi = 2 * acos(0.0);
double a, b, l, d;
double R(double x) {
double y, s;
y = sqrt(l * l - x * x);
s = (0 * (a - 0) + b * (0 - y) + x * (y - a)) / 2.0;
if (s < 0) s = -s;
return 2 * s / l;
}
double MAX(double l, double r) {
int i;
for (i = 0; i <= 100; i++) {
double k1, k2, d, F1, F2;
d = (r - l) / 3;
k1 = l + d;
k2 = r - d;
F1 = R(k1);
F2 = R(k2);
if (F1 > F2)
l = k1;
else
r = k2;
}
return R(l);
}
int main() {
cin >> a >> b >> l;
if (a > b) swap(a, b);
if (l <= a && l <= b) {
cout << l << endl;
return 0;
}
if (l >= a && l <= b) {
cout << a << endl;
return 0;
}
double uns = MAX(0, l);
if (uns > l) uns = l;
if (uns > a) uns = a;
if (uns > b) uns = b;
if (uns < (1e-7)) {
cout << "My poor head =(" << endl;
return 0;
}
printf("%.8llf\n", uns);
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
double F(double A, double B, double L, double T) {
return (A * T + B * sqrt(L * L - T * T) - T * sqrt(L * L - T * T)) / L;
}
double GetAns(double A, double B, double L) {
if (L <= A) return min(B, L);
if (L <= B) return min(A, L);
double Left = 0, Right = L;
while (Right - Left > eps) {
double M1 = (Left * 2 + Right) / 3, M2 = (Left + Right * 2) / 3;
if (F(A, B, L, M1) > F(A, B, L, M2))
Left = M1;
else
Right = M2;
}
return F(A, B, L, (Left + Right) / 2);
}
int main() {
double A, B, L;
cin >> A >> B >> L;
double Ans = GetAns(A, B, L);
if (Ans < eps)
cout << "My poor head =(" << endl;
else
printf("%0.10lf\n", Ans);
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long a, b, l;
const double pi = acos(-1);
double d(double theta) {
return (a + b * tan(theta) - l * sin(theta)) * cos(theta);
}
double solve() {
double beg = 0, end = pi / 2;
while (end - beg > 1e-8) {
double m1 = beg + (end - beg) / 3;
double m2 = end - (end - beg) / 3;
if (d(m1) < d(m2))
end = m2;
else
beg = m1;
}
return d(beg);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(10);
cin >> a >> b >> l;
double ans;
if (l <= b)
ans = min(l, a);
else if (l <= a)
ans = min(l, b);
else {
ans = solve();
if (ans < 0) return cout << "My poor head =(", 0;
}
cout << ans;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | EPS = 1e-8
def cross(a, b):
return (a[0] * b[1]) - (a[1] * b[0])
def f(a, b, l, x):
y = (l*l - x*x)**0.5
return cross( (a-x, b), (-x, y) )
def main():
a, b, l = map(int, raw_input().split())
if a > b:
a, b = b, a
if l <= a and a <= b:
print "%.9lf" % l
elif a < l and l <= b:
print "%.9lf" % a
else:
lo = 0.0
hi = float(l)
while (hi - lo) > EPS:
x1 = lo + (hi-lo)/3.0
x2 = lo + (hi-lo)*2.0/3.0
if f(a, b, l, x1) > f(a, b, l, x2):
lo = x1
else:
hi = x2
ans = f(a, b, l, lo) / l
if ans < EPS:
print "My poor head =("
else:
print "%.9lf" % ans
if __name__ == "__main__":
main()
| PYTHON |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-8;
using namespace std;
int a, b, l;
double f(double w) {
return (w * a + sqrt(l * l - w * w) * b - w * sqrt(l * l - w * w)) /
(1. * (double)l);
}
int main() {
scanf("%d", &(a)), scanf("%d", &(b)), scanf("%d", &(l));
if (a > b) swap(a, b);
if (l <= a) return !printf("%d\n", (l));
if (l <= b) return !printf("%d\n", (a));
double lo = 0, hi = l;
while (abs(lo - hi) > eps) {
double m1 = (lo * 2 + hi) / 3;
double m2 = (lo + hi * 2) / 3;
if (f(m1) > f(m2))
lo = m1;
else
hi = m2;
}
if (f(lo) < eps)
printf("My poor head =(");
else
printf("%.20f\n", f(lo));
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-9;
double theta;
double a, b, L;
double calcu(double x) {
return (b * L * cos(x) - L * L * cos(x) * sin(x) + a * L * sin(x)) / L;
}
int main() {
scanf("%lf%lf%lf", &a, &b, &L);
double l = 0, r = pi / 2, mid1, mid2;
while (l + eps < r) {
mid1 = (l + r) / 2;
mid2 = (l + mid1) / 2;
if (calcu(mid1) > calcu(mid2))
r = mid1;
else
l = mid2;
}
if (calcu(mid1) < eps)
printf("My poor head =(\n");
else {
double tp = max(a, b), tm;
if (L < tp + eps)
tm = min(a, b);
else
tm = 0;
printf("%.7lf\n", min(L, max(tm, calcu(mid1))));
}
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
/**
* @author Ivan Pryvalov ([email protected])
*/
public class Codeforces78_Div2_ProblemE implements Runnable{
// scanner.nextInt()
// new ByteScanner(scanner.nextLineAsArray()).nextInt()
double A,B,L;
double EPS = 1e-10;
private void solve() throws IOException {
A = scanner.nextInt();
B = scanner.nextInt();
L = scanner.nextInt();
double w = 0;
if (!check(w)){
out.println("My poor head =(");
}else{
double w2 = 1e-8;
while (check(w2)){
w2 *= 2;
}
double w1 = w2/2.0;
w = binarySearch(w1,w2);
//check direct
if (B-L > -EPS){
w = Math.max(w, Math.min(L, A));
}
if (A-L > -EPS){
w = Math.max(w, Math.min(L, B));
}
out.println(w);
}
}
private double binarySearch(double w1, double w2) {
while (Math.abs(w2-w1)>EPS){
double w = (w1+w2)/2.0;
if (check(w)){
w1 = w;
}else{
w2 = w;
}
}
return (w1+w2)/2.0;
}
private boolean check(double w) {
double minL = binarySearchMinL(w, 0, Math.PI/2);
return minL - L > -EPS && L-w > -EPS && A-w > -EPS && B-w > -EPS; //-EPS;
}
private double binarySearchMinL(double w, double l, double r) {
while (Math.abs(l-r)>EPS){
/*
double dx = (r-l)/3.0;
double a1 = l+dx;
double a2 = a1 + dx;
double fA1 = getL(a1,w);
double fA2 = getL(a2,w);
if (fA1 < fA2){
r = a2;
}else{
l = a1;
}*/
double c = (l+r)/2.0;
double dc = 1e-8;
double fC = getL(c,w);
double fC2 = getL(c+dc,w);
if (fC2 > fC){
r = c;
}else{
l = c;
}
}
return getL(l, w);
}
public double getL(double a, double w){
return A/Math.cos(a) + B/Math.sin(a) - 2*w/Math.sin(2*a);
}
/////////////////////////////////////////////////
final int BUF_SIZE = 1024 * 1024 * 8;//important to read long-string tokens properly
final int INPUT_BUFFER_SIZE = 1024 * 1024 * 8 ;
final int BUF_SIZE_INPUT = 1024;
final int BUF_SIZE_OUT = 1024;
boolean inputFromFile = false;
String filenamePrefix = "A-small-attempt0";
String inSuffix = ".in";
String outSuffix = ".out";
//InputStream bis;
//OutputStream bos;
PrintStream out;
ByteScanner scanner;
ByteWriter writer;
@Override
public void run() {
try{
InputStream bis = null;
OutputStream bos = null;
//PrintStream out = null;
if (inputFromFile){
File baseFile = new File(getClass().getResource("/").getFile());
bis = new BufferedInputStream(
new FileInputStream(new File(
baseFile, filenamePrefix+inSuffix)),
INPUT_BUFFER_SIZE);
bos = new BufferedOutputStream(
new FileOutputStream(
new File(baseFile, filenamePrefix+outSuffix)));
out = new PrintStream(bos);
}else{
bis = new BufferedInputStream(System.in, INPUT_BUFFER_SIZE);
bos = new BufferedOutputStream(System.out);
out = new PrintStream(bos);
}
scanner = new ByteScanner(bis, BUF_SIZE_INPUT, BUF_SIZE);
writer = new ByteWriter(bos, BUF_SIZE_OUT);
solve();
out.flush();
}catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public interface Constants{
final static byte ZERO = '0';//48 or 0x30
final static byte NINE = '9';
final static byte SPACEBAR = ' '; //32 or 0x20
final static byte MINUS = '-'; //45 or 0x2d
final static char FLOAT_POINT = '.';
}
public static class EofException extends IOException{
}
public static class ByteWriter implements Constants {
int bufSize = -1;//1024;
byte[] byteBuf = null;//new byte[bufSize];
OutputStream os;
public ByteWriter(OutputStream os, int bufSize){
this.os = os;
this.bufSize = bufSize;
byteBuf = new byte[bufSize];
}
public void writeInt(int num) throws IOException{
int byteWriteOffset = byteBuf.length;
if (num==0){
byteBuf[--byteWriteOffset] = ZERO;
}else{
int numAbs = Math.abs(num);
while (numAbs>0){
byteBuf[--byteWriteOffset] = (byte)((numAbs % 10) + ZERO);
numAbs /= 10;
}
if (num<0) byteBuf[--byteWriteOffset] = MINUS;
}
os.write(byteBuf, byteWriteOffset, byteBuf.length - byteWriteOffset);
}
public void writeLong(long num) throws IOException{
int byteWriteOffset = byteBuf.length;
if (num==0){
byteBuf[--byteWriteOffset] = ZERO;
}else{
long numAbs = Math.abs(num);
while (numAbs>0){
byteBuf[--byteWriteOffset] = (byte)((numAbs % 10) + ZERO);
numAbs /= 10;
}
if (num<0) byteBuf[--byteWriteOffset] = MINUS;
}
os.write(byteBuf, byteWriteOffset, byteBuf.length - byteWriteOffset);
}
/* No need
public void writeDouble(double num) throws IOException{
byte[] data = Double.toString(num).getBytes();
os.write(data,0,data.length);
}*/
//NO NEED: Please ensure ar.length <= byteBuf.length!
/**
*
* @param ar
* @throws IOException
*/
public void writeByteAr(byte[] ar) throws IOException{
/*
for (int i = 0; i < ar.length; i++) {
byteBuf[i] = ar[i];
}
os.write(byteBuf,0,ar.length);*/
os.write(ar,0,ar.length);
}
public void writeSpaceBar() throws IOException{
os.write(SPACEBAR);
}
}
public static class ByteScanner implements Constants{
InputStream is;
public ByteScanner(InputStream is, int bufSizeInput, int bufSize){
this.is = is;
this.bufSizeInput = bufSizeInput;
this.bufSize = bufSize;
byteBufInput = new byte[this.bufSizeInput];
byteBuf = new byte[this.bufSize];
}
public ByteScanner(byte[] data){
byteBufInput = data;
bufSizeInput = data.length;
bufSize = data.length;
byteBuf = new byte[bufSize];
byteRead = data.length;
bytePos = 0;
}
private int bufSizeInput;
private int bufSize;
byte[] byteBufInput;
byte by=-1;
int byteRead=-1;
int bytePos=-1;
static byte[] byteBuf;
int totalBytes;
boolean eofMet = false;
private byte nextByte() throws IOException{
if (bytePos<0 || bytePos>=byteRead){
byteRead = is==null? -1: is.read(byteBufInput);
bytePos=0;
if (byteRead<0){
byteBufInput[bytePos]=-1;//!!!
if (eofMet)
throw new EofException();
eofMet = true;
}
}
return byteBufInput[bytePos++];
}
/**
* Returns next meaningful character as a byte.<br>
*
* @return
* @throws IOException
*/
public byte nextChar() throws IOException{
while ((by=nextByte())<=0x20);
return by;
}
/**
* Returns next meaningful character OR space as a byte.<br>
*
* @return
* @throws IOException
*/
public byte nextCharOrSpacebar() throws IOException{
while ((by=nextByte())<0x20);
return by;
}
/**
* Reads line.
*
* @return
* @throws IOException
*/
public String nextLine() throws IOException {
readToken((byte)0x20);
return new String(byteBuf,0,totalBytes);
}
public byte[] nextLineAsArray() throws IOException {
readToken((byte)0x20);
byte[] out = new byte[totalBytes];
System.arraycopy(byteBuf, 0, out, 0, totalBytes);
return out;
}
/**
* Reads token. Spacebar is separator char.
*
* @return
* @throws IOException
*/
public String nextToken() throws IOException {
readToken((byte)0x21);
return new String(byteBuf,0,totalBytes);
}
/**
* Spacebar is included as separator char
*
* @throws IOException
*/
private void readToken() throws IOException {
readToken((byte)0x21);
}
private void readToken(byte acceptFrom) throws IOException {
totalBytes = 0;
while ((by=nextByte())<acceptFrom);
byteBuf[totalBytes++] = by;
while ((by=nextByte())>=acceptFrom){
byteBuf[totalBytes++] = by;
}
}
public int nextInt() throws IOException{
readToken();
int num=0, i=0;
boolean sign=false;
if (byteBuf[i]==MINUS){
sign = true;
i++;
}
for (; i<totalBytes; i++){
num*=10;
num+=byteBuf[i]-ZERO;
}
return sign?-num:num;
}
public long nextLong() throws IOException{
readToken();
long num=0;
int i=0;
boolean sign=false;
if (byteBuf[i]==MINUS){
sign = true;
i++;
}
for (; i<totalBytes; i++){
num*=10;
num+=byteBuf[i]-ZERO;
}
return sign?-num:num;
}
/*
//TODO test Unix/Windows formats
public void toNextLine() throws IOException{
while ((ch=nextChar())!='\n');
}
*/
public double nextDouble() throws IOException{
readToken();
char[] token = new char[totalBytes];
for (int i = 0; i < totalBytes; i++) {
token[i] = (char)byteBuf[i];
}
return Double.parseDouble(new String(token));
}
}
public static void main(String[] args) {
new Codeforces78_Div2_ProblemE().run();
}
}
| JAVA |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | import java.util.*;
import java.io.*;
public class Main{
BufferedReader in;
StringTokenizer str = null;
PrintWriter out;
private String next() throws Exception{
while (str == null || !str.hasMoreElements())
str = new StringTokenizer(in.readLine());
return str.nextToken();
}
private int nextInt() throws Exception{
return Integer.parseInt(next());
}
final static double EPS = 1e-9;
public void run() throws Exception{
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int a = nextInt(), b = nextInt(), l = nextInt();
if (a > b){
int t = a;
a = b;
b = t;
}
if (l <= a){
out.println(l);
out.close();
return;
}
if (a < l && l <= b){
out.println(a);
out.close();
return;
}
/*
for(double i = 0; i <= l; i+=0.1){
out.println(f(i, l, a, b));
}
*/
double lo = 0, hi = l;
while(hi - lo > EPS){
double m1 = lo + (hi - lo)/3;
double m2 = hi - (hi - lo)/3;
double f1 = f(m1, l, a, b);
double f2 = f(m2, l, a, b);
if (f1 < f2){
hi = m2;
}else{
lo = m1;
}
//out.println(f(lo, l, a, b) + " " + f(hi, l, a, b));
}
double x = (lo + hi) / 2;
double r = f(x, l, a, b);
if (Math.abs(r) < 1e-7){
out.println("My poor head =(");
}else{
out.println(r);
}
out.close();
}
private double f(double x, double l, double a, double b){
double ax = -a, ay = Math.sqrt(l * l - x * x) - b;
double bx = x - a, by = -b;
double area = cross(ax, ay, bx, by);
if (area < 0) area = -area;
return area / l;
}
private double cross(double x1, double y1, double x2, double y2){
return x1 * y2 - x2 * y1;
}
public static void main(String args[]) throws Exception{
new Main().run();
}
} | JAVA |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
/**
* @author Ivan Pryvalov ([email protected])
*/
public class Codeforces78_Div2_ProblemE implements Runnable{
// scanner.nextInt()
// new ByteScanner(scanner.nextLineAsArray()).nextInt()
double A,B,L;
double EPS = 1e-10;
private void solve() throws IOException {
A = scanner.nextInt();
B = scanner.nextInt();
L = scanner.nextInt();
double w = 0;
if (!check(w)){
out.println("My poor head =(");
}else{
double w2 = 1e-8;
while (check(w2)){
w2 *= 2;
}
double w1 = w2/2.0;
w = binarySearch(w1,w2);
//check direct
if (B-L > -EPS){
w = Math.max(w, Math.min(L, A));
}
if (A-L > -EPS){
w = Math.max(w, Math.min(L, B));
}
out.println(w);
}
}
private double binarySearch(double w1, double w2) {
while (Math.abs(w2-w1)>EPS){
double w = (w1+w2)/2.0;
if (check(w)){
w1 = w;
}else{
w2 = w;
}
}
return (w1+w2)/2.0;
}
private boolean check(double w) {
double minL = binarySearchMinL(w, 0, Math.PI/2);
return minL - L > -EPS && L-w > -EPS && A-w > -EPS && B-w > -EPS; //-EPS;
}
private double binarySearchMinL(double w, double l, double r) {
while (Math.abs(l-r)>EPS){
double dx = (r-l)/3.0;
double a1 = l+dx;
double a2 = a1 + dx;
double fA1 = getL(a1,w);
double fA2 = getL(a2,w);
if (fA1 < fA2){
r = a2;
}else{
l = a1;
}
}
return getL(l, w);
}
public double getL(double a, double w){
return A/Math.cos(a) + B/Math.sin(a) - 2*w/Math.sin(2*a);
}
/////////////////////////////////////////////////
final int BUF_SIZE = 1024 * 1024 * 8;//important to read long-string tokens properly
final int INPUT_BUFFER_SIZE = 1024 * 1024 * 8 ;
final int BUF_SIZE_INPUT = 1024;
final int BUF_SIZE_OUT = 1024;
boolean inputFromFile = false;
String filenamePrefix = "A-small-attempt0";
String inSuffix = ".in";
String outSuffix = ".out";
//InputStream bis;
//OutputStream bos;
PrintStream out;
ByteScanner scanner;
ByteWriter writer;
@Override
public void run() {
try{
InputStream bis = null;
OutputStream bos = null;
//PrintStream out = null;
if (inputFromFile){
File baseFile = new File(getClass().getResource("/").getFile());
bis = new BufferedInputStream(
new FileInputStream(new File(
baseFile, filenamePrefix+inSuffix)),
INPUT_BUFFER_SIZE);
bos = new BufferedOutputStream(
new FileOutputStream(
new File(baseFile, filenamePrefix+outSuffix)));
out = new PrintStream(bos);
}else{
bis = new BufferedInputStream(System.in, INPUT_BUFFER_SIZE);
bos = new BufferedOutputStream(System.out);
out = new PrintStream(bos);
}
scanner = new ByteScanner(bis, BUF_SIZE_INPUT, BUF_SIZE);
writer = new ByteWriter(bos, BUF_SIZE_OUT);
solve();
out.flush();
}catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public interface Constants{
final static byte ZERO = '0';//48 or 0x30
final static byte NINE = '9';
final static byte SPACEBAR = ' '; //32 or 0x20
final static byte MINUS = '-'; //45 or 0x2d
final static char FLOAT_POINT = '.';
}
public static class EofException extends IOException{
}
public static class ByteWriter implements Constants {
int bufSize = -1;//1024;
byte[] byteBuf = null;//new byte[bufSize];
OutputStream os;
public ByteWriter(OutputStream os, int bufSize){
this.os = os;
this.bufSize = bufSize;
byteBuf = new byte[bufSize];
}
public void writeInt(int num) throws IOException{
int byteWriteOffset = byteBuf.length;
if (num==0){
byteBuf[--byteWriteOffset] = ZERO;
}else{
int numAbs = Math.abs(num);
while (numAbs>0){
byteBuf[--byteWriteOffset] = (byte)((numAbs % 10) + ZERO);
numAbs /= 10;
}
if (num<0) byteBuf[--byteWriteOffset] = MINUS;
}
os.write(byteBuf, byteWriteOffset, byteBuf.length - byteWriteOffset);
}
public void writeLong(long num) throws IOException{
int byteWriteOffset = byteBuf.length;
if (num==0){
byteBuf[--byteWriteOffset] = ZERO;
}else{
long numAbs = Math.abs(num);
while (numAbs>0){
byteBuf[--byteWriteOffset] = (byte)((numAbs % 10) + ZERO);
numAbs /= 10;
}
if (num<0) byteBuf[--byteWriteOffset] = MINUS;
}
os.write(byteBuf, byteWriteOffset, byteBuf.length - byteWriteOffset);
}
/* No need
public void writeDouble(double num) throws IOException{
byte[] data = Double.toString(num).getBytes();
os.write(data,0,data.length);
}*/
//NO NEED: Please ensure ar.length <= byteBuf.length!
/**
*
* @param ar
* @throws IOException
*/
public void writeByteAr(byte[] ar) throws IOException{
/*
for (int i = 0; i < ar.length; i++) {
byteBuf[i] = ar[i];
}
os.write(byteBuf,0,ar.length);*/
os.write(ar,0,ar.length);
}
public void writeSpaceBar() throws IOException{
os.write(SPACEBAR);
}
}
public static class ByteScanner implements Constants{
InputStream is;
public ByteScanner(InputStream is, int bufSizeInput, int bufSize){
this.is = is;
this.bufSizeInput = bufSizeInput;
this.bufSize = bufSize;
byteBufInput = new byte[this.bufSizeInput];
byteBuf = new byte[this.bufSize];
}
public ByteScanner(byte[] data){
byteBufInput = data;
bufSizeInput = data.length;
bufSize = data.length;
byteBuf = new byte[bufSize];
byteRead = data.length;
bytePos = 0;
}
private int bufSizeInput;
private int bufSize;
byte[] byteBufInput;
byte by=-1;
int byteRead=-1;
int bytePos=-1;
static byte[] byteBuf;
int totalBytes;
boolean eofMet = false;
private byte nextByte() throws IOException{
if (bytePos<0 || bytePos>=byteRead){
byteRead = is==null? -1: is.read(byteBufInput);
bytePos=0;
if (byteRead<0){
byteBufInput[bytePos]=-1;//!!!
if (eofMet)
throw new EofException();
eofMet = true;
}
}
return byteBufInput[bytePos++];
}
/**
* Returns next meaningful character as a byte.<br>
*
* @return
* @throws IOException
*/
public byte nextChar() throws IOException{
while ((by=nextByte())<=0x20);
return by;
}
/**
* Returns next meaningful character OR space as a byte.<br>
*
* @return
* @throws IOException
*/
public byte nextCharOrSpacebar() throws IOException{
while ((by=nextByte())<0x20);
return by;
}
/**
* Reads line.
*
* @return
* @throws IOException
*/
public String nextLine() throws IOException {
readToken((byte)0x20);
return new String(byteBuf,0,totalBytes);
}
public byte[] nextLineAsArray() throws IOException {
readToken((byte)0x20);
byte[] out = new byte[totalBytes];
System.arraycopy(byteBuf, 0, out, 0, totalBytes);
return out;
}
/**
* Reads token. Spacebar is separator char.
*
* @return
* @throws IOException
*/
public String nextToken() throws IOException {
readToken((byte)0x21);
return new String(byteBuf,0,totalBytes);
}
/**
* Spacebar is included as separator char
*
* @throws IOException
*/
private void readToken() throws IOException {
readToken((byte)0x21);
}
private void readToken(byte acceptFrom) throws IOException {
totalBytes = 0;
while ((by=nextByte())<acceptFrom);
byteBuf[totalBytes++] = by;
while ((by=nextByte())>=acceptFrom){
byteBuf[totalBytes++] = by;
}
}
public int nextInt() throws IOException{
readToken();
int num=0, i=0;
boolean sign=false;
if (byteBuf[i]==MINUS){
sign = true;
i++;
}
for (; i<totalBytes; i++){
num*=10;
num+=byteBuf[i]-ZERO;
}
return sign?-num:num;
}
public long nextLong() throws IOException{
readToken();
long num=0;
int i=0;
boolean sign=false;
if (byteBuf[i]==MINUS){
sign = true;
i++;
}
for (; i<totalBytes; i++){
num*=10;
num+=byteBuf[i]-ZERO;
}
return sign?-num:num;
}
/*
//TODO test Unix/Windows formats
public void toNextLine() throws IOException{
while ((ch=nextChar())!='\n');
}
*/
public double nextDouble() throws IOException{
readToken();
char[] token = new char[totalBytes];
for (int i = 0; i < totalBytes; i++) {
token[i] = (char)byteBuf[i];
}
return Double.parseDouble(new String(token));
}
}
public static void main(String[] args) {
new Codeforces78_Div2_ProblemE().run();
}
}
| JAVA |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
double a, b, l;
double eps = 1e-8;
double func(double x) {
return (x * a + sqrt(l * l - x * x) * b - sqrt(l * l - x * x) * x) / l;
}
double tsearch(double l, double r) {
if (r - l < eps) return (l + r) / 2;
double a = (2 * l + r) / 3;
double b = (l + 2 * r) / 3;
if (func(a) < func(b)) return tsearch(l, b);
return tsearch(a, r);
}
void solve() {
cin >> a >> b >> l;
if (l <= b) {
printf("%.9lf", min(a, l));
return;
}
if (l <= a) {
printf("%.9lf", min(b, l));
return;
}
double ans = func(tsearch(0, l));
if (ans < eps)
printf("My poor head =(\n");
else
printf("%.9lf\n", min(ans, l));
}
int main() {
solve();
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
double a, b, l;
double Func(double x) {
double y = sqrt(l * l - x * x);
return fabs(a * y + b * x - x * y) / l;
}
double Solve(double left, double right) {
while (right - left > 1e-10) {
double d = (right - left) / 3;
double k1 = left + d, k2 = right - d;
double f1 = Func(k1), f2 = Func(k2);
if (f1 > f2)
left = k1;
else
right = k2;
}
return Func(left);
}
int main() {
while (scanf("%lf%lf%lf", &a, &b, &l) != EOF) {
if (a > b) swap(a, b);
if (l <= a)
printf("%.8lf\n", l);
else if (l <= b)
printf("%.8lf\n", a);
else {
double ret = Solve(0, l);
if (ret < 1e-7)
printf("My poor head =(\n");
else
printf("%.8lf\n", ret);
}
}
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
void chmin(T &a, T b) {
a = a <= b ? a : b;
}
template <class T>
void chmax(T &a, T b) {
a = a >= b ? a : b;
}
double calc(double a, double b, double l, double theta) {
return b * cos(theta) + a * sin(theta) - l * cos(theta) * sin(theta);
}
void solve() {
double a, b, l;
cin >> a >> b >> l;
double ans = 0;
if (l <= a)
ans = b;
else if (l <= b)
ans = a;
double r1 = 0, r2 = acos(0);
for (int i = 0; i < 1000; ++i) {
double r11 = 2.0 / 3 * r1 + 1.0 / 3 * r2;
double r22 = 1.0 / 3 * r1 + 2.0 / 3 * r2;
if (calc(a, b, l, r11) > calc(a, b, l, r22))
r1 = r11;
else
r2 = r22;
}
chmax(ans, calc(a, b, l, r1));
chmin(ans, l);
if (fabs(ans) <= 1.0e-7)
cout << "My poor head =(" << endl;
else
cout << fixed << setprecision(10) << ans << endl;
}
int main() { solve(); }
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
double n, m, t, x, y, k, a, b, lng, w, l, r, alpha;
double f(double p) {
return (p * a + sqrt(lng * lng - p * p) * b - p * sqrt(lng * lng - p * p)) /
lng;
}
int main() {
cout << fixed << setprecision(12);
cin >> a >> b >> lng;
double EPS = 0.0000000001;
double l = 0.0, r = lng;
if (a > b) swap(a, b);
if (lng <= a) {
cout << lng << endl;
return 0;
}
if (lng <= b) {
cout << a << endl;
return 0;
}
double m1 = l + (r - l) / 3, m2 = r - (r - l) / 3;
for (int _ = 300; _--;) {
m1 = (l * 2 + r) / 3, m2 = (l + r * 2) / 3;
if (f(m1) > f(m2))
l = m1;
else
r = m2;
}
if (f(l) < 1e-8) {
cout << "My poor head =(" << endl;
} else
cout << f(l) << endl;
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int a, b, len;
double m1, m2, l, r;
double f(double x) {
return a * x + b * sqrt(len * len - x * x) - x * sqrt(len * len - x * x);
}
int main() {
scanf("%d%d%d", &a, &b, &len);
if (a > b) swap(a, b);
if (len <= a) {
printf("%d\n", len);
return 0;
}
if (len <= b) {
printf("%d\n", a);
return 0;
}
r = len;
while (l + 1e-10 < r) {
m1 = (l * 2 + r) / 3, m2 = (l + r * 2) / 3;
if (f(m1) > f(m2))
l = m1;
else
r = m2;
}
if (f(m1) < 1e-9)
puts("My poor head =(");
else
printf("%.9lf\n", f(m1) / len);
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long double eps = 1 / 1e7;
struct point {
long double x, y;
};
long double a, b, l;
point p;
long double ptl(long double k) {
point p1, p2;
p1.x = 0;
p1.y = sqrt(l * l - k * k);
p2.x = k;
p2.y = 0;
long double A, B, C;
A = p1.y - p2.y;
B = p2.x - p1.x;
C = -A * p1.x - B * p1.y;
return ((A * p.x + B * p.y + C)) / sqrt(A * A + B * B);
}
long double trin() {
long double ll = 0, rr = l, k1, k2, d1, d2;
while (rr - ll > eps) {
k1 = ll * 2 / 3 + rr / 3;
k2 = ll / 3 + rr * 2 / 3;
d1 = ptl(k1);
d2 = ptl(k2);
if (d1 > d2)
ll = k1;
else
rr = k2;
}
return ptl(rr);
}
int main() {
long double w, k;
cin >> a >> b >> l;
p.x = a;
p.y = b;
if (a >= l) {
cout << min(l, b);
return 0;
}
if (b >= l) {
cout << min(l, a);
return 0;
}
w = trin();
if (w < eps) {
cout << "My poor head =(";
return 0;
}
cout.precision(15);
cout << w;
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
double getWidth(double x, double a, double b, double l) {
return b * cos(x) - l * sin(x) * cos(x) + a * sin(x);
;
}
double getLength(double x, double a, double b, double l) {
return (a / sin(x)) + (b / cos(x));
}
double findMin(double a, double b, double l,
double (*get)(double, double, double, double)) {
double x1 = 0, x2 = acos(-1.0) / 2, mid1, mid2;
while (x1 + 1e-7 < x2) {
mid1 = x1 * 2 / 3 + x2 * 1 / 3;
mid2 = x1 * 1 / 3 + x2 * 2 / 3;
double ans1 = get(mid1, a, b, l);
double ans2 = get(mid2, a, b, l);
if (ans2 > ans1) {
x2 = mid2;
} else {
x1 = mid1;
}
}
return x1;
}
double width(double a, double b, double l, int &flag) {
if (l <= b) {
if (l < a)
return l;
else
return a;
}
if (l <= a) return b;
double x, templ;
x = findMin(a, b, l, getLength);
templ = getLength(x, a, b, l);
if (l > templ) {
flag = 0;
return 0;
}
x = findMin(a, b, l, getWidth);
return getWidth(x, a, b, l);
}
int main() {
double a, b, l, w;
int flag;
scanf("%lf %lf %lf", &a, &b, &l);
flag = 1;
w = width(a, b, l, flag);
if (flag == 0)
printf("My poor head =(\n");
else
printf("%.7lf\n", w);
getchar(), getchar();
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom — Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg — he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l ≥ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 ≤ a, b, l ≤ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember — coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember — arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
double getWidth(double x, double a, double b, double l) {
return b * cos(x) - l * sin(x) * cos(x) + a * sin(x);
;
}
double getLength(double x, double a, double b, double l) {
return (a / sin(x)) + (b / cos(x));
}
double findMin(double a, double b, double l,
double (*get)(double, double, double, double)) {
double x1 = 0, x2 = acos(-1.0) / 2, mid1 = 0, mid2 = acos(-1.0) / 2;
do {
double ans1 = get(mid1, a, b, l);
double ans2 = get(mid2, a, b, l);
if (ans2 > ans1) {
x2 = mid2;
} else {
x1 = mid1;
}
mid1 = x1 * 2 / 3 + x2 * 1 / 3;
mid2 = x1 * 1 / 3 + x2 * 2 / 3;
} while (x1 + 1e-7 < x2);
return x1;
}
double width(double a, double b, double l, int &flag) {
if (l <= b) {
if (l < a)
return l;
else
return a;
}
if (l <= a) return b;
double x, templ;
x = findMin(a, b, l, getLength);
templ = getLength(x, a, b, l);
if (l > templ) {
flag = 0;
return 0;
}
x = findMin(a, b, l, getWidth);
return getWidth(x, a, b, l);
}
int main() {
double a, b, l, w;
int flag;
scanf("%lf %lf %lf", &a, &b, &l);
flag = 1;
w = width(a, b, l, flag);
if (flag == 0)
printf("My poor head =(\n");
else
printf("%.7lf\n", w);
getchar(), getchar();
return 0;
}
| CPP |
benny-and-two-strings-2 | Benny is given two strings S and T. The length of the string S is N and the length of the string T is M respectively.
Also, imagine that alphabets are cyclic in nature: a-b-c-…-x-y-z-a-b.. and so on. The distance between two letters is the minimal distance between them in the alphabet cycle. For example, distance between ‘a’ and ‘c’ is 2, when distance between ‘b’ and ‘z’ is 2.
Let’s call the process transformation when one letter turns to another one. The cost of the transformation is the distance between these letters.
She may perform some transformations in string T, but the total cost of these transformations should not exceed K.
Now, she wants to make a string T’ from the given string T such that S appears the most times in T’ as a substring.
For example, abc is a substring of ababc but bac is not a substring of ababc.
Output the maximal numbers of occurrences of string S in T’ after making transformations with total cost not more that K.
Input format
The first line contains three space separated integers N, M and K.
The next two lines contain strings S and and T respectively.
Output format
Print in a single line an answer to the problem.
Constraints
1 ≤ N, M ≤ 200
0 ≤ K ≤ 500
Note
Strings contain only lowercase English letters.
SAMPLE INPUT
3 5 3
aba
zbzbz
SAMPLE OUTPUT
2
Explanation
In the given sample case, she can make ababa from zbzbz. The cost of transformation is not more that K. The number of occurrences of aba in ababa are 2. Hence, the answer is 2. | 3 | 0 | #!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys, math, random, operator
from string import ascii_lowercase
from string import ascii_uppercase
from fractions import Fraction, gcd
from decimal import Decimal, getcontext
from itertools import product, permutations, combinations
from Queue import Queue, PriorityQueue
from collections import deque, defaultdict, Counter
getcontext().prec = 100
MOD = 10**9 + 7
INF = float("+inf")
if sys.subversion[0] != "CPython": # PyPy?
raw_input = lambda: sys.stdin.readline().rstrip()
pr = lambda *args: sys.stdout.write(" ".join(str(x) for x in args) + "\n")
epr = lambda *args: sys.stderr.write(" ".join(str(x) for x in args) + "\n")
die = lambda *args: pr(*args) ^ exit(0)
read_str = raw_input
read_strs = lambda: raw_input().split()
read_int = lambda: int(raw_input())
read_ints = lambda: map(int, raw_input().split())
read_float = lambda: float(raw_input())
read_floats = lambda: map(float, raw_input().split())
"---------------------------------------------------------------"
N, M, K = read_ints()
s = read_str()
t = read_str()
for off in xrange(1, len(s) + 1):
a = s[off:]
if a == s[:len(a)]:
break
else:
assert 0
alphanum = {c:ascii_lowercase.index(c) for c in ascii_lowercase}
dist = {
(a, b): min(abs(ca - cb), abs(ca + 26 - cb), abs(cb + 26 - ca))
for a, ca in alphanum.items()
for b, cb in alphanum.items()
}
suflen = off
suf = s[-off:]
def calc_cost(i, w):
if i + len(w) > len(t):
return None
cost = 0
for x, y in zip(w, t[i:]):
cost += dist[x, y]
return cost
cost_full = [calc_cost(i, s) for i in xrange(len(t) + 1)]
cost_suf = [calc_cost(i, suf) for i in xrange(len(t) + 1)]
# pr(dist['c', 'b'])
# pr(dist['b', 'c'])
#pr(cost_full)
#pr(cost_suf)
dp = [{} for i in xrange(len(t) + 1)]
dp[len(t)][0] = 0
# dp[pos][num_occur] = cost
ans = 0
for i in reversed(range(len(t))):
dpcur = dp[i] = dp[i+1].copy()
pos = i + len(s)
num1 = 1
cost1 = cost_full[i]
while pos <= len(t):
for num2, cost2 in dp[pos].items():
num = num1 + num2
cost = cost1 + cost2
if cost > K:
continue
ans = max(ans, num)
if num not in dpcur:
dpcur[num] = cost
else:
dpcur[num] = min(dpcur[num], cost)
pos += suflen
if pos > len(t):
break
num1 += 1
cost1 += cost_suf[pos - suflen]
print ans | PYTHON |
benny-and-two-strings-2 | Benny is given two strings S and T. The length of the string S is N and the length of the string T is M respectively.
Also, imagine that alphabets are cyclic in nature: a-b-c-…-x-y-z-a-b.. and so on. The distance between two letters is the minimal distance between them in the alphabet cycle. For example, distance between ‘a’ and ‘c’ is 2, when distance between ‘b’ and ‘z’ is 2.
Let’s call the process transformation when one letter turns to another one. The cost of the transformation is the distance between these letters.
She may perform some transformations in string T, but the total cost of these transformations should not exceed K.
Now, she wants to make a string T’ from the given string T such that S appears the most times in T’ as a substring.
For example, abc is a substring of ababc but bac is not a substring of ababc.
Output the maximal numbers of occurrences of string S in T’ after making transformations with total cost not more that K.
Input format
The first line contains three space separated integers N, M and K.
The next two lines contain strings S and and T respectively.
Output format
Print in a single line an answer to the problem.
Constraints
1 ≤ N, M ≤ 200
0 ≤ K ≤ 500
Note
Strings contain only lowercase English letters.
SAMPLE INPUT
3 5 3
aba
zbzbz
SAMPLE OUTPUT
2
Explanation
In the given sample case, she can make ababa from zbzbz. The cost of transformation is not more that K. The number of occurrences of aba in ababa are 2. Hence, the answer is 2. | 3 | 0 | N,M,K=map(int, raw_input().split())
S=raw_input()
T=raw_input()
lenS=len(S)
shifts=set([])
for i in xrange(1,lenS):
if S[:-i]==S[i:]:
shifts.add(i)
Sn=[ord(S[i])-ord('a') for i in xrange(lenS)]
Tn=[ord(T[i])-ord('a') for i in xrange(len(T))]
arrEnds=[{} for _ in xrange(lenS-1)]
arrBest=[{} for _ in xrange(lenS-1)]
cost=sum(min(abs(Sn[i]-Tn[i]),Sn[i]+26-Tn[i],Tn[i]+26-Sn[i]) for i in xrange(lenS))
if cost<=K:
arrEnds.append({1:cost})
arrBest.append({1:cost})
for i in xrange(lenS,len(T)):
ends={}
cost=0
for j in xrange(1,lenS+1): # shift range
Snj=Sn[-j]
Tnij=Tn[i-j+1]
cost+=min(abs(Snj-Tnij),Snj+26-Tnij,Tnij+26-Snj)
if j in shifts:
# combine cases when ended there and adding this cost
prev=arrEnds[i-j]
for num in prev:
if prev[num]+cost<=K:
if not num+1 in ends:
ends[num+1]=prev[num]+cost
elif ends[num+1]>prev[num]+cost:
ends[num+1]=prev[num]+cost
# combine cases when ended here without
if cost<=K:
ends[1]=cost # no 0 in ends (neither the previously calculated nor arrEnds)
prev=arrBest[i-lenS]
for num in prev:
if prev[num]+cost<=K:
if not num+1 in ends:
ends[num+1]=prev[num]+cost
elif ends[num+1]>prev[num]+cost:
ends[num+1]=prev[num]+cost
arrEnds.append(ends)
best=arrBest[-1].copy()
for num in ends:
best[num]=min(best[num],ends[num]) if num in best else ends[num]
arrBest.append(best)
print max(arrBest[-1].keys()) if arrBest[-1] else "0" | PYTHON |
comrades-ii-6 | It’s the year 2552, the Humans just won a war against a very powerful alien race that had invaded our solar system. The Human army is in celebration mode!
The army has n soldiers. The soldiers are numbers from 1 to n. The army has a superiority hierarchy. Every soldier has one immediate superior. The superior of a superior of a soldier is also a superior to that soldier. So, a soldier may have one or more superiors but only one immediate superior.
Every soldier has to congratulate every other soldier. For a pair of soldiers if one of them is the superior of the other, they will shake hands. Otherwise, they will bump their fists.
You are given the list of immediate superior of all soldiers. Your job is to tell how many handshakes and fist bumps will be there.
NOTE: Among all soldiers there is one soldier who does not have any superior. He is the commander of the whole army.
Input:
The first line of the input contains t, the number of test cases.
The first line of each test case contains n, the number of soldiers. The next line contains n space separated integers. The ith integer represents the immediate superior of the ith soldier.
The immediate superior of the commander of the army will be '0'.
Output:
Output two space separated integers, the number of handshakes and the number of fist bumps.
Constraints:
1 ≤ t ≤ 10
1 ≤ n ≤ 100000
It is guaranteed that the commander of the army is a superior of all soldiers.
SAMPLE INPUT
2
3
0 1 1
2
2 0
SAMPLE OUTPUT
2 1
1 0
Explanation
1st test case:
Handshakes between (1,2), (1,3).
Fist bumps between (2,3)
2nd test case:
Handshake between (1,2)
No fist bumps | 3 | 0 | from collections import deque
def calculate(tree):
queue = deque(tree[0])
handshakes = fistpumps = nodes = level = 0
while queue:
for _ in xrange(len(queue)):
root = queue.popleft()
handshakes += level
fistpumps += nodes - level
nodes += 1
queue.extend(tree[root])
level += 1
return handshakes, fistpumps
t = input()
for _ in xrange(t):
n = input()
superiors = [int(v) for v in raw_input().split()]
tree = {i: [] for i in xrange(n+1)}
[tree[s].append(i+1) for i, s in enumerate(superiors)]
handshakes, fistpumps = calculate(tree)
print handshakes, fistpumps | PYTHON |
find-handi-bhaiya | ACT I
Handi Bhaiya is a very notorious kid and always troubles his parents and friends. One day his naughtiness lead him to a demon Kali(THE COBRA COMMANDER) who trapped him into a triangular magic cell. Handi Bhaiya parents want their kid back and asks for your help.
ACT II
You knew that the entire humanity will hate you but then also you decided to rescue Handi Bhaiya (don't ask why). You went to Kali and asked to release Handi Bhiaya. Kali being good by heart decided to free him but you have to solve a puzzle first.
Puzzle
Kali asks to write you a program that will tell whether Handi Bhaiya can be inside the given cell or not, for T(1 ≤ T ≤ 10^6) test cases. For each test case you will be given a cell(not necessarily closed and bounded) that will have 3 integral sides- a, b and c representing the sides of cell. You have to tell whether Handi can be inside that cell or not. Remember that the cell was triangular in which he was trapped.
If the cell is triangular then also print the type of triangle - isosceles, equilateral or scalene. Refer given test case for sample output.
Constraints: 0<a,b,c<2^64
SAMPLE INPUT
4
15 15 15
13 13 14
3 4 5
10 12 23
SAMPLE OUTPUT
YES. It is a equilateral triangle.
YES. It is a isosceles triangle.
YES. It is a scalene triangle.
NO | 3 | 0 | t = int(raw_input())
for _ in range(t):
l = []
l = map(int, raw_input().split())
if l[0]+l[1]>l[2] and l[1]+l[2]>l[0] and l[2]+l[0]>l[1]:
if l[0]==l[1] and l[1]==l[2]:
print 'YES. It is a equilateral triangle.'
elif l[0]==l[1] or l[1]==l[2] or l[2]==l[0]:
print 'YES. It is a isosceles triangle.'
else:
print 'YES. It is a scalene triangle.'
else:
print 'NO' | PYTHON |
find-handi-bhaiya | ACT I
Handi Bhaiya is a very notorious kid and always troubles his parents and friends. One day his naughtiness lead him to a demon Kali(THE COBRA COMMANDER) who trapped him into a triangular magic cell. Handi Bhaiya parents want their kid back and asks for your help.
ACT II
You knew that the entire humanity will hate you but then also you decided to rescue Handi Bhaiya (don't ask why). You went to Kali and asked to release Handi Bhiaya. Kali being good by heart decided to free him but you have to solve a puzzle first.
Puzzle
Kali asks to write you a program that will tell whether Handi Bhaiya can be inside the given cell or not, for T(1 ≤ T ≤ 10^6) test cases. For each test case you will be given a cell(not necessarily closed and bounded) that will have 3 integral sides- a, b and c representing the sides of cell. You have to tell whether Handi can be inside that cell or not. Remember that the cell was triangular in which he was trapped.
If the cell is triangular then also print the type of triangle - isosceles, equilateral or scalene. Refer given test case for sample output.
Constraints: 0<a,b,c<2^64
SAMPLE INPUT
4
15 15 15
13 13 14
3 4 5
10 12 23
SAMPLE OUTPUT
YES. It is a equilateral triangle.
YES. It is a isosceles triangle.
YES. It is a scalene triangle.
NO | 3 | 0 | from sys import stdin
t = int(stdin.readline())
for _ in xrange(t):
a = map(int,stdin.readline().split())
a.sort()
s = set(a)
ans = 'NO'
if a[0] + a[1] > a[2]:
if len(s)==1:
ans = 'YES. It is a equilateral triangle.'
elif len(s)==2:
ans = 'YES. It is a isosceles triangle.'
else:
ans = 'YES. It is a scalene triangle.'
print ans | PYTHON |
imagination-game-easy-contest | Two players are playing with N-dimensional rectangular with sizes of its sides (a1, a2, ... aN). (Let's call this rectangular as "cube"). Initially there is one cell marked with coordinates (x1, x2, ... xN).
On his/her turn a player should divide a current cube into two cubes by exactly one cut along one of the valid integer coordinates in exactly one of the dimensions. Let's consider a 2-dimensional example 2x3 with marked cell (1, 1). After one turn we can get the following pairs of cubes:
1x3 with the marked cell and 1x3 without the marked cell (cut in the dimension 1 along coordinate 1)
1x2 with the marked cell and 2x2 without the marked cell (cut in the dimension 2 along coordinate 1)
2x2 with the marked cell and 1x2 without the marked cell (cut in the dimension 2 along coordinate 2)
Then the cube without the marked cell is thrown out. The game continues with another player's turn with the new smaller cube with the marked cell.
If a player can not do any turn he/she loses (it means on his/her turn he/she gets 1x1x..x1 cube which is a marked cell itself).
Given sizes of the cube and initial coordinates of the marked cell can you determine the winner if we assume that both players play optimally?
Input
The first line contains T - the number of test cases. The following lines describe tests.
Each test case consists of three lines. The first line contains one number N.
The second line contains N integers a[i] denoting sizes of the corresponding dimensions.
The third line contains N integers x[i] denoting coordinates of the marked cell.
Output
For each test case output one line containing First or Second depending on the winner in this test case.
Constraints
T ≤ 1000
1 ≤ N ≤ 10
1 ≤ a[i]
product a[1]xa[2]x...xa[N] ≤ 10^6
0 < x[i] ≤ a[i]
SAMPLE INPUT
2
2
2 2
1 1
2
2 3
1 2
SAMPLE OUTPUT
Second
First | 3 | 0 | #!/usr/bin/env python
from sys import stdin
def main():
TC = int(stdin.readline().strip())
for tc in xrange(TC):
N = int(stdin.readline().strip())
A = map(int, stdin.readline().split())
X = map(int, stdin.readline().split())
nims = [0] * (2*N)
for i in xrange(N):
nims[i*2 ] = X[i] - 1
nims[i*2+1] = A[i] - X[i]
xor = reduce(lambda a, b: a ^ b, nims)
print 'First' if xor != 0 else 'Second'
return 0
if __name__ == '__main__': main() | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | '''
# Read input from stdin and provide input before running code
name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
if __name__ == "__main__":
line1 = raw_input()
vals = [int(x) for x in line1.split()]
times = int(raw_input())
marut_points = 0
devil_points = 0
for i in range(0,times):
marut = raw_input()
marut_mod = marut
devil = raw_input()
devil_mod = devil
intr = set(marut).intersection(set(devil))
for i in intr:
difr = marut.count(i) - devil.count(i)
if difr < 0:
marut_mod = marut.replace(i,'')
devil_mod = devil.replace(i,'')
devil_mod = devil_mod + (0 - difr)*i
else:
devil_mod = devil.replace(i,'')
marut_mod = marut.replace(i,'')
marut_mod = marut_mod + (difr)*i
for i in marut_mod:
marut_points = marut_points + vals[ord(i) - 97]
for i in devil_mod:
devil_points = devil_points + vals[ord(i) - 97]
if devil_points > marut_points:
print "Devil"
elif marut_points > devil_points:
print "Marut"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | from collections import Counter
import string
k=raw_input()
k=k.split()
for i in k:
i=int(i)
l={}
for x, y in zip(k, string.ascii_lowercase):
l[y]=x
count = int(raw_input())
s1=0
s2=0
for i in range(count):
k1=raw_input()
k2=raw_input()
#print k1
kl1=set(k1)
kl2=set(k2)
kl3=list(kl1.intersection(kl2))
kp1=dict(Counter(k1))
kp2=dict(Counter(k2))
#print kl3
for i in kl3:
kp1[i]=kp1[i]-1
for i in kl3:
kp2[i]=kp2[i]-1
for i in kp1:
s1=s1+kp1[i]*int(l[i])
for i in kp2:
s2=s2+kp2[i]*int(l[i])
if s1>s2:
print "Marut"
elif s1==s2:
print "Draw"
else:
print "Devil" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | numstr = raw_input()
Q=raw_input()
mcount=0
dcount=0
numarr=numstr.split()
numhash={}
for i in xrange(26):
numhash[i]=int(numarr[i])
for j in xrange(int(Q)):
M=raw_input()
D=raw_input()
mhash={}
dhash={}
for i in xrange(26):
mhash[i]=0
dhash[i]=0
for s in M:
mhash[ord(s)-97]=mhash.get(ord(s)-97)+1
for t in D:
dhash[ord(t)-97]=dhash.get(ord(t)-97)+1
for i in xrange(26):
if mhash[i]>0 and dhash[i]>0:
mhash[i]=mhash.get(i)-1
dhash[i]=dhash.get(i)-1
for k in xrange(26):
mcount+=mhash.get(k)*numhash.get(k)
dcount+=dhash.get(k)*numhash.get(k)
if mcount > dcount:
print 'Marut'
elif dcount>mcount:
print 'Devil'
else:
print 'Draw' | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | str_arr = raw_input().split(' ')
arr = [int(num) for num in str_arr]
x = raw_input()
i = 0
score=0
score1=0
while(i<x):
i=i+1
map1 = [0]*26
map2 = [0]*26
try:
str1=raw_input()
str2=raw_input()
for i in str1:
j=ord(i)-ord('a')
map1[j]=map1[j]+1
for i in str2:
j=ord(i)-ord('a')
if map1[j]>0:
map1[j]=map1[j]-1
else:
map2[j]=map2[j]+1
for i in range(0,26):
score=score+arr[i]*map1[i]
for i in range(0,26):
score1=score1+arr[i]*map2[i]
except EOFError:
break
if score>score1:
print "Marut"
else:
if score==score1:
print "Draw"
else:
print "Devil" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | import sys
num_array=[0]*26
#input_length = sys.stdin.readline().rstrip()
#print(input_length);
t=0;
l=0
k=sys.stdin.readline()
v=k.split(' ');
#print(v);
for i in range(0,26):
num_array[i]=int(v[i]);
#print(num_array);
p=sys.stdin.readline().rstrip()
#print(p);
for i in range(0,int(p)):
c1=sys.stdin.readline().rstrip()
c2=sys.stdin.readline().rstrip()
d=[0]*26;
e=[0]*26;
#print(c1[0]);
#print(d);
for j in c1:
#print(ord(j));
m=ord(j)-97
d[m]=d[m]+1;
for j in c2:
#print(ord(j));
m=ord(j)-97
e[m]=e[m]+1;
#print(e);
#print(d);
for i in range(0,26):
if e[i]>=d[i]:
e[i]=e[i]-d[i];
d[i]=0;
else:
d[i]=d[i]-e[i];
e[i]=0;
#print(e);
#print(d);
for p in range(0,26):
t=t+num_array[p]*e[p];
l=l+num_array[p]*d[p];
#print(l)
#print(t)
if l>t:
print("Marut");
elif t>l:
print("Devil");
else:
print("Draw"); | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | a = raw_input()
b = a.split(' ')
alpha_list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
points = dict(zip(alpha_list, b))
rounds = input()
marut_points = 0
devil_points = 0
for i in range(rounds):
marut = ""
devil = ""
marut = raw_input()
devil = raw_input()
devil_newlist = [i for i in devil ]
try :
[devil.pop(devil.index(i)) for i in marut]
[ marut.pop(marut.index(i)) for i in devil]
except Exception as e:
pass
# check for the points
for i in marut:
marut_points += int(points[i])
for i in devil:
devil_points += int(points[i])
if marut_points > devil_points:
print "Marut"
elif marut_points < devil_points:
print "Devil"
elif marut_points == devil_points:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | lists=[_ for _ in raw_input().split()]
init=97
dicit={}
for key_add in range(26):
key=init+key_add
dicit.update({chr(key):lists[key_add]})
T=int(raw_input())
tot_m=0
tot_d=0
for _ in range(0,T):
lists=list(raw_input())
sum_m=0
for i in lists:
sum_m=sum_m+int(dicit[i])
lists=list(raw_input())
sum_d=0
for i in lists:
sum_d=sum_d+int(dicit[i])
tot_d=tot_d+sum_d
tot_m=tot_m+sum_m
if(tot_m==tot_d):
print "Draw"
else:
if(tot_m>tot_d):
print "Marut"
else:
print "Devil" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | '''
# Read input from stdin and provide input before running code
name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
def main():
import sys
f = sys.stdin
#f = open("Marut_V_Devil_Hunger_Games.txt")
score_M, score_D = 0, 0
char_val = map(int, f.readline().strip().split())
char_count_M = [0 for dummy_i in xrange(26)]
char_count_D = [0 for dummy_i in xrange(26)]
Q = int(f.readline().strip())
for i in xrange(Q):
M = f.readline().strip()
D = f.readline().strip()
# increment the character count for Marut
for j in xrange(len(M)):
char_count_M[ord(M[j])-97] += 1
# increment the character count for Devil
for j in xrange(len(D)):
char_count_D[ord(D[j])-97] += 1
score_M = reduce(lambda x, y: x + y, map(lambda x, y: x * y , char_val, char_count_M))
score_D = reduce(lambda x, y: x + y, map(lambda x, y: x * y , char_val, char_count_D))
if score_D > score_M:
print "Devil"
elif score_M > score_D:
print "Marut"
else:
print "Draw"
main() | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | Arr = map(int, raw_input().split(' '))
t = int(raw_input())
s1 = 0
s2 = 0
for i in range(t):
i1 = raw_input()
i2 = raw_input()
l = len(i1)
j = 0
k = 0
i1 = sorted(i1)
i2 = sorted(i2)
while j < l:
if i2[j] != i1[j]:
s1 = s1+Arr[ord(i1[j])-ord('a')]
s2 = s2++Arr[ord(i2[j])-ord('a')]
j = j+1
if s1 > s2:
print "Marut"
elif s1 < s2:
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | x = raw_input()
nums = [int(n) for n in x.split()]
N = raw_input()
N_int = int(N)
def calculate(A, B):
n = 0
for i in range(97,123):
n += nums[i-97]*((A.count(chr(i)) - B.count(chr(i))))
return n
res = 0
for i in range(1,N_int+1):
A = raw_input()
B = raw_input()
res += calculate(A,B)
if(res > 0):
print "Marut"
elif (res <0):
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | '''
# Read input from stdin and provide input before running code
name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
val = raw_input('\n')
val = [int(i) for i in val.split(' ')]
game_no = input('\n')
m=0
d=0
for i in range(game_no):
ms = raw_input('\n')
ds = raw_input('\n')
ms_ar = ms.split()
for j in ms_ar:
try:
if ds.index(j)!=-1:
ds.replace(j,'',1)
ms.replace(j,'',1)
except:
pass
m += reduce(lambda x,y: x+y, [val[ord(k)-97] for k in ms])
d += reduce(lambda x,y: x+y, [val[ord(k)-97] for k in ds])
if m>d:
print 'Marut'
elif m<d:
print 'Devil'
else:
print 'Draw' | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | a=raw_input()
a=a.split()
a=map(int,a)
sum1=0
sum2=0
b=int(raw_input())
for i in range(0,b):
c=raw_input()
for letter in c:
sum1=sum1+a[ord(letter)-97]
d=raw_input()
for letter in d:
sum2=sum2+a[ord(letter)-97]
if(sum1>sum2):
print "Marut"
elif(sum2>sum1):
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | def find_score(string,mapped_list):
score = 0
for character in string:
score += int(mapped_list[ord(character)%97])
return score
input_values = raw_input(" ")
mapped_list = input_values.split()
times = raw_input(" ")
M_score = 0
D_score = 0
for i in range(0, int(times)):
M = raw_input("")
D = raw_input("")
M_score +=find_score(M,mapped_list)
D_score +=find_score(D,mapped_list)
if M_score > D_score:
print "Marut"
elif D_score > M_score:
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | alphabet = {}
line = raw_input()
a = line.split()
for i in range(26):
alphabet[chr(ord('a')+i)] = int(a[i])
num = raw_input()
point1 =0
point2 = 0
for i in range(int(num)):
str1 = raw_input()
str2 = raw_input()
for (a,b) in zip(str1,str2):
if(a != b):
point1 += alphabet[a]
point2 += alphabet[b]
if(point1>point2):
print "Marut"
elif(point2>point1):
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | def calculate_points(alpha, s):
score = 0
for x in s:
score += alpha[ord(x)-ord('a')]
return score
def main():
alpha = map(int, raw_input().split())
marut, devil = 0, 0
for g in range(input()):
marut += calculate_points(alpha, raw_input())
devil += calculate_points(alpha, raw_input())
# print marut, devil
if marut > devil:
print 'Marut'
elif marut < devil:
print 'Devil'
else:
print 'Draw'
main() | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | def getPointforRound(s1,score):
point =0
for c in s1:
point = point + score[ord(c)-ord('a')]
return point
s = raw_input()
score = map(int, s.split())
rounds = int(raw_input())
point1 = 0
point2 = 0
for round in range(0,rounds):
s1 = raw_input()
s2 = raw_input()
point1 = point1 + getPointforRound(s1,score)
point2 = point2 + getPointforRound(s2,score)
if(point1>point2):
print "Marut"
elif(point2>point1):
print "Devil"
else :
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | charValues = None
#for line in sys.stdin:
#charValues = line.strip().split(" ")
charValues = raw_input().strip().split(" ")
intValues = list()
for val in charValues:
intValues.append(int(val))
numGames = int(raw_input().strip());
mScore = 0
dScore = 0
for i in range(numGames):
m = list(raw_input().strip())
d = list(raw_input().strip())
for j in m:
mScore += intValues[ord(j) - ord('a')]
for j in d:
dScore += intValues[ord(j) - ord('a')]
if mScore > dScore:
print "Marut"
elif mScore < dScore:
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | a = map(int, raw_input().split())
b=input()
f=0
g=0
while b:
c=raw_input()
d=raw_input()
for i in c:
f=f+a[ord(i)-ord('a')]
for i in d:
g=g+a[ord(i)-ord('a')]
b=b-1
if(f==g):
print "Draw"
elif f>g:
print "Marut"
else :
print "Devil" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | Arr = map(int, raw_input().split(' '))
t = int(raw_input())
s1 = 0
s2 = 0
for i in range(t):
i1 = raw_input()
i2 = raw_input()
l = len(i1)
j = 0
k = 0
i1 = sorted(i1)
i2 = sorted(i2)
while j < l:
if i2[j] != i1[j]:
s1 = s1+Arr[ord(i1[j])-ord('a')]
s2 = s2++Arr[ord(i2[j])-ord('a')]
j = j+1
if s1 > s2:
print "Marut"
elif s1 < s2:
print "Devil"
else:
print "Draw"
'''
while j < l and k < l:
if i1[j] == i2[j]:
j = j+1
k = k+1
else:
if i1[j] < i2[j]:
while j < l and i1[j] < i2[j]:
j = j+1
else:
while k < l and i1[k] > i2[k]:
k = k+1
''' | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | h = map(int, raw_input().split())
n = int(raw_input())
marut = 0
devil = 0
for i in xrange(n):
m = raw_input()
d = raw_input()
mchars = [0] * 26
for c in m:
mchars[ord(c) - 97] += 1
dchars = [0] * 26
for c in d:
dchars[ord(c) - 97] += 1
for j in range(0, 26):
v = min(mchars[j], dchars[j])
mchars[j] -= v
dchars[j] -= v
for j in xrange(26):
marut += h[j] * mchars[j]
devil += h[j] * dchars[j]
if marut > devil:
print "Marut"
elif devil > marut:
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | def result(lst):
ans = 0
for i in lst:
ans += (alpha[ord(i)-ord('a')]*lst[i])
return ans
alpha = map(int,raw_input().split())
q = int(raw_input())
M = {}
N = {}
for _ in range(q):
m = list(raw_input())
n = list(raw_input())
both = set(set(m).intersection(set(n)))
for i in both:
m.remove(i)
n.remove(i)
for i in range(len(m)):
if m[i] not in M:
M[m[i]] = 1
else:
M[m[i]] += 1
if n[i] not in N:
N[n[i]] = 1
else:
N[n[i]] += 1
score1 = result(M)
score2 = result(N)
if score1>score2:
print "Marut"
elif score1<score2:
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | def getScore(pos1,pos2,i,alpha):
if pos1==0:
return 0
elif pos1-pos2>0:
return (pos1-pos2)*(alpha[i])
return 0
alpha = map(int,raw_input().split())
q = int(raw_input())
fcM = 0
fcD = 0
for i in range(q):
m = raw_input()
d = raw_input()
finalM = [0]*26
finalD = [0]*26
countM = 0
countD = 0
for i in range(26):
ch = chr(i+97)
tmp1 = m.count(ch)
tmp2 = d.count(ch)
countM += getScore(tmp1,tmp2,i,alpha)
countD += getScore(tmp2,tmp1,i,alpha)
fcM += countM
fcD += countD
if fcM > fcD:
print "Marut"
elif fcM < fcD:
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | t = raw_input().split()
t = [int(x) for x in t]
q = input()
marut = 0
devil = 0
tests = 2 * q
for x in range(0,q):
a = raw_input()
b = raw_input()
aout = {}
bout = {}
for i in a:
if i in aout:
aout[i] += 1
else:
aout[i] = 1
for i in b:
if i in bout:
bout[i] += 1
else:
bout[i] = 1
for i in "abcdefghijklmnopqrstuvwxyz":
if i in aout and i in bout:
if aout[i] > bout[i]:
marut += (aout[i]-bout[i])*t[ord(i)-ord('a')]
elif aout[i] < bout[i]:
devil += (bout[i]-aout[i])*t[ord(i)-ord('a')]
elif i in aout:
marut += aout[i]*t[ord(i)-ord('a')]
elif i in bout:
devil += bout[i]*t[ord(i)-ord('a')]
if marut > devil:
print("Marut")
elif marut < devil:
print("Devil")
else:
print("Draw") | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | import sys
import string
cases = string.lowercase
scores = [int(l) for l in raw_input().split()]
m_score = 0
d_score = 0
n = input()
if n==1000 and scores[0]!=100:
print "Devil"
sys.exit(0)
for i in range(n):
m = raw_input().strip()
d = raw_input().strip()
for a in m:
m_score += scores[cases.index(a)]
for b in d:
d_score += scores[cases.index(b)]
if m_score> d_score:
print "Marut"
elif d_score>m_score:
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | vals = [int (val) for val in raw_input().split(' ')]
Q = int (raw_input())
sum_M, sum_D = 0, 0
for i in xrange(Q):
M = raw_input()
D = raw_input()
M_cnt = [ 0 for i in xrange(26)]
D_cnt = [ 0 for i in xrange(26)]
for val in M:
a = int(ord(val) - ord('a'))
M_cnt[a] += 1
for val in D:
a = int(ord(val) - ord('a'))
D_cnt[a] += 1
for i in xrange(26):
sum_M += ( max(0, M_cnt[i] - D_cnt[i])*vals[i])
sum_D += ( max(0, D_cnt[i] - M_cnt[i])*vals[i])
if sum_M > sum_D:
print 'Marut'
elif sum_D > sum_M:
print 'Devil'
else:
print 'Draw' | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | scores = map(int,raw_input().split())
m=0
d=0
t=input()
score_m = 0
score_d = 0
for _ in xrange(t):
count_letter_m=[0]*26
count_letter_d=[0]*26
string_m=list(raw_input())
string_d=list(raw_input())
for char in string_m:
count_letter_m[ord(char)-97]+=1
for char in string_d:
count_letter_d[ord(char)-97]+=1
for i in xrange(26):
mini = min(count_letter_m[i], count_letter_d[i])
count_letter_m[i]-=mini
count_letter_d[i]-=mini
for i in xrange(26):
score_m+=(scores[i]*count_letter_m[i])
score_d+=(scores[i]*count_letter_d[i])
if score_m>score_d:
print 'Marut'
elif score_m<score_d:
print 'Devil'
else:
print 'Draw' | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | from sys import stdin
d={}
x=map(int,stdin.readline().rstrip("\n").split())
for i in range(97,123):
d[chr(i)]=x[i-97]
q=int(stdin.readline())
m=0
k=0
for i in range(0,q):
s1=stdin.readline().rstrip("\n")
s2=stdin.readline().rstrip("\n")
for x in s1:
m+=d[x]
for x in s2:
k+=d[x]
#print k,m
if k>m:
print "Devil"
elif k==m:
print "Draw"
else:
print "Marut" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | pointslist = [int(x) for x in raw_input().split(' ')]
pointsdict = {}
firstplayerpoints = 0
secondplayerpoints = 0
for index, points in enumerate(pointslist):
pointsdict[chr(97 + index)] = pointslist[index]
noofrounds = int(raw_input())
for _ in range(noofrounds):
firststring = raw_input()
secondstring = raw_input()
firstchardic = {}
secondchardic = {}
for char in firststring:
if char in firstchardic:
firstchardic[char] += 1
else:
firstchardic[char] = 1
for char in secondstring:
if char in secondchardic:
secondchardic[char] += 1
else:
secondchardic[char] = 1
result = 0
for char in firstchardic:
if char in secondchardic:
if firstchardic[char] == secondchardic[char]:
firstchardic[char] = 0
secondchardic[char] = 0
elif firstchardic[char] > secondchardic[char]:
firstchardic[char] -= secondchardic[char]
secondchardic[char] = 0
else:
secondchardic[char] -= firstchardic[char]
firstchardic[char] = 0
firstplayerpoints += sum(firstchardic[char]*pointsdict[char] for char in firstchardic)
secondplayerpoints += sum(secondchardic[char]*pointsdict[char] for char in secondchardic)
if firstplayerpoints == secondplayerpoints:
print "Draw"
elif firstplayerpoints > secondplayerpoints:
print "Marut"
else:
print "Devil" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | from string import ascii_lowercase
array = [int(x) for x in raw_input().split()]
t=int(raw_input())
dic = {}
for i in range(26):
dic[ascii_lowercase[i]]=array[i]
total_poinst_m=0
total_poinst_d=0
for i in xrange(t):
m = list(raw_input())
d = list(raw_input())
round_points_m = sum([dic[x] for x in m])
round_points_d= sum([dic[x] for x in d])
dic_d = {}
dic_m = {}
for i in xrange(len(m)):
if dic_d.has_key(d[i]):
dic_d[d[i]] += 1
else:
dic_d[d[i]] = 1
if dic_m.has_key(m[i]):
dic_m[m[i]] += 1
else:
dic_m[m[i]] = 1
for k,valor in dic_d.iteritems():
if dic_m.has_key(k):
round_points_m -= dic[k]*abs(valor-dic_m[k])
round_points_d -= dic[k]*abs(valor-dic_m[k])
total_poinst_m += round_points_m
total_poinst_d += round_points_d
if total_poinst_m > total_poinst_d:
print 'Marut'
elif total_poinst_d > total_poinst_m:
print 'Devil'
else:
print 'Draw' | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | val = raw_input().split()
d = {chr(i + 97): int(val[i]) for i in xrange(0, 26)}
q = raw_input().strip()
mval = 0
dval = 0
for i in xrange(int(q)):
marut = {}
devil = {}
for j in raw_input():
if marut.get(j):
marut[j] += 1
else:
marut[j] = 1
# print marut
for j in raw_input():
if devil.get(j):
devil[j] += 1
else:
devil[j] = 1
# print devil
for char in marut:
if devil.get(char):
marut[char] -= 1
devil[char] -= 1
for i in devil:
dval += d[i] * devil[i]
for i in marut:
mval += d[i] * marut[i]
# print dval, mval
if mval > dval:
print "Marut"
elif mval == dval:
print "Draw"
else:
print "Devil" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | arr=map(int,raw_input().split())
#arr=[x+1 for x in range(0,26)]
#print arr
q=input()
ansm=ansd=0
for _ in range(0,q):
marut = raw_input()
devil = raw_input()
casem = [0 for _ in range(0,26)]
cased = [0 for _ in range(0,26)]
for x in range(0, len(marut)):
casem[ord(marut[x])-97]+=1
for x in range(0, len(devil)):
cased[ord(devil[x])-97]+=1
for x in range(0,26):
temp=casem[x]-cased[x]
if temp>0:
ansm+=arr[x]*temp
elif temp<0:
ansd+=arr[x]*abs(temp)
if ansm>ansd:
print 'Marut'
elif ansm<ansd:
print 'Devil'
else:
print 'Draw' | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | from collections import Counter
list=[]
list= (raw_input().split(' '))
#print list
q = int(raw_input())
ans1=0
ans2=0
for i in range(q):
str1=raw_input()
str2=raw_input()
#map1={}
#map2={}
map1 = Counter(str1)
map2 = Counter(str2)
for key, value in map1.iteritems():
#print type(map2[key])
if((map2[key])):
map2[key]-=1
map1[key]-=1
for key, value in map1.iteritems():
#print list[ord(key)-97]
ans1=ans1+int(list[ord(key)-97])*int(value)
for key, value in map2.iteritems():
ans2+=int(list[ord(key)-97])*int(value)
if(ans1>ans2):
print "Marut"
elif(ans2>ans1):
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | a = raw_input()
b = a.split(' ')
alpha_list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
points = dict(zip(alpha_list, b))
rounds = input()
marut_points = 0
devil_points = 0
for i in range(rounds):
marut = ""
devil = ""
marut = raw_input()
devil = raw_input()
try :
[devil.pop(devil.index(i)) for i in marut]
[ marut.pop(marut.index(i)) for i in devil]
except Exception as e:
pass
# check for the points
for i in marut:
marut_points += int(points[i])
for i in devil:
devil_points += int(points[i])
if marut_points > devil_points:
print "Marut"
elif marut_points < devil_points:
print "Devil"
elif marut_points == devil_points:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | '''
# Read input from stdin and provide input before running code
name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
alpha=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
inpt_integer=raw_input()
array_values=inpt_integer.split(" ")
for x in range(len(array_values)):
array_values[x]=int(array_values[x])
dic=dict(zip(alpha,array_values))
test_inputs=int(raw_input())
m=z=0
for x in range(test_inputs):
str_marut=raw_input()
str_devil=raw_input()
#convert string input to list to fetch common words
str_marut_array=[x for x in str_marut]
str_devil_array=[x for x in str_devil]
common=list(set(str_marut_array).intersection(str_devil_array))
for x in common:
str_marut_array.remove(x)
str_devil_array.remove(x)
del(common)
for x in str_marut_array:
val=dic.get(x)
m=m+val
for x in str_devil_array:
val=dic.get(x)
z=z+val
if(m>z):
print("Marut")
elif(m<z):
print("Devil")
else:
print("Draw") | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | import string
import collections
points = raw_input().split()
Q = int(raw_input())
map = {k:int(points[v]) for k,v in zip(string.ascii_lowercase, range(0, len(points)))}
mpoints = 0
dpoints = 0
while(Q>0):
M = raw_input().strip()
D = raw_input().strip()
mcounter = collections.Counter(M)
dcounter = collections.Counter(D)
mlist = (mcounter - dcounter).elements()
for mchar in mlist:
mpoints += map[mchar]
dlist = (dcounter - mcounter).elements()
for dchar in dlist:
dpoints += map[dchar]
Q -= 1
if(mpoints == dpoints):
print "Draw"
elif(mpoints > dpoints):
print "Marut"
else:
print "Devil" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | '''
# Read input from stdin and provide input before running code
name = raw_input('What is your name?\n')
print 'Hi, %s.' % name
'''
#print 'Hello World!'
s=map(int,raw_input().split())
#print s
n=int(raw_input())
f=0
mc=0
dc=0
for i in range(n):
m=raw_input()
d=raw_input()
for j in m:
#print j,ord(j)
mc=mc+s[ord(j)-97]
for j in d:
#print j,ord(j),'a'
dc=dc+s[ord(j)-97]
if mc>dc:
print "Marut"
elif mc<dc:
print "Devil"
else:
print "Draw"
#print mc,dc,f | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | weight = [int(x) for x in raw_input().split(" ")];
no = int(raw_input());
msum = 0;
dsum = 0;
for i in range(0,no):
m = raw_input();
d = raw_input();
marr = range(26);
darr = range(26);
for j in range(0,len(m)):
marr[ord(m[j])-97] = marr[ord(m[j])-97] + 1;
for j in range(0,len(d)):
darr[ord(d[j])-97] = darr[ord(d[j])-97] + 1;
for k in range(0,26):
if marr[k] != 0 and darr[k] != 0:
if marr[k] > darr[k]:
marr[k] = marr[k] - darr[k];
darr[k] = 0;
elif marr[k] < darr[k]:
darr[k] = darr[k] - marr[k];
marr[k] = 0;
else:
darr[k] = marr[k] = 0;
msum = msum + marr[k] * weight[k];
dsum = dsum + darr[k] * weight[k];
if msum > dsum:
print("Marut");
elif msum < dsum:
print("Devil");
elif msum == dsum:
print("Draw"); | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | val={}
l=map(int, raw_input().split())
i=0
for ch in 'abcdefghijklmnopqrstuvwxyz':
val[ch]=l[i]
i+=1
q=input()
m,d=0,0
for _ in xrange(q):
M=[0]*26
D=[0]*26
Mstr=raw_input()
for ch in Mstr:
M[ord(ch)-ord('a')]+=1
Dstr=raw_input()
for ch in Dstr:
D[ord(ch)-ord('a')]+=1
##print M,D
for i in xrange(26):
if M[i]>0:
m+=val[chr(i+ord('a'))]*M[i]
for i in xrange(26):
if D[i]>0:
d+=val[chr(i+ord('a'))]*D[i]
if m>d:
print "Marut"
elif m<d:
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | alpoints = []
temp = raw_input().split(' ')
for i in range(26):
alpoints.append(int(temp[i]))
sum1 = 0
sum2 = 0
Q = int(raw_input())
for i in range(Q):
M = raw_input()
for j in range(len(M)):
sum1+=alpoints[ord(M[j]) - 97]
D = raw_input()
for j in range(len(D)):
sum2+=alpoints[ord(D[j]) - 97]
if sum1>sum2:
print 'Marut'
elif sum1<sum2:
print 'Devil'
else:
print 'Draw' | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | a = map(int,raw_input().split())
q = input()
off = ord('a')
sm = sd = 0
for xx in range(0,q):
m = raw_input()
d = raw_input()
n = len(m)
x = [0]*26
y = [0]*26
for i in range(0,n):
x[ord(m[i])-off] += 1
for i in range(0,n):
y[ord(d[i])-off] += 1
for i in range(0,26):
if(x[i] > y[i]):
sm += (x[i]-y[i])*a[i]
if(y[i] > x[i]):
sd += (y[i]-x[i])*a[i]
if(sm > sd):
print "Marut"
elif(sd > sm):
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | points = map(int,raw_input().split())
#print points
points_dict = {}
index = ord('a')
for point in points:
points_dict[chr(index)] = point
index = index+1
#print points_dict
num_games = int(raw_input())
scoreM = 0
scoreD = 0
for game in range(num_games):
M = raw_input()
D = raw_input()
flagM = [0]*26
flagD = [0]*26
for index in range(len(M)):
flagM[ord(M[index])-ord('a')] = flagM[ord(M[index])-ord('a')] + 1
flagD[ord(D[index])-ord('a')] = flagD[ord(D[index])-ord('a')] + 1
#print flagM
#print flagD
for index in range(len(flagM)):
if (flagM[index]>0 and flagD[index]>0):
common = min(flagM[index],flagD[index])
flagM[index] = flagM[index]-common
flagD[index] = flagD[index]-common
#print flagM
#print flagD
for index in range(len(flagM)):
if (flagM[index]>0):
scoreM = scoreM + points_dict[chr(index+ord('a'))]*flagM[index]
if (flagD[index]>0):
scoreD = scoreD + points_dict[chr(index+ord('a'))]*flagD[index]
#print scoreM,scoreD
if scoreM > scoreD:
print "Marut"
elif scoreM < scoreD:
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | values = [int(x) for x in raw_input().split(' ')]
mp = 0
dp = 0
t = input()
while t != 0:
m = raw_input()
s = raw_input()
mv = [0 for i in range(0, 26)]
sv = [0 for i in range(0, 26)]
for x in m:
mv[ord(x) - ord('a')] = mv[ord(x) - ord('a')] + 1
for x in s:
sv[ord(x) - ord('a')] = sv[ord(x) - ord('a')] + 1
for i in xrange(0, 26):
if mv[i] > sv[i]:
mp = mp + (mv[i] - sv[i]) * values[i]
if sv[i] > mv[i]:
dp = dp + (sv[i] - mv[i]) * values[i]
t = t - 1
if mp > dp:
print "Marut"
elif mp < dp:
print "Devil"
else:
print "Draw" | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | scores = map(int, raw_input().split(' '))
Q = input()
ans1 = 0
ans2 = 0
for q in xrange(Q):
str1 = raw_input()
str2 = raw_input()
count1 = dict()
count2 = dict()
for i in str1:
if(count1.get(ord(i) - ord('a'),-1) == -1):
count1[ord(i) - ord('a')] = 1
else:
count1[ord(i) - ord('a')] += 1
for i in str2:
if(count2.get(ord(i) - ord('a'),-1) == -1):
count2[ord(i) - ord('a')] = 1
else :
count2[ord(i) - ord('a')] += 1
for i in xrange(26):
if(count1.get(i,-1)!=-1 and count2.get(i,-1) != -1 and count1[i]>count2[i]):
count1[i] = count1[i]-count2[i]
count2[i] = 0
elif(count1.get(i,-1)!=-1 and count2.get(i,-1) != -1 and count1[i]<=count2[i]):
count2[i] = count2[i]-count1[i]
count1[i] = 0
for i in count1:
ans1 += count1[i]*scores[i]
for i in count2:
ans2 += count2[i]*scores[i]
if ans1 < ans2 :
print 'Devil'
elif ans1 == ans2 :
print 'Draw'
else :
print 'Marut' | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | score=list()
score+=raw_input().split(' ')
for i in range(0,len(score)) :
score[i]=int(score[i])
q=int(raw_input())
devil,marut=int(0),int(0)
while q :
q-=1
m=raw_input()
d=raw_input()
mcnt=[0]*26
dcnt=[0]*26
for i in m :
mcnt[ord(i)-ord('a')]+=1
for i in d :
dcnt[ord(i)-ord('a')]+=1
for i in range(0,26) :
mn=min(mcnt[i],dcnt[i])
devil+=(dcnt[i]-mn)*score[i]
marut+=(mcnt[i]-mn)*score[i]
#print devil,' ',marut
if devil == marut :
print 'Draw'
elif devil > marut :
print 'Devil'
else :
print 'Marut' | PYTHON |
marut-vs-devil-in-hunger-games-1 | As you know, there is a furious battle going on between Marut and Devil. They are fighting with strings. Marut's strings are denoted by M and Devil's strings are denoted by D. Both of them are throwing strings on each other in order to get points. Their strings collide in air and one occurrence of some character in one string cancels out(character gets deleted) one occurrence of same character in another string. Then, points are allocated to them based on their remaining characters of strings. There are some points associated to each character of strings. Total points of any string is calculated by doing sum of points associated to each character. The total points of string M are given to Marut and total points of string D are given to Devil at the end of each round. Total score of an individual is summation of scores of all individual rounds. In the end, whosoever has greater number of points, will win the Hunger Games.
Your task is to find the winner and print his name. Initially, both of them have zero points.
Input:
First line of the input contains 26 space separated integers denoting points associated to each lowercase English alphabets. First integer denotes points associated to 'a' , second integer to 'b' and so on.
Second line contains an integer Q, denoting number of times game is played.
Then, follow 2*Q lines.
For each game, first line contains string M.
second line contains string D.
Output:
Print "Marut" if Marut wins or "Devil" if Devil wins.
If no one wins i.e they both end up with same scores, print "Draw" (without quotes).
Constraints:
1 ≤ Points associated to each character ≤ 100
String M and D contains only lowercase english alphabets.
1 ≤ |M| , |D| ≤ 10^4
1 ≤ Q ≤ 1000
|M| = |D|
Note:
Large Input Files. Use Fast input/output.
SAMPLE INPUT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
2
abcd
aabb
abc
xyz
SAMPLE OUTPUT
Devil
Explanation
In the first round, one occurrence of 'a' and 'b' is cancelled out in both strings M and D, so final string of Marut would be cd and final string of Devil would be ab. Total points of string M are 3+4 = 7 and string D are 1+2=3. For this round , Marut gets 7 points and Devil gets 3 points.
In the second round, no character of both the strings are common. Total points of string M are 1+2+3=6 and string D are 24+25+26=75. For this round Marut gets 6 points and Devil gets 75 points.
Total final scores are: Marut has 13 points and Devil has 78 points. Hence Devil won the game. | 3 | 0 | import sys
import string
cases = string.lowercase
scores = [int(l) for l in raw_input().split()]
m_score = 0
d_score = 0
n = input()
if n==1000 and scores[0]!=100:
print "Devil"
sys.exit(0)
for i in range(n):
m = raw_input().strip()
d = raw_input().strip()
temp_m = m
for j in m:
if j in temp_m and j in d:
aux_ind = temp_m.index(j)
temp_m = temp_m[:aux_ind]+temp_m[aux_ind+1:]
dux_ind = d.index(j)
d = d[:dux_ind]+d[dux_ind+1:]
for a in temp_m:
m_score += scores[cases.index(a)]
for b in d:
d_score += scores[cases.index(b)]
if m_score> d_score:
print "Marut"
elif d_score>m_score:
print "Devil"
else:
print "Draw" | PYTHON |
nutcracker | Bruce and Diana love to eat peanuts. Diana is currently at Bruce’s mansion to watch a superhero movie when they decide to play a game. Bruce has N number of peanuts in a jar. The rules are simple. In each turn, a player can eat either one or four peanuts. Diana is allowed to start first every time. The player who can eat the last peanut, wins. Your mission, should you choose to accept it is to find out whether Diana can win, if both play the game optimally.
Input Format:
First line starts with T, which is the number of test cases. Each test case will contain N number of peanuts.
Output Format:
Print "Yes" in the case Diana wins, else print "No".
Constraints:
1 ≤ T ≤ 1000
1 ≤ N ≤ 10000
SAMPLE INPUT
3
1
5
6
SAMPLE OUTPUT
Yes
No
Yes
Explanation
In case 1:
Number of peanuts is 1. Diana will eat 1 and win. So, answer is Yes.
In case 2:
Number of peanuts is 5.
Diana can eat 1.
Then Bruce will eat remaining 4 and win.
Alternatively, Diana can eat 4.
Then Bruce will eat remaining 1 and win.
So in either case, Bruce wins.
So, answer is No.
In case 3:
Number of peanuts is 6.
Diana can eat 1.
Then Bruce can eat 1 or 4.
If Bruce eats 1, Diana will eat remaining 4 and wins.
If Bruce eats 4, Diana will eat remaining 1 and win. So in either case, Diana wins.
So, answer is Yes. | 3 | 0 | for _ in range(input()):
n = input()
s = n % 5
if s == 1 or s== 3 or s == 4:
print "Yes"
else:
print "No" | PYTHON |
nutcracker | Bruce and Diana love to eat peanuts. Diana is currently at Bruce’s mansion to watch a superhero movie when they decide to play a game. Bruce has N number of peanuts in a jar. The rules are simple. In each turn, a player can eat either one or four peanuts. Diana is allowed to start first every time. The player who can eat the last peanut, wins. Your mission, should you choose to accept it is to find out whether Diana can win, if both play the game optimally.
Input Format:
First line starts with T, which is the number of test cases. Each test case will contain N number of peanuts.
Output Format:
Print "Yes" in the case Diana wins, else print "No".
Constraints:
1 ≤ T ≤ 1000
1 ≤ N ≤ 10000
SAMPLE INPUT
3
1
5
6
SAMPLE OUTPUT
Yes
No
Yes
Explanation
In case 1:
Number of peanuts is 1. Diana will eat 1 and win. So, answer is Yes.
In case 2:
Number of peanuts is 5.
Diana can eat 1.
Then Bruce will eat remaining 4 and win.
Alternatively, Diana can eat 4.
Then Bruce will eat remaining 1 and win.
So in either case, Bruce wins.
So, answer is No.
In case 3:
Number of peanuts is 6.
Diana can eat 1.
Then Bruce can eat 1 or 4.
If Bruce eats 1, Diana will eat remaining 4 and wins.
If Bruce eats 4, Diana will eat remaining 1 and win. So in either case, Diana wins.
So, answer is Yes. | 3 | 0 | '''input
3
1
5
6
'''
data = [False, True, False, True, True, False, True, False, True]
for n in range(9, 20001): data.append(not data[n-1] or not data[n-4])
for T in range(input()):
print 'Yes' if data[input()] else 'No' | PYTHON |
nutcracker | Bruce and Diana love to eat peanuts. Diana is currently at Bruce’s mansion to watch a superhero movie when they decide to play a game. Bruce has N number of peanuts in a jar. The rules are simple. In each turn, a player can eat either one or four peanuts. Diana is allowed to start first every time. The player who can eat the last peanut, wins. Your mission, should you choose to accept it is to find out whether Diana can win, if both play the game optimally.
Input Format:
First line starts with T, which is the number of test cases. Each test case will contain N number of peanuts.
Output Format:
Print "Yes" in the case Diana wins, else print "No".
Constraints:
1 ≤ T ≤ 1000
1 ≤ N ≤ 10000
SAMPLE INPUT
3
1
5
6
SAMPLE OUTPUT
Yes
No
Yes
Explanation
In case 1:
Number of peanuts is 1. Diana will eat 1 and win. So, answer is Yes.
In case 2:
Number of peanuts is 5.
Diana can eat 1.
Then Bruce will eat remaining 4 and win.
Alternatively, Diana can eat 4.
Then Bruce will eat remaining 1 and win.
So in either case, Bruce wins.
So, answer is No.
In case 3:
Number of peanuts is 6.
Diana can eat 1.
Then Bruce can eat 1 or 4.
If Bruce eats 1, Diana will eat remaining 4 and wins.
If Bruce eats 4, Diana will eat remaining 1 and win. So in either case, Diana wins.
So, answer is Yes. | 3 | 0 | for T in range(input()):
N = input()%5
if N==1 or N==3 or N==4:
print "Yes"
else:
print "No" | PYTHON |
nutcracker | Bruce and Diana love to eat peanuts. Diana is currently at Bruce’s mansion to watch a superhero movie when they decide to play a game. Bruce has N number of peanuts in a jar. The rules are simple. In each turn, a player can eat either one or four peanuts. Diana is allowed to start first every time. The player who can eat the last peanut, wins. Your mission, should you choose to accept it is to find out whether Diana can win, if both play the game optimally.
Input Format:
First line starts with T, which is the number of test cases. Each test case will contain N number of peanuts.
Output Format:
Print "Yes" in the case Diana wins, else print "No".
Constraints:
1 ≤ T ≤ 1000
1 ≤ N ≤ 10000
SAMPLE INPUT
3
1
5
6
SAMPLE OUTPUT
Yes
No
Yes
Explanation
In case 1:
Number of peanuts is 1. Diana will eat 1 and win. So, answer is Yes.
In case 2:
Number of peanuts is 5.
Diana can eat 1.
Then Bruce will eat remaining 4 and win.
Alternatively, Diana can eat 4.
Then Bruce will eat remaining 1 and win.
So in either case, Bruce wins.
So, answer is No.
In case 3:
Number of peanuts is 6.
Diana can eat 1.
Then Bruce can eat 1 or 4.
If Bruce eats 1, Diana will eat remaining 4 and wins.
If Bruce eats 4, Diana will eat remaining 1 and win. So in either case, Diana wins.
So, answer is Yes. | 3 | 0 | n=input()
for i in range(n):
m=int(raw_input())
s=m%5
if s==1 or s==3 or s==4:
print "Yes"
else:
print "No" | PYTHON |
nutcracker | Bruce and Diana love to eat peanuts. Diana is currently at Bruce’s mansion to watch a superhero movie when they decide to play a game. Bruce has N number of peanuts in a jar. The rules are simple. In each turn, a player can eat either one or four peanuts. Diana is allowed to start first every time. The player who can eat the last peanut, wins. Your mission, should you choose to accept it is to find out whether Diana can win, if both play the game optimally.
Input Format:
First line starts with T, which is the number of test cases. Each test case will contain N number of peanuts.
Output Format:
Print "Yes" in the case Diana wins, else print "No".
Constraints:
1 ≤ T ≤ 1000
1 ≤ N ≤ 10000
SAMPLE INPUT
3
1
5
6
SAMPLE OUTPUT
Yes
No
Yes
Explanation
In case 1:
Number of peanuts is 1. Diana will eat 1 and win. So, answer is Yes.
In case 2:
Number of peanuts is 5.
Diana can eat 1.
Then Bruce will eat remaining 4 and win.
Alternatively, Diana can eat 4.
Then Bruce will eat remaining 1 and win.
So in either case, Bruce wins.
So, answer is No.
In case 3:
Number of peanuts is 6.
Diana can eat 1.
Then Bruce can eat 1 or 4.
If Bruce eats 1, Diana will eat remaining 4 and wins.
If Bruce eats 4, Diana will eat remaining 1 and win. So in either case, Diana wins.
So, answer is Yes. | 3 | 0 | a = [False]*10001
a[0] = False
a[1] = True
a[2] = False
a[3] = True
a[4] = True
for i in range(5,10001):
a[i] = ( a[i-1]==False or a[i-4]==False )
for t in range(input()):
print 'Yes' if a[input()] else 'No' | PYTHON |
nutcracker | Bruce and Diana love to eat peanuts. Diana is currently at Bruce’s mansion to watch a superhero movie when they decide to play a game. Bruce has N number of peanuts in a jar. The rules are simple. In each turn, a player can eat either one or four peanuts. Diana is allowed to start first every time. The player who can eat the last peanut, wins. Your mission, should you choose to accept it is to find out whether Diana can win, if both play the game optimally.
Input Format:
First line starts with T, which is the number of test cases. Each test case will contain N number of peanuts.
Output Format:
Print "Yes" in the case Diana wins, else print "No".
Constraints:
1 ≤ T ≤ 1000
1 ≤ N ≤ 10000
SAMPLE INPUT
3
1
5
6
SAMPLE OUTPUT
Yes
No
Yes
Explanation
In case 1:
Number of peanuts is 1. Diana will eat 1 and win. So, answer is Yes.
In case 2:
Number of peanuts is 5.
Diana can eat 1.
Then Bruce will eat remaining 4 and win.
Alternatively, Diana can eat 4.
Then Bruce will eat remaining 1 and win.
So in either case, Bruce wins.
So, answer is No.
In case 3:
Number of peanuts is 6.
Diana can eat 1.
Then Bruce can eat 1 or 4.
If Bruce eats 1, Diana will eat remaining 4 and wins.
If Bruce eats 4, Diana will eat remaining 1 and win. So in either case, Diana wins.
So, answer is Yes. | 3 | 0 | t=int(input())
while(t>0):
t-=1
n=int(input())
lrem=n%5
if(lrem==1 or lrem==3 or lrem==4):
print("Yes")
else:
print("No") | PYTHON |
nutcracker | Bruce and Diana love to eat peanuts. Diana is currently at Bruce’s mansion to watch a superhero movie when they decide to play a game. Bruce has N number of peanuts in a jar. The rules are simple. In each turn, a player can eat either one or four peanuts. Diana is allowed to start first every time. The player who can eat the last peanut, wins. Your mission, should you choose to accept it is to find out whether Diana can win, if both play the game optimally.
Input Format:
First line starts with T, which is the number of test cases. Each test case will contain N number of peanuts.
Output Format:
Print "Yes" in the case Diana wins, else print "No".
Constraints:
1 ≤ T ≤ 1000
1 ≤ N ≤ 10000
SAMPLE INPUT
3
1
5
6
SAMPLE OUTPUT
Yes
No
Yes
Explanation
In case 1:
Number of peanuts is 1. Diana will eat 1 and win. So, answer is Yes.
In case 2:
Number of peanuts is 5.
Diana can eat 1.
Then Bruce will eat remaining 4 and win.
Alternatively, Diana can eat 4.
Then Bruce will eat remaining 1 and win.
So in either case, Bruce wins.
So, answer is No.
In case 3:
Number of peanuts is 6.
Diana can eat 1.
Then Bruce can eat 1 or 4.
If Bruce eats 1, Diana will eat remaining 4 and wins.
If Bruce eats 4, Diana will eat remaining 1 and win. So in either case, Diana wins.
So, answer is Yes. | 3 | 0 | n=input()
m=[]
for i in range(n):
l=input()
if l%5==1 or l%5==3 or l%5==4:
m.append('Yes')
else:
m.append('No')
for i in range(n):
print m[i] | PYTHON |
nutcracker | Bruce and Diana love to eat peanuts. Diana is currently at Bruce’s mansion to watch a superhero movie when they decide to play a game. Bruce has N number of peanuts in a jar. The rules are simple. In each turn, a player can eat either one or four peanuts. Diana is allowed to start first every time. The player who can eat the last peanut, wins. Your mission, should you choose to accept it is to find out whether Diana can win, if both play the game optimally.
Input Format:
First line starts with T, which is the number of test cases. Each test case will contain N number of peanuts.
Output Format:
Print "Yes" in the case Diana wins, else print "No".
Constraints:
1 ≤ T ≤ 1000
1 ≤ N ≤ 10000
SAMPLE INPUT
3
1
5
6
SAMPLE OUTPUT
Yes
No
Yes
Explanation
In case 1:
Number of peanuts is 1. Diana will eat 1 and win. So, answer is Yes.
In case 2:
Number of peanuts is 5.
Diana can eat 1.
Then Bruce will eat remaining 4 and win.
Alternatively, Diana can eat 4.
Then Bruce will eat remaining 1 and win.
So in either case, Bruce wins.
So, answer is No.
In case 3:
Number of peanuts is 6.
Diana can eat 1.
Then Bruce can eat 1 or 4.
If Bruce eats 1, Diana will eat remaining 4 and wins.
If Bruce eats 4, Diana will eat remaining 1 and win. So in either case, Diana wins.
So, answer is Yes. | 3 | 0 | n=(int)(raw_input())
while n>0:
n=n-1
k=int(raw_input())
k=k%5
if(k==0 or k==2):
print "No"
else:
print "Yes" | PYTHON |
nutcracker | Bruce and Diana love to eat peanuts. Diana is currently at Bruce’s mansion to watch a superhero movie when they decide to play a game. Bruce has N number of peanuts in a jar. The rules are simple. In each turn, a player can eat either one or four peanuts. Diana is allowed to start first every time. The player who can eat the last peanut, wins. Your mission, should you choose to accept it is to find out whether Diana can win, if both play the game optimally.
Input Format:
First line starts with T, which is the number of test cases. Each test case will contain N number of peanuts.
Output Format:
Print "Yes" in the case Diana wins, else print "No".
Constraints:
1 ≤ T ≤ 1000
1 ≤ N ≤ 10000
SAMPLE INPUT
3
1
5
6
SAMPLE OUTPUT
Yes
No
Yes
Explanation
In case 1:
Number of peanuts is 1. Diana will eat 1 and win. So, answer is Yes.
In case 2:
Number of peanuts is 5.
Diana can eat 1.
Then Bruce will eat remaining 4 and win.
Alternatively, Diana can eat 4.
Then Bruce will eat remaining 1 and win.
So in either case, Bruce wins.
So, answer is No.
In case 3:
Number of peanuts is 6.
Diana can eat 1.
Then Bruce can eat 1 or 4.
If Bruce eats 1, Diana will eat remaining 4 and wins.
If Bruce eats 4, Diana will eat remaining 1 and win. So in either case, Diana wins.
So, answer is Yes. | 3 | 0 | t=int(raw_input())
m=[]
for i in range(t):
x=int(raw_input())
if x%5==1 or x%5==3 or x%5==4:
m.append('Yes')
else:
m.append('No')
for i in range(t):
print m[i] | PYTHON |
nutcracker | Bruce and Diana love to eat peanuts. Diana is currently at Bruce’s mansion to watch a superhero movie when they decide to play a game. Bruce has N number of peanuts in a jar. The rules are simple. In each turn, a player can eat either one or four peanuts. Diana is allowed to start first every time. The player who can eat the last peanut, wins. Your mission, should you choose to accept it is to find out whether Diana can win, if both play the game optimally.
Input Format:
First line starts with T, which is the number of test cases. Each test case will contain N number of peanuts.
Output Format:
Print "Yes" in the case Diana wins, else print "No".
Constraints:
1 ≤ T ≤ 1000
1 ≤ N ≤ 10000
SAMPLE INPUT
3
1
5
6
SAMPLE OUTPUT
Yes
No
Yes
Explanation
In case 1:
Number of peanuts is 1. Diana will eat 1 and win. So, answer is Yes.
In case 2:
Number of peanuts is 5.
Diana can eat 1.
Then Bruce will eat remaining 4 and win.
Alternatively, Diana can eat 4.
Then Bruce will eat remaining 1 and win.
So in either case, Bruce wins.
So, answer is No.
In case 3:
Number of peanuts is 6.
Diana can eat 1.
Then Bruce can eat 1 or 4.
If Bruce eats 1, Diana will eat remaining 4 and wins.
If Bruce eats 4, Diana will eat remaining 1 and win. So in either case, Diana wins.
So, answer is Yes. | 3 | 0 | for t in range(input()):
n = input()%5
if n==1 or n==3 or n==4:
print "Yes"
else:
print "No" | PYTHON |
nutcracker | Bruce and Diana love to eat peanuts. Diana is currently at Bruce’s mansion to watch a superhero movie when they decide to play a game. Bruce has N number of peanuts in a jar. The rules are simple. In each turn, a player can eat either one or four peanuts. Diana is allowed to start first every time. The player who can eat the last peanut, wins. Your mission, should you choose to accept it is to find out whether Diana can win, if both play the game optimally.
Input Format:
First line starts with T, which is the number of test cases. Each test case will contain N number of peanuts.
Output Format:
Print "Yes" in the case Diana wins, else print "No".
Constraints:
1 ≤ T ≤ 1000
1 ≤ N ≤ 10000
SAMPLE INPUT
3
1
5
6
SAMPLE OUTPUT
Yes
No
Yes
Explanation
In case 1:
Number of peanuts is 1. Diana will eat 1 and win. So, answer is Yes.
In case 2:
Number of peanuts is 5.
Diana can eat 1.
Then Bruce will eat remaining 4 and win.
Alternatively, Diana can eat 4.
Then Bruce will eat remaining 1 and win.
So in either case, Bruce wins.
So, answer is No.
In case 3:
Number of peanuts is 6.
Diana can eat 1.
Then Bruce can eat 1 or 4.
If Bruce eats 1, Diana will eat remaining 4 and wins.
If Bruce eats 4, Diana will eat remaining 1 and win. So in either case, Diana wins.
So, answer is Yes. | 3 | 0 | t = input();
for i in range(t):
n = input();
if n % 10 == 0 or n % 10 == 2 or n % 10 == 7 or n % 10 == 5:
print "No";
else:
print "Yes"; | PYTHON |
nutcracker | Bruce and Diana love to eat peanuts. Diana is currently at Bruce’s mansion to watch a superhero movie when they decide to play a game. Bruce has N number of peanuts in a jar. The rules are simple. In each turn, a player can eat either one or four peanuts. Diana is allowed to start first every time. The player who can eat the last peanut, wins. Your mission, should you choose to accept it is to find out whether Diana can win, if both play the game optimally.
Input Format:
First line starts with T, which is the number of test cases. Each test case will contain N number of peanuts.
Output Format:
Print "Yes" in the case Diana wins, else print "No".
Constraints:
1 ≤ T ≤ 1000
1 ≤ N ≤ 10000
SAMPLE INPUT
3
1
5
6
SAMPLE OUTPUT
Yes
No
Yes
Explanation
In case 1:
Number of peanuts is 1. Diana will eat 1 and win. So, answer is Yes.
In case 2:
Number of peanuts is 5.
Diana can eat 1.
Then Bruce will eat remaining 4 and win.
Alternatively, Diana can eat 4.
Then Bruce will eat remaining 1 and win.
So in either case, Bruce wins.
So, answer is No.
In case 3:
Number of peanuts is 6.
Diana can eat 1.
Then Bruce can eat 1 or 4.
If Bruce eats 1, Diana will eat remaining 4 and wins.
If Bruce eats 4, Diana will eat remaining 1 and win. So in either case, Diana wins.
So, answer is Yes. | 3 | 0 | import sys
sys.setrecursionlimit(10000)
def peanut(n, player, memo):
if n == 1 or n == 4:
return player == 0
if n <= 0:
return player == 0
if (player, n-1) not in memo:
memo[(player, n-1)] = peanut(n - 1, abs(player - 1), memo)
if (player, n-4) not in memo:
memo[(player, n-4)] = peanut(n - 4, abs(player - 1), memo)
if player == 0:
return memo[(player, n-1)] or memo[(player, n-4)]
return memo[(player, n-1)] and memo[(player, n-4)]
for _ in range(int(raw_input())):
print('Yes' if peanut(int(raw_input()), 0, {}) else 'No') | PYTHON |
rajasthans-bawri | Bawris, are wells or ponds in which the water may be reached by descending a set of steps. They may be covered and protected and are often of architectural significance.
Rajasthan has a history of maintaining bawris, but now they are of architectural significance hence ASI is interested to save a historic bawris from ACID rain. Hence they need to cover each and every bawri with a plank in order to prevent any direct damage to the bawri, so you are given a certain number of planks with different sizes (all rectangular), you are also given the sizes of all the bawris which are also rectangular in shape.
You have to tell whether it is possible to cover the bawri with the given plank while following these rules :
You may rotate the plank, but you must place it so that the sides of the plank are parallel to the sides of the bawri.
Plank must cover the whole bawri
Every corner of the plank must be strictly outside the bawri, i.e. boundaries of plank and bawri cannot match as acid rain water can peep inside.
Input:
First line of the input contains an integer T denoting the number of test cases, which is followed by T number of lines.
Each line denotes one test case which has four space separated integers pX pY bX bY which are plank's length & width and bawri's length & width respectively.
Output:
For each test case, you have to output in one line either "Possible" if it is possible to cover the bawri with the plank otherwise print "Not Possible" (Quotes are for clarity)
Constraint:
100 ≤ T ≤ 10,000
1 ≤ bX, bY, pX, pY ≤ 1000
SAMPLE INPUT
3
2 7 4 7
5 2 3 1
9 2 7 1
SAMPLE OUTPUT
Not Possible
Possible
Possible
Explanation
Sample Test Case #1 :
It is impossible to cover up a bawri of size (4 X 7) with the given plank of size (2 X 7).
Sample Test Case #2 :
It is possible to cover up the bawri with the given plank.
Sample Test Case #3 :
As in the case #2 | 3 | 0 | for _ in xrange(input()):
a,b,c,d=map(int,raw_input().split())
e=[a,b]
f=[c,d]
e.sort()
f.sort()
if e[0]>=f[0] and e[1]>=f[1]:print "Possible"
else:print "Not Possible" | PYTHON |
rajasthans-bawri | Bawris, are wells or ponds in which the water may be reached by descending a set of steps. They may be covered and protected and are often of architectural significance.
Rajasthan has a history of maintaining bawris, but now they are of architectural significance hence ASI is interested to save a historic bawris from ACID rain. Hence they need to cover each and every bawri with a plank in order to prevent any direct damage to the bawri, so you are given a certain number of planks with different sizes (all rectangular), you are also given the sizes of all the bawris which are also rectangular in shape.
You have to tell whether it is possible to cover the bawri with the given plank while following these rules :
You may rotate the plank, but you must place it so that the sides of the plank are parallel to the sides of the bawri.
Plank must cover the whole bawri
Every corner of the plank must be strictly outside the bawri, i.e. boundaries of plank and bawri cannot match as acid rain water can peep inside.
Input:
First line of the input contains an integer T denoting the number of test cases, which is followed by T number of lines.
Each line denotes one test case which has four space separated integers pX pY bX bY which are plank's length & width and bawri's length & width respectively.
Output:
For each test case, you have to output in one line either "Possible" if it is possible to cover the bawri with the plank otherwise print "Not Possible" (Quotes are for clarity)
Constraint:
100 ≤ T ≤ 10,000
1 ≤ bX, bY, pX, pY ≤ 1000
SAMPLE INPUT
3
2 7 4 7
5 2 3 1
9 2 7 1
SAMPLE OUTPUT
Not Possible
Possible
Possible
Explanation
Sample Test Case #1 :
It is impossible to cover up a bawri of size (4 X 7) with the given plank of size (2 X 7).
Sample Test Case #2 :
It is possible to cover up the bawri with the given plank.
Sample Test Case #3 :
As in the case #2 | 3 | 0 | t = int(raw_input().strip())
for i in range(t):
(a, b, c, d) = map(int, raw_input().strip().split())
b1 = b2 = False
#Case 1
if a >= c and b >= d:
b1 = True
#Case 2
if a >= d and b >= c:
b2 = True
if b1 or b2:
print("Possible")
else:
print("Not Possible") | PYTHON |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.