2016微軟4月線上筆試題

weixin_34234823發表於2016-10-20

1288 : Font Size#

時間限制:10000ms 單點時限:1000ms 記憶體限制:256MB
描述
Steven loves reading book on his phone. The book he reads now consists of N paragraphs and the i-th paragraph contains ai characters. Steven wants to make the characters easier to read, so he decides to increase the font size of characters. But the size of Steven’s phone screen is limited. Its width is W and height is H. As a result, if the font size of characters is S then it can only show ⌊W / S⌋ characters in a line and ⌊H / S⌋ lines in a page. (⌊x⌋ is the largest integer no more than x) So here’s the question, if Steven wants to control the number of pages no more than P, what’s the maximum font size he can set? Note that paragraphs must start in a new line and there is no empty line between paragraphs.
輸入
Input may contain multiple test cases. The first line is an integer TASKS, representing the number of test cases. For each test case, the first line contains four integers N, P, W and H, as described above. The second line contains N integers a1, a2, … aN, indicating the number of characters in each paragraph.
For all test cases,

1<=N<=103,1<=W,H,ai<=103,1<=P<=106,There is always a way to control the number of pages no more than P.
輸出
For each testcase, output a line with an integer Ans, indicating the maximum font size Steven can set.
樣例輸入

2
1 10 4 3
10
2 10 4 3
10 10

樣例輸出

3
2

解題思路:
二分查詢解決判定性問題。注意邊界條件。

