微软2016 9月笔试

全场梦游。。考研狗好久没码题了已经跪了T T

被自己蠢哭

题目1 : Farthest Point

时间限制:5000ms

单点时限:1000ms

内存限制:256MB

描述

Given a circle on a two-dimentional plane.

Output the integral point in or on the boundary of the circle which has the largest distance from the center.

输入

One line with three floats which are all accurate to three decimal places, indicating the coordinates of the center x, y and the radius r.

For 80% of the data: |x|,|y|<=1000, 1<=r<=1000

For 100% of the data: |x|,|y|<=100000, 1<=r<=100000

输出

One line with two integers separated by one space, indicating the answer.

If there are multiple answers, print the one with the largest x-coordinate.

If there are still multiple answers, print the one with the largest y-coordinate.

样例输入
1.000 1.000 5.000
样例输出
6 1

求离圆心最远的整点

思路:

暴力流,注意到X范围有限,枚举之。

接下来有两种方法

1.列方程解出Y,枚举附近点。

2.分上下半圆二分Y

注意精度。。。

#include<bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int INF = 0x3f3f3f3f;
int dcmp(double x){
    if(fabs(x) < eps)return 0;
    return x < 0 ? -1 : 1;
}
int main(){
    double x, y, r;
    int left, right;
    double ansdis = -1;
    int ansx, ansy;

    cin >> x>> y >>r;
    left = ceil(x - r);
    right = floor(x + r);
    for(int i = right; i >= left; i--){
        int nowx = i;
        int high = floor(y + r),low = ceil(y),mid;
        int nowy = -INF;
        while(low <= high){
            mid = (low + high) >> 1;
            if(dcmp((mid-y)*(mid-y)+(i-x)*(i-x) - r* r) <= 0){
                nowy = mid;
                low = mid + 1;
            }else high = mid - 1;
        }

        if(nowy != -INF){
            double nowdis = (nowx-x)*(nowx-x)+(nowy-y)*(nowy-y);
            if(dcmp(nowdis - ansdis) > 0){
                ansx = i;   ansy = nowy;
                ansdis = nowdis;
            }
        }
        nowy = -INF;
        high = floor(y),low = ceil(y-r);
        while(low <= high){
            mid = (low + high) >> 1;
            if(dcmp((mid-y)*(mid-y)+(i-x)*(i-x) - r* r) <= 0){
                nowy = mid;
                high = mid - 1;
            }else low = mid + 1;
        }
        if(nowy != -INF){
            double nowdis = (nowx-x)*(nowx-x)+(nowy-y)*(nowy-y);
            if(dcmp(nowdis - ansdis) > 0){
                ansx = i;   ansy = nowy;
                ansdis = nowdis;
            }
        }
    }
    cout << ansx << " " << ansy << endl;
    return 0;
}

题目2 : Total Highway Distance

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

Little Hi and Little Ho are playing a construction simulation game. They build N cities (numbered from 1 to N) in the game and connect them by N-1 highways. It is guaranteed that each pair of cities are connected by the highways directly or indirectly.

The game has a very important value called Total Highway Distance (THD) which is the total distances of all pairs of cities. Suppose there are 3 cities and 2 highways. The highway between City 1 and City 2 is 200 miles and the highway between City 2 and City 3 is 300 miles. So the THD is 1000(200 + 500 + 300) miles because the distances between City 1 and City 2, City 1 and City 3, City 2 and City 3 are 200 miles, 500 miles and 300 miles respectively.

During the game Little Hi and Little Ho may change the length of some highways. They want to know the latest THD. Can you help them?

输入

Line 1: two integers N and M.

Line 2 .. N: three integers u, v, k indicating there is a highway of k miles between city u and city v.

Line N+1 .. N+M: each line describes an operation, either changing the length of a highway or querying the current THD. It is in one of the following format.

EDIT i j k, indicating change the length of the highway between city i and city j to k miles.

QUERY, for querying the THD.

For 30% of the data: 2<=N<=100, 1<=M<=20

For 60% of the data: 2<=N<=2000, 1<=M<=20

For 100% of the data: 2<=N<=100,000, 1<=M<=50,000, 1 <= u, v <= N, 0 <= k <= 1000.

输出

For each QUERY operation output one line containing the corresponding THD.

样例输入
3 5
1 2 2
2 3 3
QUERY
EDIT 1 2 4
QUERY
EDIT 2 3 2
QUERY
样例输出
10
14
12

全场最水题?给一棵树,以及边权,定义THD为点两两之间的距离和,一边修改一边查询THD的值思路:另其有根,设深度较深的Y,浅的为X, 则X必为Y的父亲。一条边链接X, Y,则其影响Y子树与外部的距离,即cnt[y]*(n-cnt[y])的贡献。处理出子树规模。记录每个点的深度及通向其父亲的边是哪一条。没了

