You are on page 1of 4

SPOJSOLUTIONS

Hereyouwillfindsolutionsofmanyproblemsonspoj.Ifyouwantsolutionofsomeproblemwhichisnotlistedinblogorhavedoubtregardinganyspojproblem(whichihavesolved)oranypro
youcanmailme@raj.nishant360@gmail.com
Andmyhumblerequesttoyouallthatdon'tcopythecodeonlytrytounderstandthelogicandalgorithmbehindthecode.Ihavestartedthisbecauseifyoutriedashardasyoucanandstill
thenyoucanrefertothis.
YoucanreadmyanswerhowtostartcompetitiveprogrammingCLICKHERE

Tuesday,March11,2014

LKSLargeKnapsack
LKSLargeKnapsack(linktothequestion)
canbeimplementedbyusing0/1knapsackwithlittlebitmodificationsinceineachiterationtwoconsecutive
rowarebeingusedoneisinreadmodeandanotherinwritemode.Sincethearraycouldexceedthelimitthatwhyididthismodification...
#include<stdio.h>
#definemax(x,y)(x>y)?x:y;
intarr[2][2000001]={0};
structnode
{
intvalue;
intweight;
}obj[501];

intmain()
{
inti,j,n,W,k;
scanf("%i%i",&W,&n);
i=0;
while(++i<=n)
scanf("%i%i",&obj[i].value,&obj[i].weight);
i=0;
while(++i<=n)
{
j=0;
if(i%2)
{
while(++j<=W)
{
if(obj[i].weight<=j)
arr[1][j]=max(obj[i].value+arr[0][jobj[i].weight],arr[0][j])
else
arr[1][j]=arr[0][j];
}
}
else
{
while(++j<=W)
{
if(obj[i].weight<=j)
arr[0][j]=max(obj[i].value+arr[1][jobj[i].weight],arr[1][j])
else
arr[0][j]=arr[1][j];
}
}

if(n&1)
printf("%i\n",arr[1][W]);
else
printf("%i\n",arr[0][W]);
return0;
}

PostedbyLackofProperNameat12:33AM
EmailThisBlogThis!SharetoTwitterSharetoFacebookSharetoPinterest

NewerPostOlderPostHome
Subscribeto:PostComments(Atom)

SearchThisBlog
Loading...

ALLPOST
2015(34)
October(9)
September(5)
August(5)
June(1)
February(6)
January(8)
2014(143)
December(6)
November(9)
October(15)
September(1)
August(9)
July(5)
June(10)
May(11)
April(7)
March(31)
SIRNUMSSIRCHIRAGANDMAGIC
NUMBERS
BLOPEROperators
RMIDRunningMedianRevisited
AMSCO1TheAMSCOcipher
AMSCO2DeciphertheAMSCOcipher
ICPC12CTheEncryptedPassword
FERT21_0Matches
RMIDRunningMedian
GSSQUNCESequence
IITWPC4DArrangementValidity
binaryindexedtreeBIT
JOKER1KnifesAreFun
QN01XORGame
IITKWPCOCreateCollections
LOOPEXPLoopExpectation
SPCJGopuandCreateCollectionsPartTwo
BOGGLEBoggleScoring
IITWPC4CMagguandVectors
AKVOD05RossgeneratesData
GOPI_SWGopiandSandwich
MSCHEDMilkScheduling
GSHOPRamaandFriends
BTCD14AGameofPips
LKSLargeKnapsack
HLP_RAMSTopperRamaRao
EMTY2CanYouMakeItEmpty
TRIGALGEEasyCalculation
SNGMSGEncodeMessage
SYNC13CWHATACOACCIDENT

PRISMSATRIANGULARPRISM
TipTopGameTIPTOP
February(30)
January(9)
2013(34)
December(34)

FacebookFollowers

Subscribe

Posts

Comments

pagehits

Therewasanerrorinthisgadget

FollowbyEmail
Emailaddress...

Submit

Google+Followers

Contributors
NishantRaj
NishantRaj

Labels
math(27)implementation(23)Graph(12)
BinarySearch(11)prime(11)Dynamic
Programming(10)BFS(8)BIT(7)Strings
(7)easy(7)primefactorization(7)DFS(6)
greedy(6)Hash(5)SegmentTree(5)Suffix
Array(4)AVLTree(2)ConvexHull(2)Game
Theory(2)HeightBalancedTree(2)Z
function(2)factorization(2)fibonacci(2)
matrixexponentiation(2)sorting(2)2DBIT
(1)BipartiteGraph(1)Editdistance(1)
GrundyNumbers(1)Heap(1)LCP(1)
LinkedListdatastructure(1)PolicyBased
DataStructure(1)PriorityQueue(1)Rolling
Hash(1)TriesDatastructure(1)contest(1)
floydwarshall(1)inversemodulo(1)
moderate(1)offlinequery(1)queue(1)
sieve(1)stack(1)stringmatching(1)
tetranacci(1)tree(1)twopointer(1)

PopularPosts

AGGRCOWAggressivecows
BelowgivencodeisforAGGRCOWspojAggressivecowsspoj.Hint:ThinkBinarysearch,#include<bits/stdc++.h>usingnamesp...
PPATHPrimePath

PrimePathBelowgivencodeisforPPATHspojorPrimepathspoj.Hint:UseBFSandsieve.#include<bits/stdc++.h>us...
PT07ZLongestpathinatree
LongestpathinatreeGivenbelowcodeisforPTZ07ZspojorLongestpathinatreespoj.YoucansolvethisusingDFSofapplyi...
Google+BadgePALINTheNextPalindrome
Itisasolutiontothe5thandoneofmostattractiveproblemtonovice,aswellasexperiencedhackeriftheyhaven'tdonewithit...
SegmenttreeGSS3&GSS1
Firstofallreadaboutsegmenttreeanditbasicfunctioning.YoucanreaditfromHERE.GivenbelowcodeisforGSS3&GSS1spoj...

Myotherblogs

RealThrills

NISHNATRAJ.PoweredbyBlogger.

You might also like