Pick up Zoj a week to solve problems

Source: Internet
Author: User

ZOJ 2734 Exchange Cards Main topic:

Given a value of N, and a pile of cards, each card has a value of values and a number of numbers. Use any card to form N.

For example n = ten, Cards (10,2) (7,2) (5,3) (2,2) (1,5), then 10 = 10,10 =7+3+1,10=5+5 ...

Thinking Analysis:

Since done before 1204, know that the problem is naked search, directly with the DFS violence can be obtained.

Can do optimization processing is-this process is greedy greedy, first from the big to small to take over, so, you can do a step descending sort of preprocessing.

As in the above example: I chose 7, then I would not choose 10, because plus 10 is too big, beyond the N.

Summary of Knowledge points:

1) DFS walk: Go forward along the feasible solution space;

DFS (index, number, sum, target);

2) Cycle Away method: Select the next starting point enumeration;

DFS (++index, 1, sum, target);

Code:
#include <iostream>#include<vector>#include<map>#include<algorithm>#include<fstream>using namespacestd;structcards{intnum; intvalue; BOOL operator< (Constcards& card)Const    {        returnValue >Card.value; }};intTotalcardscount;vector<Cards>inputvalues;voidDfsintIndexintNumberintSuminttarget) {    if(sum = = target)//Summary of Records{Totalcardscount++; return; }    if(Index = = inputvalues.size ())return;//length//can add, deep ing ...    if(sum + inputvalues[index].value <= target && number <=inputvalues[index].num) {Sum+=Inputvalues[index].value; number++;        DFS (index, number, sum, target); number--; Sum-=Inputvalues[index].value; } DFS (++index,1, sum, target);}intMain () {//ifstream cin ("2734.txt");    intT, I, Target; intc =0; intN; BOOLFirst =true;  while(Cin >>target) {        if(!First ) {cout<<Endl; } First=false; C=1; CIN>>N;        Inputvalues.clear (); inttmp;  for(i =1; I <= N; i++) {Cards cd; CIN>>Cd.value; CIN>>Cd.num;        Inputvalues.push_back (CD);        } std::sort (Inputvalues.begin (), Inputvalues.end ()); Totalcardscount=0; DFS (0,1,0, Target); cout<< Totalcardscount <<Endl; }    return 0;}
ZOJ 1947 the Settlers of Catan Main topic:

An--the graph is given to find the longest path of the non-graph without permission longest path. That is, find a path in the graph where the edges are not repeated (points can be repeated), and the number of edges of the resulting path is the desired one.

such as: 3 points, two edges, (0,1), the longest path is 2.

The maximum size of the graph is 25*25.

Thinking Analysis:

See this topic, try to go online search for the longest path of knowledge points, but always find no useful. Finally, I put the starting point on the concept of the Euler loop, and then I understand the Hamiltonian loop, and come to a conclusion that the longest path is more extensive than the Euler circuit.

Accidentally browsing to Wikipedia, knowing that this is a NP problem, its algorithm can only be a brute force enumeration. Better understand that if it is the longest path to the graph, then the topological sort preprocessing should be done first.

As a result, DFS searches are performed directly on each point, saving the longest path at a time.

Summary of Knowledge points:

1) The Euler circuit: a loop that passes all the edges once and all the vertices one at a time.

2) Hamiltonian circuit: a loop that passes through all vertices once in the diagram.

Code:
#include <stdio.h>#include<iostream>#include<vector>#include<map>#include<Set>#include<algorithm>#include<cstring>#include<fstream>#include<list>using namespacestd;intgraph[ -][ -];BOOLvisited[ -][ -];intThelongestpath;voidDfsintRootintd) {    if(D >Thelongestpath) {Thelongestpath=D; }     for(inti =0; I < -; i++)    {        if(Root = = i)Continue; if(Graph[root][i] = =1&& Visited[root][i] = =0) {Visited[root][i]= Visited[i][root] =1; ++D;            DFS (i, d); --D; Visited[root][i]= Visited[i][root] =0; }    }}intmain1947 () {//fstream cin ("1947.txt");    intN, M;  while(Cin >> N >>m) {if(n = =0&& m = =0) Break; memset (graph,0,sizeof(graph)); intstart, end;  for(inti =0; I < m; i++) {cin>> Start >>end; Graph[start][end]= Graph[end][start] =1; } Thelongestpath=0;  for(inti =0; I < n; i++) {memset (visited,0,sizeof(visited)); DFS (i,0); } cout<< Thelongestpath <<Endl; }    return 0;}

ZOJ 1978 Assistant Required Main topic:

Given a queue, 2, 3, ... N, each time take the first element of the team as a lucky element, and then the subsequent every I to drag out to work. For example, the first time 2 is lucky, 4,6,8 ... The second time 3 is lucky, 9,15,21 ... Going to work ...

Ask for the K lucky number.

Thinking Analysis:

From 2 wrote the sequence analysis of 20, found that the prime number is similar, and even made a prime table.

But when testing the 20th lucky number, the Sample out of the given is 83, I hit 71 (or 73, specifically forget), found wrong, and then analysis, found and the prime table a little difference.

Prime table: A multiple of I is removed each time;

Lucky numbers (for the moment) table: Every time I remove the number.

Modify it, you can.

Summary of Knowledge points:

1) prime list;

Code:

#include <iostream>#include<vector>#include<map>#include<string>#include<algorithm>#include<fstream>using namespacestd;Const intMax_prime =3001;Const intSearch_int =34000;intPrime_like[max_prime];BOOL  is[Search_int];voidmake_prim_table () { for(inti =0; i < max_prime; i++)    {         is[I] =0; }    intnum =1;  for(inti =2; i < Search_int; i++)    {        if( is[I] = =1)Continue; if( is[I] = =0) {Prime_like[num++] =i; if(num = = max_prime) Break; }        if(i = =2)        {             for(intK = i; K < Search_int; K + =i) { is[K] =1; }        }        Else        {            intNumber =0;  for(intK = i +1; K < Search_int; k++)            {                if( is[K] = =0)//already out of the team                {                    if(++number = =i) { is[K] =1; number=0; }                }            }        }    }}intmain1978 () {make_prim_table (); //fstream cin ("1978.txt");    intN;  while(true) {cin>>N; if(n = =0) Break; cout<< Prime_like[n] <<Endl; }    return 0;}

Acceptted is the best thing for you ~

Pick up Zoj a week to solve problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.