Tuesday, March 29, 2016

【筆記】【機器學習】Supervised learning 與 Unsupervised learning

課程連結

Supervised Learning (監督學習)

可以由訓練資料中學到或建立一個模式(函數 / learning model),並依此模式推測新的實例。---Wiki
所以簡單來說就是可以預測和得知確實的輸出而且需要有明確的輸入來告訴計算機要做甚麼 
例如從房價預測中可以預測到該平方米的房子需要多少錢 
從癌症診斷中可以判斷出是否有癌症 
再例如 
我現在要從一堆硬幣中找出十元 
亂畫 
所以這裡十元的特徵是:
  • 圓形
  • 所有圓形中最大
所以就可以知道最右邊的是十元了

Unsupervised Learning(無監督學習)

並不需要人力來輸入標籤—wiki
與監督學習不同的是無監督學習不需要明確的輸入 
再以這圖為例 
亂畫 
無監督學習可以幫我們將這三枚硬幣分類,然而它卻不能告訴我們這堆硬幣中那個是十元 
注意它不是一個分類系統 
它會將特徵相同的聚集在一起 
稱為「聚類」

【UVa】11804 - Argentina

Problem here

Problem

The Argentine football team coach, the great Diego Maradona, 
is going to try out a new formation this year. Formation describes 
how the players are positioned on the pitch. Instead of 
the conventional 4-4-2 or 4-3-3, he has opted for 5-5. This means 
there are 5 attackers and 5 defenders. 
You have been hired by Argentina Football Federation (AFF) 
to write a code that will help them figure out which players should 
take the attacking/defensive positions. 
Maradona has given you a list containing the names of the 
10 players who will take the field. The attacking ability and the 
defensive ability of each player are also given. Your job is to 
figure out which 5 players should take the attacking positions 
and which 5 should take the defensive positions. 
The rules that need to be followed to make the decision are: 
• The sum of the attacking abilities of the 5 attackers needs to be maximized 
• If there is more than one combination, maximize the sum of the defending abilities of the 5 
defenders 
• If there is still more than one combination, pick the attackers that come lexicographically earliest.

INPUT

The first line of input contains an integer T (T < 50) that indicates the number of test cases. Each case 
contains exactly 10 lines. The i-th line contains the name of the i-th player followed by the attacking 
and defending ability of that player respectively. The length of a players name is at most 20 and consists 
of lowercase letters only. The attacking/defending abilities are integers in the range [0, 99].

OUTPUT

The output of each case contains three lines. The first line is the case number starting from 1. The 
next line contains the name of the 5 attackers in the format ‘(A1, A2, A3, A4, A5)’ where Ai 
is 
the name of an attacker. The next line contains the name of the 5 defenders in the same format. The 
attackers and defenders names should be printed in lexicographically ascending order. Look at the 
sample for more details.

SAMPLE

input


sameezahur 20 21 
sohelh 18 9 
jaan 17 86 
sidky 16 36 
shamim 16 18 
shadowcoder 12 9 
muntasir 13 4 
brokenarrow 16 16 
emotionalblind 16 12 
tanaeem 20 97

output

Case 1: 
(emotionalblind, jaan, sameezahur, sohelh, tanaeem) 
(brokenarrow, muntasir, shadowcoder, shamim, sidky)

Solution

將攻擊力高的隊員排在前面,然後⋯⋯就沒然後了⋯⋯
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

struct teammeat{
    string name;
    int attack;
    int defend;

};

bool cmp(const teammeat &a, const teammeat &b){
    if(a.attack == b.attack){
        if(a.defend == b.defend){
            return a.name < b.name;
        }else{
            return a.defend < b.defend;
        }
    }else{
        return a.attack > b.attack;
    }
}

int main(){
    int t;
    while(cin >> t){
        int round = 0;
        while(t--){
            round++;
            vector<teammeat> teammeats;
            teammeat tmp;
            for(int i = 0; i < 10; i++){
                cin >> tmp.name >> tmp.attack >> tmp.defend;
                teammeats.push_back(tmp);
            }
            vector<string> att, def;
            sort(teammeats.begin(), teammeats.end(), cmp);
            for(int i = 0; i < 5; i++){
                att.push_back(teammeats[i].name);
            }
            for(int i = 5; i < 10; i++){
                def.push_back(teammeats[i].name);
            }

            sort(att.begin(), att.end());
            sort(def.begin(), def.end());
            cout << "Case " << round << ":" << endl;

            cout << "(";
            for(int i = 0; i < att.size()-1; i++){
                cout << att[i] << ", ";
            }
            cout << att[att.size()-1] << ")" << endl;

            cout << "(";
            for(int i = 0; i < def.size()-1; i++){
                cout << def[i] << ", ";
            }
            cout << def[def.size()-1] << ")" << endl;

        }
    }

    return 0;
}

