435 - Block Voting
Moderator: Board moderators
-
- New poster
- Posts: 50
- Joined: Wed Nov 06, 2002 1:37 pm
- Location: Planet Earth, Universe
- Contact:
Failed to understand - 435(Block Voting)
Can any one plz help me with block voting. I couldn't figure out, how in input1, party 1 had power index1, and in input 2, party 1 with power index 18, party 4,5,6 with power index 2?
For the first sample input, here are all the possible party combinations that would make a majority:
Now, for each line, consider each party. If the other parties don't make a majority vote, then you can increment the power index of the party by one.1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 5
1 2 4
1 2 4 5
1 2 5
1 3 4
1 3 4 5
1 3 5
1 4
1 4 5
1 5
2 3 4 5
2 4 5
3 4 5
I thought hard on this problem and couldn't come up with an intuitive way to solve it efficiently. I seriously doubt you should solve this problem by generating all possible combinations and going through them one by one, because there may be up to 20 parties which means 2^20 different combinations.
So any hint on this? Like can it be solved by dynamic programming? Or should I still generate all combinations but try to prune some of them? Thanks.
So any hint on this? Like can it be solved by dynamic programming? Or should I still generate all combinations but try to prune some of them? Thanks.
-
- Guru
- Posts: 1080
- Joined: Thu Dec 19, 2002 7:37 pm
You can use both approaches. My first solution, dating almost 3 years back, used exhaustive search with pruning and needed more than one second to solve the problem. I just made a DP solution and it solves the problem in one hundredth of that time.
The biggest problem with most problems is not how to solve the problem, but how to not solve what is not the problem.
-
- New poster
- Posts: 19
- Joined: Mon Jan 07, 2013 9:30 am
435 - Block Voting
I got a strange WA (expected only TLE or AC)
please give me a testcase to debug
this passes all example cases
P.S. memset is a trick to reset an array with a 1-line code (I don't understand why it is in cstring)
please give me a testcase to debug
Code: Select all
deleted after AC
P.S. memset is a trick to reset an array with a 1-line code (I don't understand why it is in cstring)
Last edited by theemathas on Thu Jan 10, 2013 10:16 am, edited 1 time in total.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 435 - Block Voting
the third argument to memset is the number of bytes. An int is 4 bytes. Instead write something like:
memset(mem,-1,sizeof(mem));
memset(counter,0,sizeof(counter));
memset(mem,-1,sizeof(mem));
memset(counter,0,sizeof(counter));
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 19
- Joined: Mon Jan 07, 2013 9:30 am
Re: 435 - Block Voting
seems like the bug is actually at the memset. I rewrote it without using memset and got TLE instead!
did it go into an infinite loop?
Code: Select all
deleted after AC
Last edited by theemathas on Thu Jan 10, 2013 10:16 am, edited 1 time in total.
-
- New poster
- Posts: 19
- Joined: Mon Jan 07, 2013 9:30 am
Re: 435 - Block Voting
yet another WA after reverting to memset
Code: Select all
deleted after AC
Last edited by theemathas on Thu Jan 10, 2013 10:16 am, edited 1 time in total.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 435 - Block Voting
print a blank line after the last testcase.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 5
- Joined: Tue Apr 22, 2014 6:28 pm
Re: 435 - Block Voting
Code: Select all
code removed
Last edited by gautamsingh on Thu Dec 04, 2014 4:10 pm, edited 1 time in total.
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 435 - Block Voting
Change line 41 to:
for (int p = 0; p < n; ++p) {
for (int p = 0; p < n; ++p) {
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 5
- Joined: Tue Apr 22, 2014 6:28 pm
Re: 435 - Block Voting
Thanks a lot brainfry713. 