#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
typedef long long ll;
struct Edge{
    int x, y, w, next;
    Edge(){};
    Edge(int u, int v, int z, int dis){
        x = u, y = v, next = z, w = dis;
    }
}edge[N<<1];

int cntson[N], head[N], deep[N];
int faedge[N];

int no;
ll ans;
void init(){
    memset(head, -1, sizeof(head));
    memset(cntson, 0, sizeof(cntson));
    no = 0;
}

void add(int x, int y, int z){
    edge[no] = Edge(x, y , head[x], z);
    head[x] = no++;
    edge[no] = Edge(y, x, head[y], z);
    head[y] = no++;
}

void dfs(int x, int fa){
    int i, y;
    cntson[x] = 1;
    for(i = head[x]; i != -1; i = edge[i].next){
        y = edge[i].y;
        if(y == fa)continue;
        deep[y] = deep[x] + 1;
        dfs(y, x);
        cntson[x] += cntson[y];
        faedge[y] = i;
    }
}
int main(){
    int n, m, x, y, z, i;
    char op[10];
    init();

    scanf("%d%d", &n, &m);
    for(i = 1; i < n; i++){
        scanf("%d%d%d", &x, &y, &z);
        add(x, y, z);
    }
    deep[1] = 0;
    dfs(1, 1);
    for(i = 0; i < no; i+=2){
        x = edge[i].x;  y = edge[i].y;
        if(deep[x] > deep[y])   swap(x, y);
        ans += (ll)(cntson[y])*(n - cntson[y])*edge[i].w;
    }
    while(m--){
        scanf("%s", op);
        if(op[0] == ‘Q‘){
            printf("%lld\n", ans);
        }else{
            scanf("%d%d%d",&x, &y, &z);
            if(deep[x] > deep[y])   swap(x, y);
            i = faedge[y];
            ans += (ll)(z - edge[i].w) * (cntson[y])*(n - cntson[y]);
            edge[i].w = z;
        }
    }

    return 0;
}

题目3 : Fibonacci

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

Given a sequence {an}, how many non-empty sub-sequence of it is a prefix of fibonacci sequence.

A sub-sequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

The fibonacci sequence is defined as below:

F1 = 1, F2 = 1

Fn = Fn-1 + Fn-2, n>=3

输入

One line with an integer n.

Second line with n integers, indicating the sequence {an}.

For 30% of the data, n<=10.

For 60% of the data, n<=1000.

For 100% of the data, n<=1000000, 0<=ai<=100000.

输出

One line with an integer, indicating the answer modulo 1,000,000,007.

样例提示

The 7 sub-sequences are:

{a2}

{a3}

{a2, a3}

{a2, a3, a4}

{a2, a3, a5}

{a2, a3, a4, a6}

{a2, a3, a5, a6}

样例输入
6
2 1 1 2 2 3
样例输出
7

处理出所有FIB值。对于Ai,若其为FIB,则求出她是第几个(fibj),设cnt[j-1]为所有长度为j-1的符合题意的序列个数。则cnt[j] += cnt[j-1];特别处理一下x=1的时候。。

最后统计所有cnt就行了。注意取模= =

#include<bits/stdc++.h>
using namespace std;
const int N = 33;
const int MOD = 1000000007;

typedef long long ll;

int fib[N];
ll cnt[N];
bool vis[N];
int main(){
    int n, i,j, x;
    memset(fib, -1, sizeof(fib));
    memset(vis, 0, sizeof(vis));
    fib[1] = 1;
    fib[2] = 1;
    for(i = 3; fib[i-1] <= 100000; i++){
        fib[i] = fib[i-1] + fib[i-2];
        //printf("%d %d\n", i, fib[i]);
    }
    //fib[25]
    //1!!!!!!!!!!!!!!

    memset(cnt, 0, sizeof(cnt));
    ll ans = 0;
    scanf("%d", &n);
    for(i = 1; i <= n; i++){
        scanf("%d", &x);
        if(x == 1){
            cnt[2] =(cnt[1] + cnt[2]) % MOD;
            cnt[1] ++;
            if(cnt[2] >=1)  vis[2] = true;
            continue;
        }
        ll tmp = 1;
        for( j = 3; j <= 25 && fib[j] <= x; j++){
            if(x == fib[j]) break;
        }
        if(x == fib[j]) {
            if(vis[j-1]){
                cnt[j] = (cnt[j] + cnt[j-1]) % MOD;
                vis[j] = true;
            }
        }
       // cout << i << " " << ans<<endl;
    }
    for(i = 1; i <= 25; i++){
        ans = (ans + cnt[i]) % MOD;
    }
    cout << ans<<endl;
    return 0;
}



