2017微软秋季校园招聘在线编程笔试

题目1 : Shortening Sequence

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

There is an integer array A1, A2 ...AN. Each round you may choose two adjacent integers. If their sum is an odd number, the two adjacent integers can be deleted.

Can you work out the minimum length of the final array after elaborate deletions?

输入

The first line contains one integer N, indicating the length of the initial array.

The second line contains N integers, indicating A1, A2 ...AN.

For 30% of the data:1 ≤ N ≤ 10

For 60% of the data:1 ≤ N ≤ 1000

For 100% of the data:1 ≤ N ≤ 1000000, 0 ≤ Ai ≤ 1000000000

输出

One line with an integer indicating the minimum length of the final array.

样例提示

(1,2) (3,4) (4,5) are deleted.

样例输入
7
1 1 2 3 4 4 5
样例输出
1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef long long  ll;
using namespace std;
#define N 1000005

stack<int>s;
int a[N];
int n;

int main()
{
    int i,j;
    while(cin>>n)
    {
       for(int i=0;i<n;i++)
         scanf("%d",&a[i]);
       while(!s.empty()) s.pop();
       for(int i=0;i<n;i++)
       {
           if(s.empty())
           {
               s.push(a[i]);
               continue;
           }
           int temp=s.top();
           if((temp+a[i])%2)

            {
                s.pop();
                continue;
            }
            s.push(a[i]);
       }
       cout<<s.size()<<endl;
    }
    return 0;
}




题目2 : Composition

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

Alice writes an English composition with a length of N characters. However, her teacher requires that M illegal pairs of characters cannot be adjacent, and if 'ab' cannot be adjacent, 'ba' cannot be adjacent either.

In order to meet the requirements, Alice needs to delete some characters.

Please work out the minimum number of characters that need to be deleted.

输入

The first line contains the length of the composition N.

The second line contains N characters, which make up the composition. Each character belongs to 'a'..'z'.

The third line contains the number of illegal pairs M.

Each of the next M lines contains two characters ch1 and ch2,which cannot be adjacent.  

For 20% of the data: 1 ≤ N ≤ 10

For 50% of the data: 1 ≤ N ≤ 1000  

For 100% of the data: 1 ≤ N ≤ 100000, M ≤ 200.

输出

One line with an integer indicating the minimum number of characters that need to be deleted.

样例提示

Delete 'a' and 'd'.

样例输入
5
abcde
3
ac
ab
de
样例输出
2
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef long long  ll;
using namespace std;
#define N 100005

int dp[N][27];
int a[27][27];
int n,m;
char c[N];
char dd[3];

void solve()
{
    memset(dp,0,sizeof(dp));
    for(int i=0;i<n;i++)
    {
       int temp=c[i]-'a';
       if(i>0) for(int j=0;j<26;j++) dp[i][j]=dp[i-1][j];
       dp[i][temp]=max(dp[i][temp],1);
       if(i==0) continue;
       for(int j=0;j<26;j++)
         if(!a[temp][j]&&!a[j][temp]) dp[i][temp]=max(dp[i][temp],dp[i-1][j]+1);
    }
    int ans=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<26;j++)
         if(dp[i][j]>ans) ans=dp[i][j];
    cout<<n-ans<<endl;
}

int main()
{
    int i,j;
    while(~scanf("%d",&n))
    {
        scanf("%s",c);
        scanf("%d",&m);
        memset(a,0,sizeof(a));
        while(m--)
        {
           scanf("%s",dd);
           a[dd[0]-'a'][dd[1]-'a']=1;
           a[dd[1]-'a'][dd[0]-'a']=1;
        }
        solve();
    }
    return 0;
}



题目3 : Registration Day

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

It's H University's Registration Day for new students. There are M offices in H University, numbered from 1 to M. Students need to visit some of them in a certain order to finish their registration procedures. The offices are in different places. So it takes K units of time to move from one office to another.

