forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatriosh.cpp
75 lines (75 loc) · 1.02 KB
/
matriosh.cpp
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// 2008-06-26
#include <cstdio>
#ifdef _MSC_VER
#define GC getchar
#else
#define GC getchar_unlocked
#endif
int input(int& x)
{
char c=GC();
if (c==-1) return -1;
else ungetc(c,stdin);
bool minus=false;
x=0;
for(;;)
{
c=GC();
if (c==' '||c=='\n')
break;
else if (c=='-')
minus=true;
else
x=10*x+c-'0';
}
if (minus) x=-x;
if (c==' ') return 0;
return 1;
}
int main()
{
static int cont[100000];
static int item[100000];
for(;;)
{
int layers=0;
cont[0]=0;
int x;
bool b=true;
for(;;)
{
int r=input(x);
if (r==-1) return 0;
if (b)
{
if (x<0)
{
item[layers]=-x;
cont[layers++]=0;
}
else
{
if (!layers||item[layers-1]!=x)
b=false;
else
{
layers--;
if (layers)
{
cont[layers-1]+=x;
if (cont[layers-1]>=item[layers-1])
b=false;
}
else if (!r)
b=false;
}
}
}
if (r) break;
}
if (b&&!layers)
printf(":-) Matrioshka!\n");
else
printf(":-( Try again.\n");
}
}