题目4 : Image Encryption

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

描述

A fancy square image encryption algorithm works as follow:

0. consider the image as an N x N matrix

1. choose an integer k∈ {0, 1, 2, 3}

2. rotate the square image k * 90 degree clockwise

3. if N is odd stop the encryption process

4. if N is even split the image into four equal sub-squares whose length is N / 2 and encrypt them recursively starting from step 0

Apparently different choices of the k serie result in different encrypted images. Given two images A and B, your task is to find out whether it is POSSIBLE that B is encrypted from A. B is possibly encrypted from A if there is a choice of k serie that encrypt A into B.

输入

Input may contains multiple testcases.

The first line of the input contains an integer T(1 <= T <= 10) which is the number of testcases.

The first line of each testcase is an integer N, the length of the side of the images A and B.

The following N lines each contain N integers, indicating the image A.

The next following N lines each contain N integers, indicating the image B.

For 20% of the data, 1 <= n <= 15

For 100% of the data, 1 <= n <= 100, 0 <= Aij, Bij <= 100000000

输出

For each testcase output Yes or No according to whether it is possible that B is encrypted from A.

样例输入
3
2
1 2
3 4
3 1
4 2
2
1 2
4 3
3 1
4 2
4
4 1 2 3
1 2 3 4
2 3 4 1
3 4 1 2
3 4 4 1
2 3 1 2
1 4 4 3
2 1 3 2
样例输出
Yes
No
Yes

赛时犯了一个极其脑残的错误导致样例都过不去。。。泪目。。。。。。。。。。T T暴力了一个写法,不知道能不能过。等题库放题了再改吧

#include <bits/stdc++.h>
using namespace std;

const int N = 104;
int gox[4] = {1, 1, -1, -1};
int goy[4] = {1, -1, -1, 1};
struct Matrix{
    int f[N][N];
    int mat_size;

    void scan(){
        int i, j;
        for(i = 1; i <= mat_size; i++)
            for(j = 1; j <= mat_size; j++)
                scanf("%d", &f[i][j]);
    }

    void print(){
        int i, j;
        for(i = 1; i <= mat_size; i++){
            for(j = 1; j <= mat_size; j++)
                printf("%d ", &f[i][j]);
            printf("\n");
        }

    }
    void modify(int t, int ra, int rb, int ca, int cb){
        Matrix C;
        int i, j, ii, jj;
        if(t & 1){
            if(t == 1){
                for(j = cb, ii = ra; j >= ca; j--, ii++)
                    for(i = ra, jj = ca; i <= rb; i++, jj++)
                        C.f[ii][jj] = f[i][j];
            }else{
                for(j = ca, ii = ra; j <= cb; j++, ii++)
                    for(i = rb, jj = ca; i >= ra; i--, jj++)
                        C.f[ii][jj] = f[i][j];
            }
        }
        else{
            for(i = rb, ii = ra; i >= ra; i--, ii++)
                for(j = cb, jj = ca; j >= ca; j--, jj++)
                    C.f[ii][jj] = f[i][j];
        }
        for(i = ra; i <= rb; i++)
            for(j = ca; j <= cb; j++)
                f[i][j] = C.f[i][j];
    }

    bool check(const Matrix &I, int ra, int rb, int ca, int cb)const{
        int i, j;
        for(i = ra; i <= rb; i++)
            for(j = ca; j <= cb; j++)
                if(f[i][j] != I.f[i][j])    return false;
        return true;
    }
}A, B;

bool judge(Matrix &A, Matrix &B, int ra, int rb, int ca, int cb){
    int i;
    Matrix Tmp = A;
    for(i = 0; i < 4; i++){
        if(i != 0)
            A.modify(i, ra, rb, ca, cb);
        if(((ra - rb + 1) &1 )){
            if(B.check(A, ra, rb, ca, cb)) return true;
        }else{
            if(B.check(A, ra, rb, ca, cb))return true;
            if(judge(A, B, ra, (ra + rb) / 2, ca, (ca+cb)/2) &&
               judge(A, B, (ra + rb) / 2+1, rb, (ca + cb) / 2 + 1, cb)&&
               judge(A, B, ra, (ra + rb) / 2, (ca + cb) / 2 + 1, cb) &&
               judge(A, B, (ra + rb) / 2+1, rb, ca, (ca+cb)/2))
                return true;
        }
        A = Tmp;
    }
    return false;
}
int main(){
    int n, TC, i, j;
    scanf("%d", &TC);
    while(TC--){
        scanf("%d", &n);
        A.mat_size = B.mat_size = n;
        A.scan();
        B.scan();
        printf("%s\n", judge(A, B, 1, n, 1, n)?"Yes" : "No");
      //  A = A.modify(1, 1,n, 1, n);

       // A.print();
    }
    return 0;
}