There is only one teacher in each office. It takes him/her some time to finish one student's procedure. For different students this time may vary. At the same time the teacher can only serve one student so some students may need to wait outside until the teacher is available. Students who arrived at the office earlier will be served earlier. If multiple students arrived at the same time they will be served in ascending order bystudent number.

N new students need to finish his/her registration. They are numbered from 1 to N. The ith student's student number is Si. He will be arrived at H University's gate at time Ti. He needs to visit Pi offices in sequence which are Oi,1, Oi,2, ... Oi,Pi. It takes him Wi,1, Wi,2, ... Wi,Pi units of time to finish the procedure in respective offices. It also takes him K units of time to move from the gate to the first office.

For each student can you tell when his registration will be finished?

输入

The first line contains 3 integers, N, M and K.  (1 <= N <= 10000, 1 <= M <= 100, 1 <= K <= 1000)

The following N lines each describe a student.

For each line the first three integers are Si, Ti and Pi. Then following Pi pairs of integers: Oi,1, Wi,1, Oi,2, Wi,2, ... Oi,Pi, Wi,Pi. (1 <= Si <= 2000000000, 1 <= Ti <= 10000, 1 <= Pi <= M, 1 <= Oi,j <= M, 1 <= Wi,j <= 1000)

输出

For each student output the time when he finished the registration.

样例提示

Student 1600012345 will be arrived at the gate at time 500. He needs to first visit Office #1 and then Office #2. He will be arrived at office #1 at time 600. He will leave office #1 at time 1100. Then He will arrive at office #2 at 1200. At the same time another student arrives at the same office too. His student number is smaller so he will be served first. He leaves Office #2 at 1700. End of registration.

Student 1600054321 will be arrived at the gate at time 1100. He will be arrived at Office #2 at 1200. Another student with smaller student number will be served first so he waits for his turn until 1700. He leaves Office #2 at 2000. End of registration.

样例输入
2 2 100  
1600012345 500 2 1 500 2 500  
1600054321 1100 1 2 300
样例输出
1700  
2000
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef long long  ll;
using namespace std;
#define N 10005
map<int,int>mp;
int n,m,k;
int student[N];
int arrive[N];
int num[N];
int last[N];
struct office{
   office(int p,int c):pos(p),cost(c){}
   int pos;
   int cost;
};
int ans[N];
vector<office>g[N];

struct A{
    A(int _id,int real,int _Ti,int _p):id(_id),Ti(_Ti),p(_p),real_id(real){}
    int id;
    int Ti;
    int p;
    int real_id;
    bool operator<(const A & b)const
    {
        if(Ti!=b.Ti) return Ti>b.Ti;
        return id>b.id;
    }
};

priority_queue<A>q;

void bfs()
{
   int i,j;
   while(!q.empty())q.pop();
   memset(ans,0,sizeof(ans));
   memset(last,0,sizeof(last));
   for(int i=0;i<n;i++)
      q.push(A(student[i],i,arrive[i]+k,0));
   A cur(1,1,1,1);
   while(!q.empty())
   {
       cur=q.top();
       q.pop();
       int pp=cur.real_id;//真正的id
       int vis_office=g[pp][cur.p].pos; //访问的office
       if(cur.Ti<last[vis_office])cur.Ti=last[vis_office];
       if(cur.p==g[pp].size()-1)
       {
           ans[pp]=cur.Ti+g[pp][cur.p].cost;
           last[vis_office]=ans[pp];
           continue;
       }
       last[vis_office]=cur.Ti+g[pp][cur.p].cost;
       cur.Ti=cur.Ti+k+g[pp][cur.p].cost;
       cur.p+=1;
       q.push(cur);
   }
}

int main()
{
    int i,j;
    int x,y;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        mp.clear();
        for(int i=0;i<=n;i++) g[i].clear();
        for(int i=0;i<n;i++)
        {
             scanf("%d%d",&student[i],&arrive[i]);
             scanf("%d",&num[i]);
             for(int j=0;j<num[i];j++)
             {
                  scanf("%d%d",&x,&y);
                  g[i].push_back(office(x,y));
             }
        }
        bfs();
        for(int i=0;i<n;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值