Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&category=13&page=show_problem&problem=1135 

Bin packing, or the placement of objects of certain weights into different bins subject to certain constraints, is an historically interesting problem. Some bin packing problems are NP-complete but are amenable to dynamic programming solutions or to approximately optimal heuristic solutions. In this problem you will be solving a bin packing problem that deals with recycling glass. Recycling glass requires that the glass be separated by color into one of three categories: brown glass, green glass, and clear glass. In this problem you will be given three recycling bins, each containing a specified number of brown, green and clear bottles. In order to be recycled, the bottles will need to be moved so that each bin contains bottles of only one color. The problem is to minimize the number of bottles that are moved. You may assume that the only problem is to minimize the number of movements between boxes. For the purposes of this problem, each bin has infinite capacity and the only constraint is moving the bottles so that each bin contains bottles of a single color. The total number of bottles will never exceed 2 31 . Input The input consists of a series of lines with each line containing 9 integers. The first three integers on a line represent the number of brown, green, and clear bottles (respectively) in bin number 1, the second three represent the number of brown, green and clear bottles (respectively) in bin number 2, and the last three integers represent the number of brown, green, and clear bottles (respectively) in bin number 3. For example, the line 10 15 20 30 12 8 15 8 31 indicates that there are 20 clear bottles in bin 1, 12 green bottles in bin 2, and 15 brown bottles in bin 3. Integers on a line will be separated by one or more spaces. Your program should process all lines in the input file. Output For each line of input there will be one line of output indicating what color bottles go in what bin to minimize the number of bottle movements. You should also print the minimum number of bottle movements. The output should consist of a string of the three upper case characters ‘G’, ‘B’, ‘C’ (representing the colors green, brown, and clear) representing the color associated with each bin. The first character of the string represents the color associated with the first bin, the second character of the string represents the color associated with the second bin, and the third character represents the color associated with the third bin. The integer indicating the minimum number of bottle movements should follow the string. If more than one order of brown, green, and clear bins yields the minimum number of movements then the alphabetically first string representing a minimal configuration should be printed. Sample Input 1 2 3 4 5 6 7 8 9 5 10 5 20 10 5 10 20 10 Sample Output BCG 30 CBG 50: https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&category=13&page=show_problem&problem=1135 

Football the most popular sport in the world (americans insist to call it “Soccer”, but we will call it “Football”). As everyone knows, Brasil is the country that have most World Cup titles (four of them: 1958, 1962, 1970 and 1994). As our national tournament have many teams (and even regional tournaments have many teams also) it’s a very hard task to keep track of standings with so many teams and games played! So, your task is quite simple: write a program that receives the tournament name, team names and games played and outputs the tournament standings so far. A team wins a game if it scores more goals than its oponent. Obviously, a team loses a game if it scores less goals. When both teams score the same number of goals, we call it a tie. A team earns 3 points for each win, 1 point for each tie and 0 point for each loss. Teams are ranked according to these rules (in this order): 1. Most points earned. 2. Most wins. 3. Most goal difference (i.e. goals scored - goals against) 4. Most goals scored. 5. Less games played. 6. Lexicographic order. Input The first line of input will be an integer N in a line alone (0 < N < 1000). Then, will follow N tournament descriptions. Each one begins with the tournament name, on a single line. Tournament names can have any letter, digits, spaces etc. Tournament names will have length of at most 100. Then, in the next line, there will be a number T (1 < T ≤ 30), which stands for the number of teams participating on this tournament. Then will follow T lines, each one containing one team name. Team names may have any character that have ASCII code greater than or equal to 32 (space), except for ‘#’ and ‘@’ characters, which will never appear in team names. No team name will have more than 30 characters. Following to team names, there will be a non-negative integer G on a single line which stands for the number of games already played on this tournament. G will be no greater than 1000. Then, G lines will follow with the results of games played. They will follow this format: team name 1#goals1@goals2#team name 2 For instance, the following line: Team A#3@1#Team B Means that in a game between T eam A and T eam B, T eam A scored 3 goals and T eam B scored 1. All goals will be non-negative integers less than 20. You may assume that there will not be inexistent team names (i.e. all team names that appear on game results will have apperead on the team names section) and that no team will play against itself. Output For each tournament, you must output the tournament name in a single line. In the next T lines you must output the standings, according to the rules above. Notice that should the tie-breaker be the lexographic order, it must be done case insenstive. The output format for each line is shown bellow: [a]) T eam name [b]p, [c]g ([d]-[e]-[f]), [g]gd ([h]-[i]) Where: • [a] = team rank • [b] = total points earned • [c] = games played • [d] = wins • [e] = ties • [f] = losses • [g] = goal difference • [h] = goals scored • [i] = goals against There must be a single blank space between fields and a single blank line between output sets. See the sample output for examples. Sample Input 2 World Cup 1998 - Group A 4 Brazil Norway Morocco Scotland 6 Brazil#2@1#Scotland Norway#2@2#Morocco Scotland#1@1#Norway Brazil#3@0#Morocco Morocco#3@0#Scotland Brazil#1@2#Norway Some strange tournament 5 Team A Team B Team C Team D Team E 5 Team A#1@1#Team B Team A#2@2#Team C Team A#0@0#Team D Team E#2@1#Team C Team E#1@2#Team D Sample Output World Cup 1998 - Group A 1) Brazil 6p, 3g (2-0-1), 3gd (6-3) 2) Norway 5p, 3g (1-2-0), 1gd (5-4) 3) Morocco 4p, 3g (1-1-1), 0gd (5-5) 4) Scotland 1p, 3g (0-1-2), -4gd (2-6) Some strange tournament 1) Team D 4p, 2g (1-1-0), 1gd (2-1) 2) Team E 3p, 2g (1-0-1), 0gd (3-3) 3) Team A 3p, 3g (0-3-0), 0gd (3-3) 4) Team B 1p, 1g (0-1-0), 0gd (1-1) 5) Team C 1p, 2g (0-1-1), -1gd (3-4)