时间: 2024-10-11 19:38:36

微软2016 9月笔试的相关文章

微软2016校招笔试 第二场部分题目个人思路

A. Lucky Substrings 这道题并不难,由于字符串长度只有100,那么它的子串肯定不超过1w个,枚举出所有字串都是没有问题的,至于检验一个子串里面不同的字母数量是不是斐波那契数,我们只需要事先把斐波那契数列小于1w的项都生成出来,然后枚举一个子串之后,统计出不同字母的数量(边找边统计,如果当前字母之前出现过就不加,如果没出现过就记住并+1),去这个里面找就行了.斐波那契数列推不了几项就到1w了-- 最后要字典序从小到大排列,并且还要去重,那就用string然后直接sort+uniq

微软2016校招笔试 第一场部分题目个人思路

嗯--第一场当时还不知道报名,第二场报上了,拿了250/400,果然分如其人-- 这里包括的是第一场的ABC题,第二场的AB题的题解在另一篇(这些都是自己AC了的),第一场的D和第二场的C至今仍然在WA,不知道怎么回事,到时候也讲讲思路求指点吧. A. Magic Box 这道题并不难做,唯一需要注意的是cx,cy和cz,以及任意两个量的差之间是没有前后关系的(这个事情样例里也告诉你了),所以比较简单的方法就是先把cx,cy,cz排序,然后根据输入串一个一个模拟,然后计算出两两之间的差,排序,比

微软发布6月漏洞补丁 安全狗建议及时修复

北京时间6月10日凌晨,微软发布了2015年6月安全公告https://technet.microsoft.com/library/security/ms15-jun,本次更新共含8个补丁,其中2个级别为"严重",其余6个级别为"重要",主要修复了 Windows 系统.Office及IE浏览器等组件中的漏洞.服务器安全狗已经第一时间推送了安全更新,请及时修复安全狗提示给您的系统高危漏洞,避免漏洞被利用遭到攻击. 2015年6月微软安全公告简要如下: MS15-05

微软3个月没能修复零日漏洞被谷歌公布给黑客,发布攻击代码教人们怎么攻击

windows可能漏洞太多了,根本来不及修补漏洞,否则何至于微软三个月还修复不了.一个漏洞3个月没修复,修复100个漏洞要25年? 其实开源系统也有漏洞比如bash,ssh心跳漏洞不是也公布了吗,而且没听说linus发出怨言. 谷歌在本月早些时候公布了"概念验证代码"(proof-of-concept code),为恶意黑客提供了可利用"零日漏洞"攻破微软Windows 8.1操作系统的蓝图,此举引发了很大争议.而在本周,谷歌再次公布了该操作系统的一个漏洞. 谷歌最

2016 10月15日java的动手动脑

(1) 编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数. 源程序: //随机数的产生 //zhanxinwu,October,15,2016 public class Recur { private static final int N = 10; private static final int LEFT = 40; private static final int RIGHT = 100; private static long x0 = 1L; private long

lazarus 2016 2月18 4:22:35 支持android开发了, 既ios,linux,macosx,window,web 后 囊括一切啦。 哈哈

Android Development Lazarus for Linux Lazarus for Mac OS X Lazarus for iOS Lazarus for Windows Lazarus for Web Lazarus 1.6 - Released - February 18, 2016, 04:22:35 pm

2016搜狐笔试二叉树和最大的子树

问题描述: 给一个二叉树,每个节点都是正或负整数,如何找到一个子树,它所有节点的和最大? 思路:采用自底向上的计算.先计算左右子树总和值,用左右子树的总和加上当前节点值,如果当前总和大于最大值,则更新最大值,同时将最大子树根节点更新为当前根.简单说,就是后序遍历. 代码: [cpp] view plaincopy #include <iostream> #include <limits> using namespace std; struct Node { long data; N

2016.6月

So i'll dream until i make it real and all i see is stars.it's not until you fall that you fly. When your derams come alive you're unstoppable. take a shot chase the sun find the beautiful. We will glow in the dark turning dust to gold. And we'll dre

【hihocoder】1237 : Farthest Point 微软2016校招在线笔试题

题目:给定一个圆,要你求出一个在里面或者在边上的整数点,使得这个点到原点的距离最大,如果有多个相同,输出x最大,再输出y最大. 思路:对于一个圆,里面整点个数的x是能确定的.你找到x的上下界就可以了.就是mix = ceil(x0-r)//因为是小的值,所以要向上取整.mxx=floor(x0+r)//大的值要向下取整 对于y.我们也能用欧股定理确定.y也是有一个范围.但是并不是所有y都要枚举的.明显.y的值是离圆心越远越好.所以对于每一个x而言只需要枚举最远的两个y值 #include <cs