2016微软4月在线笔试题

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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,607评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,047评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,496评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,405评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,400评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,479评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,883评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,535评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,743评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,544评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,612评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,309评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,881评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,891评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,136评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,783评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,316评论 2 342

推荐阅读更多精彩内容