#include<iostream>
using namespace std;
int check(int mid, int N, int W, int H, int a[])
{
    int page = 0;
    int w = W / mid;
    int h = H / mid;
    int p_w,p_w_y,hang=0;
    for(int i=0;i<N;i++){
        p_w = a[i] / w;
        p_w_y = a[i] % w;
        hang = hang + p_w;
        if(p_w_y != 0)hang ++;
    }
    page = hang / h;
    if(hang % h != 0)page ++;
    return page;
}
int main()
{
    int mid = 0;
    int n,T,N,P,W,H,ans=0,cnt;
    int a[100];
    while(cin>>n){
        for(int T=1;T<=n;T++){
            cin>>N>>P>>W>>H;
            for(int i=0;i<N;i++)
            {
                cin >> a[i];
            }
            int left=1, right=min(W,H);
            while(left <= right){
                mid = (left + right) / 2;
                cnt = check(mid, N, W, H, a);
                if(cnt > P){
                    right = mid - 1;
                }else{
                    ans = mid;
                    left = mid + 1;
                }
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}

1289 : 403 Forbidden#

時間限制:10000ms 單點時限:1000ms 記憶體限制:256MB
描述
Little Hi runs a web server. Sometimes he has to deny access from a certain set of malicious IP addresses while his friends are still allow to access his server. To do this he writes N rules in the configuration file which look like:
allow 1.2.3.4/30
deny 1.1.1.1
allow 127.0.0.1
allow 123.234.12.23/3
deny 0.0.0.0/0

Each rule is in the form: allow | deny address or allow | deny address/mask.
When there comes a request, the rules are checked in sequence until the first match is found. If no rule is matched the request will be allowed. Rule and request are matched if the request address is the same as the rule address or they share the same first mask digits when both written as 32bit binary number.
For example IP “1.2.3.4” matches rule “allow 1.2.3.4” because the addresses are the same. And IP “128.127.8.125” matches rule “deny 128.127.4.100/20” because 10000000011111110000010001100100 (128.127.4.100 as binary number) shares the first 20 (mask) digits with 10000000011111110000100001111101 (128.127.8.125 as binary number).
Now comes M access requests. Given their IP addresses, your task is to find out which ones are allowed and which ones are denied.
輸入
Line 1: two integers N and M.
Line 2-N+1: one rule on each line.
Line N+2-N+M+1: one IP address on each line.
All addresses are IPv4 addresses(0.0.0.0 - 255.255.255.255). 0 <= mask <= 32.
For 40% of the data:
1<=N,M<=1000
.
For 100% of the data:
1<=N,M<=100000
.
輸出
For each request output “YES” or “NO” according to whether it is allowed.
樣例輸入

5 5
allow 1.2.3.4/30
deny 1.1.1.1
allow 127.0.0.1
allow 123.234.12.23/3
deny 0.0.0.0/0
1.2.3.4
1.2.3.5
1.1.1.1
100.100.100.100
219.142.53.100

樣例輸出

YES
YES
NO
YES
NO

解題思路:trie樹,要加快程式碼速度。

#include<iostream>
#include<vector>
#include<string>
#include<cstdlib>
#include<stdlib.h>
#include<sstream>
#include<limits.h>
using namespace std;

struct Tnode{
    Tnode* left;
    Tnode* right;
    int num;
    int flag;
};

vector<string> split(string s, char delimiter)
{
    vector<string> ret;
    int l = s.length();
    int b = 0,e = 0,i;
    for(i=0;i<l;i++)
    {
        if(s[i]==delimiter){
            e = i;
            ret.push_back(s.substr(b, e - b));
            b = i + 1;
        }
    }
    ret.push_back(s.substr(b, l));
    return ret;
}

string Tobinary(int data){
    string ret = "";
    string s;
    stringstream ss;
    for(int i=0;i<8;i++){
        int n = data % 2;
        ss << n;
        ss >> s;
        ret = s + ret;
        ss.clear();
        data /= 2;
    }
    return ret;
}

void createTree(string flag, string ip, int mask, int num, Tnode* root){
    vector<string> ipDelim = split(ip, '.');
    Tnode* node = root;
    Tnode* tmp;
    for(int i=0;i<ipDelim.size();i++){
        int data = atoi(ipDelim[i].c_str());
        string binaryS = Tobinary(data);
        cout << data<<" "<<binaryS << endl;
        for(int j = 0;j<8;j++){
            if (mask <= 0){
                if (flag == "allow") node->flag = 1;
                else node->flag = 0;
                node->num = num;
                free(tmp);
                //cout <<node->flag <<endl;
                return;
            }
            tmp = (Tnode*)malloc(sizeof(Tnode));
            tmp -> left = NULL;
            tmp -> right = NULL;
            tmp -> flag = -1;
            tmp -> num = -1;
            if(binaryS[j] == '0'){
                mask--;
                if(node->left == NULL)node->left = tmp;
                node = node->left;
            }else{
                mask--;
                if(node->right == NULL)node->right = tmp;
                node = node -> right;
            }
            //cout << binaryS[j]<<" "<<node->flag <<endl;
            free(tmp);
        }
        if (mask <= 0){
            if (flag == "allow") node->flag = 1;
            else node->flag = 0;
            node->num = num;
            cout <<node->flag <<endl;
        }
        //cout <<node->flag <<endl;
    }
    return;
}


int main()
{
    int n,m;
    string filter,ip;
    vector<string> ipDelim;
    Tnode* root = (Tnode*)malloc(sizeof(Tnode));
    root ->left = NULL;
    root ->right = NULL;
    root -> flag = 2;
    while(cin >>n>>m){
        for(int i=0;i<n;i++){
            cin>>filter>>ip;

            ipDelim = split(ip, '/');

            if (ipDelim.size() == 1){
                createTree(filter, ipDelim[0], 32, i, root);
            }else{
                int mask = atoi(ipDelim[1].c_str());
                createTree(filter, ipDelim[0], mask, i, root);
            }
            //cout << root->flag << endl;
        }

        for(int i=0;i<m;i++){
            cin>>ip;
            Tnode* node = root;
            ipDelim = split(ip, '.');
            int now_num = INT_MAX;
            bool ans = true;
            bool over = false;
            for(int j=0;j<ipDelim.size();j++){
                int data = atoi(ipDelim[j].c_str());
                string binaryS = Tobinary(data);
                for(int k=0;k<8;k++){
                    //cout << binaryS[k]<<" "<<node->flag <<endl;
                    if(node->flag != -1){
                        if(node->num < now_num){
                            now_num = node->num;
                            if(node->flag == 1)ans = true;
                            else ans = false;
                        }
                    }
                    if(binaryS[k] == '0' && node->left != NULL)node = node -> left;
                    else if(binaryS[k] == '1' && node->right != NULL)node = node->right;
                    else {
                            over = true;
                            break;
                    }
                }
                if(over)break;
            }
            if(ans)cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
    return 0;
}

1290 : Demo Day#

時間限制:10000ms 單點時限:1000ms 記憶體限制:256MB
描述
You work as an intern at a robotics startup. Today is your company’s demo day. During the demo your company’s robot will be put in a maze and without any information about the maze, it should be able to find a way out.
The maze consists of N * M grids. Each grid is either empty(represented by ‘.’) or blocked by an obstacle(represented by ‘b’). The robot will be release at the top left corner and the exit is at the bottom right corner.
Unfortunately some sensors on the robot go crazy just before the demo starts. As a result, the robot can only repeats two operations alternatively: keep moving to the right until it can’t and keep moving to the bottom until it can’t. At the beginning, the robot keeps moving to the right.
rrrrbb..
...r.... ====> The robot route with broken sensors is marked by 'r'.
...rrb..
...bb...

While the FTEs(full-time employees) are busy working on the sensors, you try to save the demo day by rearranging the maze in such a way that even with the broken sensors the robot can reach the exit successfully. You can change a grid from empty to blocked and vice versa. So as not to arouse suspision, you want to change as few grids as possible. What is the mininum number?
輸入
Line 1: N, M.
Line 2-N+1: the N * M maze.
For 20% of the data, N * M <= 16.
For 50% of the data, 1 <= N, M <= 8.
For 100% of the data, 1<= N, M <= 100.
輸出
The minimum number of grids to be changed.
解題思路:dp+spfa

#include<iostream>
#include<queue>
#include<limits.h>
using namespace std;

const int DOWN=1;
const int RIGHT=0;

struct State{
    int x,y;
    int cost;
    int dir;
    State(int _x,int _y, int _cost, int _dir):x(_x),y(_y),cost(_cost),dir(_dir){}
    State():x(0),y(0),cost(0),dir(RIGHT){};
    bool operator < (const State & arg) const
    {
        return this->cost > arg.cost;
    }
};


int main()
{
    int n,m;
    char graph[110][110];
    int vis[110][110][2];
    while(cin >>n>>m){
        priority_queue<State> q;
        //cout << n << " " << m << endl;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++){
                cin >> graph[i][j];
                vis[i][j][0]=INT_MAX;
                vis[i][j][1]=INT_MAX;
            }
            //cout << graph[i] << endl;
        }
        vis[0][0][1] = 0;
        vis[0][0][0] = 0;
        State s;
        s.x=0;
        s.y=0;
        s.cost=0;
        s.dir=RIGHT;

        q.push(s);
        while(!q.empty()){
            State now = q.top();
            int x = now.x;
            int y = now.y;
            q.pop();
            if(x == n - 1 && y == m - 1){
                cout << now.cost << endl;
                break;
            }
            State down;
            down.dir = DOWN;
            if(x + 1 < n){
                down.cost = now.cost;
                if(now.dir == RIGHT && graph[x][y + 1] == '.' && (y + 1) < m){
                    down.cost += 1;
                }

                if(graph[x+1][y] == 'b'){
                    down.cost += 1;
                }
                if(down.cost < vis[x + 1][y][DOWN])
                {
                    down.x = x + 1;
                    down.y = y;
                    vis[x + 1][y][DOWN] = down.cost;
                    q.push(down);
                }

            }
            State right;
            right.dir = RIGHT;
            if(y + 1 < m){
                right.cost = now.cost;
                if(now.dir == DOWN && graph[x + 1][y] == '.' && (x + 1) < n){
                    right.cost += 1;
                }

                if(graph[x][y+1]=='b'){
                    right.cost += 1;
                }
                if(right.cost < vis[x][y+1][RIGHT])
                {
                    right.x = x;
                    right.y = y+1;
                    vis[x][y+1][RIGHT] = right.cost;
                    q.push(right);
                }

            }

        }

    }
    return 0;
}

相關文章