Showing posts with label POJ. Show all posts
Showing posts with label POJ. Show all posts

Tuesday, February 28, 2017

【POJ】Symmetric Order

Problem here

Solution

#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<string> strs;
int n, cnt = 0;

void solve(int a, int b){
    if(b == strs.size() || a == strs.size())
        return;

    cout << strs[a] << endl;
    solve(a+2, b+2);
    if(strs[b] != " ")
        cout << strs[b] << endl;
}

int main(){
    while(cin >> n){
        strs.clear();
        if(n == 0)
            break;
        for(int i = 0; i < n; i++){
            string input;
            cin >> input;
            strs.push_back(input);
        }
        cout << "SET " << ++cnt << endl;
        if(n%2!=0)
            strs.push_back(" ");
        solve(0,1);

    }

    return 0;
}

Saturday, October 8, 2016

【POJ】Sumsets

Problem here

Description

Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7:
1) 1+1+1+1+1+1+1 
2) 1+1+1+1+1+2 
3) 1+1+1+2+2 
4) 1+1+1+4 
5) 1+2+2+2 
6) 1+2+4
Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000).

Input

A single line with a single integer, N.

Output

The number of ways to represent N as the indicated sum. Due to the potential huge size of this number, print only last 9 digits (in base 10 representation).

Sample Input

7

Sample Output

6

Source

USACO 2005 January Silver

Solution

當n為奇數時,dp[n] = dp[n-1] 
當n為偶數且含有1時,dp[n] = dp[n-1] 
當n為偶數且不含有1時,dp[n]=dp[n/2] 
所以當n為偶數時,dp[i] = (dp[i-1] + dp[i/2])%1000000000
#include <iostream>
#include <memory.h>
using namespace std;

int dp[1000001];

int main(){
    dp[0] = 0;
    dp[1] = 1;
    int n;
    while(cin >> n){
        for(int i = 2; i <= n; i++){
            if(i % 2 == 0)
                dp[i] = (dp[i-1] + dp[i/2])%1000000000;
            else
                dp[i] = dp[i-1];
        }
        cout << dp[n] << endl;
    }

    return 0;
}

Tuesday, September 20, 2016

【POJ】Cow Bowling

Problem here

Description

The cows don’t use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this:
      7



    3   8



  8   1   0



2   7   4   4
4 5 2 6 5 
Then the other cows traverse the triangle starting from its tip and moving “down” to one of the two diagonally adjacent cows until the “bottom” row is reached. The cow’s score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame.
Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.

Input

Line 1: A single integer, N
Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.

Output

Line 1: The largest sum achievable using the traversal rules
Sample Input


3 8 
8 1 0 
2 7 4 4 
4 5 2 6 5
Sample Output
30

Hint

Explanation of the sample:
      7

     *

    3   8

   *

  8   1   0

   *

2   7   4   4

   *
4 5 2 6 5 
The highest score is achievable by traversing the cows as shown above.

Solution

#include <iostream>
#include <memory.h>
using namespace std;

int dp[355][355], cost[355][355];

int main(){

    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j <= i; j++){
            cin >> cost[i][j];
        }
    }
    dp[0][0] = cost[0][0];
    for(int i = 1; i < n; i++){
        for(int j = 0; j <= i; j++){
            if(j - 1 < 0)
                dp[i][j] = dp[i-1][j] + cost[i][j];
            else
                dp[i][j] = max(dp[i-1][j], dp[i-1][j-1]) + cost[i][j];
        }
    }
    int answer = 0;
    for(int i = 0; i < n; i++){
        if(answer < dp[n-1][i])
            answer = dp[n-1][i];
    }
    cout << answer << endl;
    return 0;
}

Sunday, August 28, 2016

【POJ】Balance

Problem here

Description

Gigel has a strange “balance” and he wants to poise it. Actually, the device is different from any other ordinary balance. 
It orders two arms of negligible weight and each arm’s length is 15. Some hooks are attached to these arms and Gigel wants to hang up some weights from his collection of G weights (1 <= G <= 20) knowing that these weights have distinct values in the range 1..25. Gigel may droop any weight of any hook but he is forced to use all the weights. 
Finally, Gigel managed to balance the device using the experience he gained at the National Olympiad in Informatics. Now he would like to know in how many ways the device can be balanced.
Knowing the repartition of the hooks and the set of the weights write a program that calculates the number of possibilities to balance the device. 
It is guaranteed that will exist at least one solution for each test case at the evaluation.

Input

