×

# SUPVIL - Editorial

Author: Pawel Kacprzak
Tester: Misha Chorniy
Editorialist: Pawel Kacprzak

CAKEWALK

# PREREQUISITES:

Basic programming

# PROBLEM:

For a given list $N$ people, where each is either a superhero or a villain, joining the fight between superheroes and villains in the given order, decide which side wins or if the fight ends up with a draw. Superheroes win immediately at the earliest time when there are $2$ more superheroes fighting than villains. Villains win immediately at the earliest time when there are $3$ more villains fighting than superheroes. If none of these cases happens, then the fight results in a draw.

# QUICK EXPLANATION:

Whenever a person joins the fight check if one of the sides wins. If yes, print the name of winning group, otherwise continue. If no side wins after all people joined, print draw.

# EXPLANATION:

Solutions to both subtasks are based on the approach described in quick explanation, but they differ complexity of checking if one side wins.

In the first subtask $N$ is at most $500$, so after every person joining the fight, one can count the number of superheroes already fighting and villains already fighting, and then decide if either of the sides wins. This results in $O(N^2)$ solution, because for each of $N$ people joining the fight, in $O(N)$ time we are counting the number of superheroes and villains already fighting.

In the second subtask $N$ is at most $10^5$, so counting people from the scratch after every joining person will take too much time. However this can be easily avoided by keeping track of counters of superheroes and villains already fighting. After each person joining the fight, we increment exactly one of these counters depending on the type of the joining person and check if either group wins using these counters. This approach works in $O(N)$ time.

# AUTHOR'S AND TESTER'S SOLUTIONS:

Setter's solution can be found here.
Tester's solution can be found here.

This question is marked "community wiki".

74485097
accept rate: 12%

19.8k350498541

 0 What's the bug in my solution? #include #include int main(){ int t; long long i,n,h,v,size_of_names; char names[16]; scanf("%d",&t); while(t--){ size_of_names = 0; h = v = 1; scanf("%lld",&n); for(i=0; i
 0 Please move the problem to practice section answered 31 Dec '16, 22:41 497●1●8 accept rate: 15%
 0 Yeah please move the problem to practice section. answered 01 Jan '17, 08:29 2★aim_ioi 36●3 accept rate: 6%
 0 The question cannot be opened since last night. I've been waiting for the question to accept submissions but it shows "Problem is not visible now. Please try again later." answered 01 Jan '17, 09:42 1 accept rate: 0%
 0 Please move the problem to practice section answered 01 Jan '17, 12:46 31●1 accept rate: 0%

/dont use break; beacause user may enter more names; instead do this/

# include<string.h>

int main(){ int n,i,j,t,k; char a[15]; scanf("%d",&t); for(i=0;i<t;i++){ int="" s="0,v=0,flag=0;" scanf("%d",&n);="" for(j="0;j&lt;n;j++){" scanf("%s",a);="" k="strlen(a);" if(k="">=3&&a[k-3]=='m'&&a[k-2]=='a'&&a[k-1]=='n') s++; else v++;

    if(flag==0){
if((s-v)==2){
flag=1;
}
if((v-s)==3){
flag=2;
}
}
}
if(flag==1){
printf("superheroes\n");
}
if(flag==2){
printf("villains\n");
}
if(flag==0){
printf("draw\n");
}


}

return 0; }

4★ktcoder
212
accept rate: 0%

 0 Can anyone find what's wrong with my code : #include using namespace std; int main() { int T; cin>>T; while(T--){ int h = 1, v = 1; int n; int flag = 0; cin>>n; vector vs; while(n--){ string s; cin>>s; vs.push_back(s); } for(int i = 0; i < vs.size(); i++){ if(vs[i].find("man") != string::npos) h += 1; else v += 1; if(h - v == 2){ cout<<"superheroes"<

Where is the bug in my code?

# include<string>

using namespace std; int main() { int n, k, i, o, p, count, flag, counth, countv, w, countvv, counthh; string person, key; cin >> n; key = "woman"; while (n--) { cin >> k; counth = 0; countv = 0; countvv = 0; counthh = 0; for (i = 0; i < k; i++) { flag = 0;

        w = 4;
cin >> person;
for (o = 0; person[o] != '\0'; o++);
count = 0;
for (p = o - 1; p >= 0; p--)
{
if (person[p] == key[w])
{
count++;
}
w--;
if (w == -1)
{
break;
}
}
if (count == 3 || count == 5)
{
flag = 1;

}
if (flag == 1)
{
counth++;
}
else {
countv++;
}

if (counth - countv == 2 && countvv != 1)
{
if (counthh == 0)
{
counthh++;
}
}
if (countv - counth == 3 && counthh != 1)
{
if (countvv == 0)
{
countvv++;
}
}

}
if (counthh>0)
{
cout << "superheroes" << endl;
}

else if (countvv>0) {
cout << "villains" << endl;
}
else {
cout << "draw" << endl;
}

}

return 0;


}

3★abhi870
1
accept rate: 0%

 0 I'm getting a runtime error (NZEC), can't figure out why. Please point out what's wrong with my code. t = int(input()) for i in range(t): superheroes = 1 villains = 1 n = int(input()) flag = 0 for j in range(n): l = raw_input() a = len(l) b = l[a-3:a] if (b == "man"): superheroes += 1 else: villains += 1 if (superheroes - villains == 2): flag += 1 print ("superheroes") break elif (villains - superheroes == 3): flag += 1 print ("villains") break if (flag == 0): print ("draw")  Here's the link to my submission: https://www.codechef.com/viewsolution/12387811 answered 02 Jan '17, 10:27 1 accept rate: 0%
 0 Pleaese tell me where is the bug in my solution. I have tried a lot but I am getting wrong answer. Please help me. #include #include int strstr1(char *t) { int len=strlen(t); if(len>=3&&t[len-1]=='n'&&t[len-2]=='a'&&t[len-3]=='m') return 1; if(len>=5&&t[len-1]=='n'&&t[len-2]=='a'&&t[len-3]=='m'&&t[len-4]=='o'&&t[len-5]=='w') return 1; return 0; } int main() { int var,test,n; char t[100]; scanf("%d",&test); while(test--) { scanf("%d",&n); var=0; while(n--) { scanf("%s",t); if(strstr1(t)) { var++; } else { var--; } if(var==2) { printf("superheroes\n"); break; } else if(var==-3) { printf("villains\n"); break; } } if(var!=-3&&var!=2) { printf("draw\n"); } } return 0; } ` answered 02 Jan '17, 11:58 1 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,677
×1,652
×81
×29
×6

question asked: 31 Dec '16, 17:39

question was seen: 1,206 times

last updated: 02 Jan '17, 11:58