435 - Block Voting

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
sidky
New poster
Posts: 50
Joined: Wed Nov 06, 2002 1:37 pm
Location: Planet Earth, Universe
Contact:

Failed to understand - 435(Block Voting)

Post by sidky »

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?
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

For the first sample input, here are all the possible party combinations that would make a majority:
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
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.
stcheung
Experienced poster
Posts: 114
Joined: Mon Nov 18, 2002 6:48 am
Contact:

Post by stcheung »

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.
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post by little joey »

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.
theemathas
New poster
Posts: 19
Joined: Mon Jan 07, 2013 9:30 am

435 - Block Voting

Post by theemathas »

I got a strange WA (expected only TLE or AC)
please give me a testcase to debug

Code: Select all

deleted after AC
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)
Last edited by theemathas on Thu Jan 10, 2013 10:16 am, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 435 - Block Voting

Post by brianfry713 »

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));
Check input and AC output for thousands of problems on uDebug!
theemathas
New poster
Posts: 19
Joined: Mon Jan 07, 2013 9:30 am

Re: 435 - Block Voting

Post by theemathas »

seems like the bug is actually at the memset. I rewrote it without using memset and got TLE instead!

Code: Select all

deleted after AC
did it go into an infinite loop?
Last edited by theemathas on Thu Jan 10, 2013 10:16 am, edited 1 time in total.
theemathas
New poster
Posts: 19
Joined: Mon Jan 07, 2013 9:30 am

Re: 435 - Block Voting

Post by theemathas »

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.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 435 - Block Voting

Post by brianfry713 »

print a blank line after the last testcase.
Check input and AC output for thousands of problems on uDebug!
gautamsingh
New poster
Posts: 5
Joined: Tue Apr 22, 2014 6:28 pm

Re: 435 - Block Voting

Post by gautamsingh »

Code: Select all

code removed
I am getting WA. I have tried a lot of random test cases on udebug.com
Last edited by gautamsingh on Thu Dec 04, 2014 4:10 pm, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 435 - Block Voting

Post by brianfry713 »

Change line 41 to:
for (int p = 0; p < n; ++p) {
Check input and AC output for thousands of problems on uDebug!
gautamsingh
New poster
Posts: 5
Joined: Tue Apr 22, 2014 6:28 pm

Re: 435 - Block Voting

Post by gautamsingh »

Thanks a lot brainfry713. :D
Post Reply

Return to “Volume 4 (400-499)”