The input has the following structure: 
• the first line contains the number C (2 <= C <= 20) and the number G (2 <= G <= 20); 
• the next line contains C integer numbers (these numbers are also distinct and sorted in ascending order) in the range -15..15 representing the repartition of the hooks; each number represents the position relative to the center of the balance on the X axis (when no weights are attached the device is balanced and lined up to the X axis; the absolute value of the distances represents the distance between the hook and the balance center and the sign of the numbers determines the arm of the balance to which the hook is attached: ‘-’ for the left arm and ‘+’ for the right arm); 
• on the next line there are G natural, distinct and sorted in ascending order numbers in the range 1..25 representing the weights’ values.

Output

The output contains the number M representing the number of possibilities to poise the balance. 
Sample Input
2 4 
-2 3 
3 4 5 8 
Sample Output
2

Solution

力矩:力和力臂的乘积。
科科 
这里写图片描述
#include <iostream>
#include <memory.h>
#include <stdio.h>
using namespace std;

int cost[30], weight[30];
int dp[25][15001];
int n, m;

int main(){
    while(~scanf("%d %d", &n, &m)){
        memset(dp, 0, sizeof(dp));
        for(int i = 1; i <= n; i++)
            scanf("%d", &cost[i]);
        for(int i = 1; i <= m; i++)
            scanf("%d", &weight[i]);

        dp[0][7500] = 1;
        for(int i = 1; i <= m; i++)
            for(int j = 0; j <= 15000; j++)
                if(dp[i-1][j])
                    for(int k = 1; k <= n; k++)
                        dp[i][j+weight[i] * cost[k]] += dp[i-1][j];

        printf("%d", dp[m][7500]);
    }
    return 0;
}

Thursday, August 25, 2016

【POJ】Painter

Problem here

Description

The local toy store sells small fingerpainting kits with between three and twelve 50ml bottles of paint, each a different color. The paints are bright and fun to work with, and have the useful property that if you mix X ml each of any three different colors, you get X ml of gray. (The paints are thick and “airy”, almost like cake frosting, and when you mix them together the volume doesn’t increase, the paint just gets more dense.) None of the individual colors are gray; the only way to get gray is by mixing exactly three distinct colors, but it doesn’t matter which three. Your friend Emily is an elementary school teacher and every Friday she does a fingerpainting project with her class. Given the number of different colors needed, the amount of each color, and the amount of gray, your job is to calculate the number of kits needed for her class.

Input

The input consists of one or more test cases, followed by a line containing only zero that signals the end of the input. Each test case consists of a single line of five or more integers, which are separated by a space. The first integer N is the number of different colors (3 <= N <= 12). Following that are N different nonnegative integers, each at most 1,000, that specify the amount of each color needed. Last is a nonnegative integer G <= 1,000 that specifies the amount of gray needed. All quantities are in ml.

Output

For each test case, output the smallest number of fingerpainting kits sufficient to provide the required amounts of all the colors and gray. Note that all grays are considered equal, so in order to find the minimum number of kits for a test case you may need to make grays using different combinations of three distinct colors.

Sample Input

3 40 95 21 0 
7 25 60 400 250 0 60 0 500 
4 90 95 75 95 10 
4 90 95 75 95 11 
5 0 0 0 0 0 333 
0

Sample Output





4

SOLUTION

#include <iostream>
#include <memory.h>
#include <algorithm>
using namespace std;

int color[13];

int cmp(int &a, int &b){
    return a > b;
}

int main(){

    int n;
    while(cin >> n){
        if(n == 0)
            break;
        int count = 0;
        for(int i = 0; i < n; i++)
            cin >> color[i];

        int gray;
        cin >> gray;

        sort(color, color+n, cmp);
        if(color[0] % 50)
            count = color[0]/50+1;
        else
            count = color[0]/50;

        for(int i = 0; i < n; i++)
            color[i] = count * 50 - color[i];

            while(gray!=0){
                sort(color, color+n, cmp);

                if(color[2] <= 0){
                    count++;
                    for(int i = 0; i < n; i++)
                        color[i] += 50;
                }
                color[0] --;
                color[1] --;
                color[2] --;
                gray--;
            }
            cout << count << endl;
    }

    return 0;
}

Monday, August 22, 2016

【POJ】Power of Cryptography

Description

Current work in cryptography involves (among other things) large prime numbers and computing powers of numbers among these primes. Work in this area has resulted in the practical use of results from number theory and other branches of mathematics once considered to be only of theoretical interest.
This problem involves the efficient computation of integer roots of numbers.
Given an integer n>=1 and an integer p>= 1 you have to write a program that determines the n th positive root of p. In this problem, given such integers n and p, p will always be of the form k to the nth. power, for an integer k (this integer is what your program must find).