Friday, March 25, 2016

【UVa】441 - Lotto

Problem

In the German Lotto you have to select 6 numbers from the set {1,2,…,49}.
A popular strategy to play Lotto - although it doesn’t increase your chance of winning - is to select a subset S containing k (k>6) of these 49 numbers, and then play several games with choosing numbers only from S.
For example, for k=8 and S = 1,2,3,5,8,13,21,34 there are 28 possible games: [1,2,3,5,8,13], [1,2,3,5,8,21], [1,2,3,5,8,34], [1,2,3,5,13,21], …, [3,5,8,13,21,34].
Your job is to write a program that reads in the number k and the set S and then prints all possible games choosing numbers only from S.
Input Specification
The input file will contain one or more test cases.
Each test case consists of one line containing several integers separated from each other by spaces. The first integer on the line will be the number k (6 < k < 13). Then k integers, specifying the set S, will follow in ascending order.
Input will be terminated by a value of zero (0) for k.
Output Specification
For each test case, print all possible games, each game on one line.
The numbers of each game have to be sorted in ascending order and separated from each other by exactly one space. The games themselves have to be sorted lexicographically, that means sorted by the lowest number first, then by the second lowest and so on, as demonstrated in the sample output below.
The test cases have to be separated from each other by exactly one blank line. Do not put a blank line after the last test case.

Sample Input

7 1 2 3 4 5 6 7 
8 1 2 3 5 8 13 21 34 
0

Sample Output

1 2 3 4 5 6 
1 2 3 4 5 7 
1 2 3 4 6 7 
1 2 3 5 6 7 
1 2 4 5 6 7 
1 3 4 5 6 7 
2 3 4 5 6 7
1 2 3 5 8 13 
1 2 3 5 8 21 
1 2 3 5 8 34 
1 2 3 5 13 21 
1 2 3 5 13 34 
1 2 3 5 21 34 
1 2 3 8 13 21 
1 2 3 8 13 34 
1 2 3 8 21 34 
1 2 3 13 21 34 
1 2 5 8 13 21 
1 2 5 8 13 34 
1 2 5 8 21 34 
1 2 5 13 21 34 
1 2 8 13 21 34 
1 3 5 8 13 21 
1 3 5 8 13 34 
1 3 5 8 21 34 
1 3 5 13 21 34 
1 3 8 13 21 34 
1 5 8 13 21 34 
2 3 5 8 13 21 
2 3 5 8 13 34 
2 3 5 8 21 34 
2 3 5 13 21 34 
2 3 8 13 21 34 
2 5 8 13 21 34 
3 5 8 13 21 34

Solution

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

int main(){

    int n;
    int cnt = 0;

    while(cin >> n){
        vector<int> nums;
        if(n == 0)
            break;

        for(int i = 0; i < n; i++){
            int tmp;
            cin >> tmp;
            nums.push_back(tmp);
        }
        sort(nums.begin(), nums.end());
        if(cnt > 0)
            cout << endl;
        cnt++;
        for(int i = 0; i < nums.size()-5; i++){
            for(int j = i+1; j < nums.size()-4; j++){
                for(int n = j+1; n < nums.size()-3; n++){
                    for(int m = n+1; m < nums.size()-2; m++){
                        for(int l = m+1; l < nums.size()-1; l++){
                            for(int k = l+1; k < nums.size(); k++){
                                cout << nums[i] << " " << nums[j] << " " << nums[n] << " " << nums[m] << " " << nums[l] << " " << nums[k] << endl;

                            }
                        }
                    }
                }
            }
        }
    }

    return 0;
}

Wednesday, March 23, 2016

【UVa】10360 - Rat Attack

Problem here

Problem

