2016微軟4月在線筆試題

1288 : Font Size#

時間限制:10000ms 單點時限:1000ms 內(nèi)存限制: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 內(nèi)存限制: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 內(nèi)存限制: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;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末肿轨,一起剝皮案震驚了整個濱河市茧球,隨后出現(xiàn)的幾起案子便监,更是在濱河造成了極大的恐慌晶府,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件钻趋,死亡現(xiàn)場離奇詭異川陆,居然都是意外死亡,警方通過查閱死者的電腦和手機蛮位,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門较沪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人失仁,你說我怎么就攤上這事尸曼。” “怎么了萄焦?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵控轿,是天一觀的道長。 經(jīng)常有香客問我拂封,道長茬射,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任冒签,我火速辦了婚禮在抛,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘萧恕。我一直安慰自己刚梭,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布票唆。 她就那樣靜靜地躺著朴读,像睡著了一般。 火紅的嫁衣襯著肌膚如雪走趋。 梳的紋絲不亂的頭發(fā)上磨德,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天,我揣著相機與錄音,去河邊找鬼典挑。 笑死酥宴,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的您觉。 我是一名探鬼主播拙寡,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼琳水!你這毒婦竟也來了肆糕?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤在孝,失蹤者是張志新(化名)和其女友劉穎诚啃,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體私沮,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡始赎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了仔燕。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片造垛。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖晰搀,靈堂內(nèi)的尸體忽然破棺而出五辽,到底是詐尸還是另有隱情,我是刑警寧澤外恕,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布杆逗,位于F島的核電站,受9級特大地震影響鳞疲,放射性物質(zhì)發(fā)生泄漏髓迎。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一建丧、第九天 我趴在偏房一處隱蔽的房頂上張望排龄。 院中可真熱鬧,春花似錦翎朱、人聲如沸橄维。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽争舞。三九已至,卻和暖如春澈灼,著一層夾襖步出監(jiān)牢的瞬間竞川,已是汗流浹背店溢。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留委乌,地道東北人床牧。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像遭贸,于是被迫代替她去往敵國和親戈咳。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,577評論 2 353

推薦閱讀更多精彩內(nèi)容