Input

The input consists of a sequence of integer pairs n and p with each integer on a line by itself. For all such pairs 1<=n<= 200, 1<=p<10101 and there exists an integer k, 1<=k<=109 such that kn = p.
Output
For each integer pair n and p the value k should be printed, i.e., the number k such that k n =p.

Sample Input

2 16
3 27
7 4357186184021382204544

Sample Output

4
3
1234
这里写图片描述

Solution

kn=p
p=k1n
#include <iostream>
#include <math.h>
using namespace std;

int main(){
    double n, p;
    while(cin >> n >> p){
        double k = pow(p, 1/n);
        cout << k << endl;
    }
    return 0;
}

Saturday, August 20, 2016

【POJ】Y2K Accounting Bug

Problem here

Description

Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc. 
All what they remember is that MS Inc. posted a surplus or a deficit each month of 1999 and each month when MS Inc. posted surplus, the amount of surplus was s and each month when MS Inc. posted deficit, the deficit was d. They do not remember which or how many months posted surplus or deficit. MS Inc., unlike other companies, posts their earnings for each consecutive 5 months during a year. ACM knows that each of these 8 postings reported a deficit but they do not know how much. The chief accountant is almost sure that MS Inc. was about to post surplus for the entire year of 1999. Almost but not quite.
Write a program, which decides whether MS Inc. suffered a deficit during 1999, or if a surplus for 1999 was possible, what is the maximum amount of surplus that they can post.

Input

Input is a sequence of lines, each containing two positive integers s and d.

Output

For each line of input, output one line containing either a single integer giving the amount of surplus for the entire year, or output Deficit if it is impossible.

Sample Input

59 237 
375 743 
200000 849694 
2500000 8000000 
Sample Output
116 
28 
300612 
Deficit

Solution

#include <iostream>
using namespace std;
/*
ssssd-> ssssd ssssd ss
sssdd-> sssdd sssdd ss
ssddd-> ssddd ssddd ss
sdddd-> sdddd sdddd sd
ddddd-> ddddd ddddd dd
*/
int main(){
    int s, d;
    while(cin >> s >> d){
        int ans = 0;
        if(4*s < d)
            ans = 10*s - 2*d;
        else if(3*s < 2*d)
            ans = 8*s - 4*d;
        else if(2*s < 3*d)
            ans = 6*s - 6*d;
        else if(s < 4*d)
            ans = 3*s - 9*d;
        else
            ans = -12*d;

        if(ans > 0)
            cout << ans << endl;
        else
            cout << "Deficit" << endl;
    }

    return 0;
}

【POJ】Heavy Transportation

Problem here

Description

Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight. 
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.
Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo’s place) to crossing n (the customer’s place). You may assume that there is at least one path. All streets can be travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

Sample Input


3 3 
1 2 3 
1 3 4 
2 3 5 
Sample Output
Scenario #1: 
4

Solution

#include <iostream>
#include <queue>
#include <memory.h>
#include <vector>
#include <utility>
#include <stdio.h>
#include <algorithm>
using namespace std;
int s, n, m;
bool visit[1001];
int dis[1001];
int map[1001][1001];
int dijkstra(){
    for(int i = 1; i <= n; i++){
        dis[i] = map[1][i];
    }
    memset(visit, false, sizeof(visit));
    visit[1] = true;
    for(int i = 1; i <= n-1; i++){
        int maxnum = 0;
        int u;
        for(int j = 1; j <= n; j++)
            if(visit[j] == false && dis[j] > maxnum){
                maxnum = dis[j];
                u = j;
            }

        visit[u] = true;

        for(int k = 1; k <= n; k++)
            if(visit[k] == false){
                int ans = min(maxnum, map[u][k]);
                if(ans > dis[k])
                    dis[k] = ans;
            }

    }
    return dis[n];
}

int main(){
    scanf("%d", &s);
    for(int i = 1; i <= s; i++){
        memset(map, 0, sizeof(map));
        scanf("%d %d", &n, &m);
        for(int j = 1; j <= m; j++){
            int a, b, c;
            scanf("%d %d %d", &a, &b, &c);
            map[a][b] = map[b][a] = c;
        }
        printf("Scenario #%d", i);
        cout << ":" << endl;
        cout << dijkstra() << endl << endl;

    }
    return 0;
}

Installing xAct (Mac)

Download from http : www.xact.es . Before unpack the tar/tgz file, remove attributes with xattr -c xAct_1.x.x.tgz then place the un...