Baaaam! Another deadly gas bomb explodes in Manhattan’s underworld. Rats have taken over the
sewerage and the city council is doing everything to get the rat population under control.
As you know, Manhattan is organized in a regular fashion with streets and avenues arranged like
a rectangular grid. Waste water drains run beneath the streets in the same arrangement and the rats
have always set up their nests below street intersections. The only viable method to extinguish them
is to use gas bombs like the one which has just exploded. However, gas bombs are not only dangerous
for rats. The skyscrapers above the explosion point have to be evacuated in advance and so the point
of rat attack must be chosen very carefully.
The gas bombs used are built by a company called American Catastrophe Management (ACM) and
they are sold under the heading of “smart rat gas”. They are smart because —when fired— the gas
spreads in a rectangular fashion through the under street canals. The strength of a gas bomb is given
by a number d which specifies the rectangular “radius” of the gas diffusion area. For example, the
figure shows what happens when a bomb with d = 1 explodes.
The area of interest consists of a discrete grid of 1025 × 1025 fields. Rat exterminator scouts have
given a detailed report on where rat populations of different sizes have built their nests. You are given
a gas bomb with strength d and your task is to find an explosion location for this gas bomb which
extinguishes the largest number of rats.
The best position is determined by the following criteria:
• The sum of all rat population sizes within the diffusion area of the gas bomb (given by d) is
maximal.
• If there is more than one of these best positions then the location with the “minimal” position
will be chosen. Positions are ordered first by their x coordinate and second by their y coordinate.
Formally, given a location (x1, y1) on the grid, a point (x2, y2) is within the diffusion area of a gas
bomb with strength d if the following equation holds:
max(abs(x2 − x1), abs(y2 − y1)) ≤ d

Input

The first line contains the number of scenarios in the input.
For each scenario the first line contains the strength d of the gas bomb in the scenario (1 ≤ d ≤ 50).
The second line contains the number n (1 ≤ n ≤ 20000) of rat populations. Then for every rat
population follows a line containing three integers separated by spaces for the position (x, y) and “size”
i of the population (1 ≤ i ≤ 255). It is guaranteed that position coordinates are valid (i.e., in the range
between 0 and 1024) and no position is given more than once.

Output

For every problem print a line containing the x and y coordinate of the chosen location for the gas
bomb, followed by the sum of the rat population sizes which will be extinguished. The three numbers
must be separated by a space.

Sample

Input:
1
1
2
4 4 10
6 6 20
Output:
5 5 30

Solution

由老鼠開始枚舉
    #include <iostream>
    #include <stdlib.h>
    #include "memory.h"
    using namespace std;
    int bomb;
    int killed[1025][1025];

    int main(){
        int scen;
        cin >> scen;
        while(scen--){
            memset(killed, 0, sizeof(killed));
            cin >> bomb;
            int rats;
            cin >> rats;
            while(rats--){
                int x, y, popu;
                cin >> x >> y >> popu;
                for(int i=x-bomb ; i<=x+bomb ; i++)
                    for(int j=y-bomb ; j<=y+bomb ; j++)
                        if(i >= 0 && i <= 1024 && j >= 0 && j <= 1024) 
                            killed[i][j] += popu;
            }
            int tmp = 0, tmpX, tmpY;
            for(int i=0 ; i<=1024 ; i++){
                for(int j=0 ; j<=1024 ; j++) {
                    if(killed[i][j] > tmp) {
                        tmp = killed[i][j];
                        tmpX = i, tmpY = j;
                    }
                }
            }
            cout << tmpX << " " << tmpY << " " << tmp << endl;
        }

        return 0;
    }

Tuesday, March 1, 2016

【CodeForces】A. Dragons

Problem here

Problem

Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he’s got to defeat all n dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel’s outcome is determined by their strength. Initially, Kirito’s strength equals s.
If Kirito starts duelling with the i-th (1 ≤ i ≤ n) dragon and Kirito’s strength is not greater than the dragon’s strength xi, then Kirito loses the duel and dies. But if Kirito’s strength is greater than the dragon’s strength, then he defeats the dragon and gets a bonus strength increase by yi.
Kirito can fight the dragons in any order. Determine whether he can move on to the next level of the game, that is, defeat all dragons without a single loss.

Input

The first line contains two space-separated integers s and n (1 ≤ s ≤ 104, 1 ≤ n ≤ 103). Then n lines follow: the i-th line contains space-separated integers xi and yi (1 ≤ xi ≤ 104, 0 ≤ yi ≤ 104) — the i-th dragon’s strength and the bonus for defeating it.

Output

On a single line print “YES” (without the quotes), if Kirito can move on to the next level and print “NO” (without the quotes), if he can’t.

Examples

input

2 2 
1 99 
100 0

output

YES

input

10 1 
100 100

output

NO

Note