Code Examples

#1 Code Example with C Programming

Code - C Programming

 #include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
using namespace std;

struct team{
    string name;
    int gplayed, wins, ties, loses, gscored, gagainst, points, games, gd;
    team(string n){
        name = n;
        gplayed = wins = ties = loses = gscored = gagainst = points = games = gd = 0;
    }
};

int main() {
    int n;
    cin >> n;
    cin.ignore();
    while(n--){
        string tname;
        string pname, pname2;
        string in;
        getline(cin,tname);
        int tnum, gnum, a, b;
        unordered_map < string,team*> teamMap;
        cin >> tnum;
        cin.ignore();
        while(tnum--){
            getline(cin,pname);
            team* newT = new team(pname);
            teamMap[pname] = newT;
        }
        cin >> gnum;
        cin.ignore();
        while(gnum--){
            getline(cin,in);
            istringstream iss(in);
            getline(iss,pname,'#');
            getline(iss,in,'@');
            a = stoi(in);
            getline(iss,in,'#');
            b = stoi(in);
            getline(iss,pname2);
            teamMap[pname]->gscored += a;
            teamMap[pname]->gagainst += b;
            teamMap[pname2]->gscored += b;
            teamMap[pname2]->gagainst += a;
            if(a == b){
                teamMap[pname]->ties +=1;
                teamMap[pname2]->ties +=1;
             } else if(a > b){
                teamMap[pname]->wins++;
                teamMap[pname2]->loses++;
             } else {
                ++teamMap[pname]->loses;
                ++teamMap[pname2]->wins;
             }
        }
        vector < team*> res;
        for(auto& p : teamMap) {
            p.second->games = p.second->ties + p.second->wins + p.second->loses;
            p.second->points = p.second->wins*3 + p.second->ties;
            p.second->gd = p.second->gscored - p.second->gagainst;
            res.push_back(p.second);
        }
        sort(res.begin(), res.end(), [](team* a, team* b){
            if(a->points != b->points) return a->points > b->points;
            if(a->wins != b->wins) return a->wins > b->wins;
            if(a->gd != b->gd) return a->gd > b->gd;
            if(a->gscored != b->gscored) return a->gscored > b->gscored;
            if(a->games != b->games) return a->games  <  b->games;
            string a_name = a->name;
            string b_name = b->name;
            transform(a_name.begin(),a_name.end(),a_name.begin(),::tolower);
            transform(b_name.begin(),b_name.end(),b_name.begin(),::tolower);
            return a_name  <  b_name;
        });
        cout << tname << endl;
        int i=1;
        for(auto it=res.begin(); it!=res.end(); it++){
            cout<< i++ << ") " << (*it)->name;
            printf(" %dp, %dg (%d-%d-%d), %dgd (%d-%d)\n",
                   (*it)->points, (*it)->games, (*it)->wins, (*it)->ties, (*it)->loses,
                   (*it)->gd, (*it)->gscored, (*it)->gagainst
                   );
        }
        if(n) cout << endl;
    }
}.
Copy The Code & Try With Live Editor

Input

x
+
cmd
2 World Cup 1998 - Group A
4
Brazil
Norway
Morocco
Scotland
6
Brazil#2@1#Scotland
Norway#2@2#Morocco
Scotland#1@1#Norway
Brazil#3@0#Morocco
Morocco#3@0#Scotland
Brazil#1@2#Norway
Some strange tournament
5
Team A
Team B
Team C
Team D
Team E
5
Team A#1@1#Team B
Team A#2@2#Team C
Team A#0@0#Team D
Team E#2@1#Team C
Team E#1@2#Team D

Output

x
+
cmd
World Cup 1998 - Group A,
1) Brazil 6p, 3g (2-0-1), 3gd (6-3)
2) Norway 5p, 3g (1-2-0), 1gd (5-4)
3) Morocco 4p, 3g (1-1-1), 0gd (5-5)
4) Scotland 1p, 3g (0-1-2), -4gd (2-6)
Some strange tournament
1) Team D 4p, 2g (1-1-0), 1gd (2-1)
2) Team E 3p, 2g (1-0-1), 0gd (3-3)
3) Team A 3p, 3g (0-3-0), 0gd (3-3)
4) Team B 1p, 1g (0-1-0), 0gd (1-1)
5) Team C 1p, 2g (0-1-1), -1gd (3-4)
Advertisements

Demonstration


UVA Online Judge solution - 10194 - Football (aka Soccer) -  UVA Online Judge solution in C,C++,Java

Previous
UVA Online Judge solution - 10192 - Vacation - UVA Online Judge solution
Next
UVA Online Judge solution - 102 - Ecological Bin Packing - UVA Online Judge solution C,C++,Java