In the first sample Kirito’s strength initially equals 2. As the first dragon’s strength is less than 2, Kirito can fight it and defeat it. After that he gets the bonus and his strength increases to 2 + 99 = 101. Now he can defeat the second dragon and move on to the next level.
In the second sample Kirito’s strength is too small to defeat the only dragon and win.

Solution

桐人23333先排序再做比較
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct dragon{
    int level;
    int bonus;
};
bool cmp(dragon &a, dragon &b){
    return a.level < b.level;
}

int main(){
    int s, n;
    while(cin >> s >> n){
        int bs = s;
        bool next = true;
        vector<dragon> vd;
        dragon d;
        while(n--){
            int l, b;
            cin >> l >> b;
            d.level = l;
            d.bonus = b;
            vd.push_back(d);
        }
        sort(vd.begin(), vd.end(), cmp);
        for(int i = 0; i < vd.size(); i++){
            if(bs > vd[i].level){
                bs += vd[i].bonus;
            }else{
                next = false;
            }
        }
        if(next){
            cout << "YES" << endl;
        }else{
            cout << "NO" << endl;
        }
    }
    return 0;
}

【CodeForces】A. Football

Problem here

Problem

Petya loves football very much. One day, as he was watching a football match, he was writing the players’ current positions on a piece of paper. To simplify the situation he depicted it as a string consisting of zeroes and ones. A zero corresponds to players of one team; a one corresponds to players of another team. If there are at least 7 players of some team standing one after another, then the situation is considered dangerous. For example, the situation 00100110111111101 is dangerous and 11110111011101 is not. You are given the current situation. Determine whether it is dangerous or not.

Input

The first input line contains a non-empty string consisting of characters “0” and “1”, which represents players. The length of the string does not exceed 100 characters. There’s at least one player from each team present on the field.

Output

Print “YES” if the situation is dangerous. Otherwise, print “NO”.

Examples

input

001001

output

NO

input

1000000001

output

YES

Solution

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(){
    string input;
    while(cin >> input){
        int count = 0;
        char last = '/';
        for(int i = 0; i < input.length(); i++){
            if(last == '/'){
                last = input[i];
                count++;
                continue;
            } 
            if(input[i] == last){
                count++;
                if(count >= 7){
                    cout << "YES" << endl;
                    break;
                }
            }else if(input[i] != last){
                count = 0;
                last = input[i];
                count++;
            }
        }
        if(count < 7){
            cout << "NO" << endl;
        }
    }
    return 0;
}

【CodeForces】A. I Wanna Be the Guy

Problem here

Problem

There is a game called “I Wanna Be the Guy”, consisting of n levels. Little X and his friend Little Y are addicted to the game. Each of them wants to pass the whole game. 
Little X can pass only p levels of the game. And Little Y can pass only q levels of the game. You are given the indices of levels Little X can pass and the indices of levels Little Y can pass. Will Little X and Little Y pass the whole game, if they cooperate each other?

Input

The first line contains a single integer n (1 ≤  n ≤ 100).
The next line contains an integer p (0 ≤ p ≤ n) at first, then follows p distinct integers a1, a2, …, ap (1 ≤ ai ≤ n). These integers denote the indices of levels Little X can pass. The next line contains the levels Little Y can pass in the same format. It’s assumed that levels are numbered from 1 to n.

Output

If they can pass all the levels, print “I become the guy.”. If it’s impossible, print “Oh, my keyboard!” (without the quotes).

Example

input


3 1 2 3 
2 2 4

output

I become the guy.

input


3 1 2 3 
2 2 3

output

Oh, my keyboard!

Note

In the first sample, Little X can pass levels [1 2 3], and Little Y can pass level [2 4], so they can pass all the levels both.
In the second sample, no one can pass level 4.

Solution

開一個布爾數組
每次讀入都將狀態改成true
#include <iostream>
#include <vector>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<bool> level;
    for(int i = 0; i < n; i++){
        level.push_back(false);
    }
    int x;
    cin >> x;
    while(x--){
        int tmp;
        cin >> tmp;
        level[tmp-1] = true;
    }
    int y;
    cin >> y;
    while(y--){
        int tmp;
        cin >> tmp;
        level[tmp-1] = true;
    }
    bool succ = true;
    for(int i = 0; i < level.size(); i++){
        if(level[i] == false){
            succ = false;
        }
    }

    if(succ){
        cout << "I become the guy." << endl;
    }else{
        cout << "Oh, my keyboard!" << endl;
    }
    return 0;
}