BestCoder Round #3

    后面两题感觉看起来很简单、、(

    链接:http://acm.hdu.edu.cn/search.php?field=problem&key=BestCoder+Round+%233&source=1&searchmode=source

Task schedule



Problem Description
有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务。
有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之外的任务请求,请计算何时这个任务才能被执行。
机器总是按照工作表执行,当机器空闲时立即执行工作表之外的任务请求。
 

Input
输入的第一行包含一个整数T, 表示一共有T组测试数据。

对于每组测试数据:
第一行是两个数字n, m,表示工作表里面有n个任务, 有m个询问;
第二行是n个不同的数字t1, t2, t3....tn,表示机器在ti时间执行第i个任务。
接下来m行,每一行有一个数字q,表示在q时间有一个工作表之外的任务请求。

特别提醒:m个询问之间是无关的。

[Technical Specification]
1. T <= 50
2. 1 <= n, m <= 10^5
3. 1 <= ti <= 2*10^5, 1 <= i <= n
4. 1 <= q <= 2*10^5
 

Output
对于每一个询问,请计算并输出该任务何时才能被执行,每个询问输出一行。
 

Sample Input
  
  
1 5 5 1 2 3 5 6 1 2 3 4 5
 

Sample Output
  
  
4 4 4 4 7
 

    求出大于询问值的最小的未出现过的数,标记,再遍历预处理,如果是询问一次处理一次会超时。

    官方:先排下序,然后记录每个数字出现的位置。对于询问q,如果q不存在直接输出q,如果q存在。 假设q所在位置为pos,那么二分[pos, n]这个区间,二分判断的依据是如果mid - p == num[mid] - num[p] 那么left = mid+1, 否则right = mid-1。 时间复杂度O(m * lgn)

    

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

#define N 200010

int tt[N];
bool vis[N];

void init()
{
    int tmp = N;
    for(int i = N; i >= 1; i --)
    {
        if(!vis[i])
        {
            tmp = i;
        }
        tt[i] = tmp;
    }
}

int main()
{
    int t;
    int n, m, a;
    while(~scanf("%d",&t))
    {
        while(t --)
        {
            memset(vis, false, sizeof(vis));
            memset(tt, 0, sizeof(tt));
            scanf("%d%d",&n,&m);
            for(int i = 0; i < n; i ++)
            {
                scanf("%d",&a);
                vis[a] = true;
            }
            init();
            for(int i = 0; i < m; i ++)
            {
                scanf("%d",&a);
                printf("%d\n",tt[a]);
            }
        }
    }
}

BestCoder Sequence


Problem Description
Mr Potato is a coder.
Mr Potato is the BestCoder.

One night, an amazing sequence appeared in his dream. Length of this sequence is odd, the median number is M, and he named this sequence as  Bestcoder Sequence.

As the best coder, Mr potato has strong curiosity, he wonder the number of consecutive sub-sequences which are  bestcoder sequences in a given permutation of 1 ~ N.
 

Input
Input contains multiple test cases. 
For each test case, there is a pair of integers N and M in the first line, and an permutation of 1 ~ N in the second line.

[Technical Specification]
1. 1 <= N <= 40000
2. 1 <= M <= N
 

Output
For each case, you should output the number of consecutive sub-sequences which are the  Bestcoder Sequences
 

Sample Input
  
  
1 1 1 5 3 4 5 3 2 1
 

Sample Output
  
  
1 3
Hint
For the second case, {3},{5,3,2},{4,5,3,2,1} are Bestcoder Sequence.
 

    以前周赛有出现过一模一样的题、、

   官方:将大于M的数标记为1,小于M的数标记为-1,M本身标记为0,则题目就是要求和为0并且包括M的连续序列的个数;用sum_i表示从第一个数到第i个数的标记的和,对于所有大于等于M的位置的i,我们要求小于M的位置的sum_j == sum_i的个数的和即为答案。

    

#include <cstdio>
#include <string>
#include <algorithm>
#include<iostream>

using namespace std;


struct node
{
    int sum, l;
}s[100010];

bool cmp (node a, node b)
{
    if(a.sum == b. sum)
        return a.l > b.l;
    else
        return a.sum < b.sum;
}

int main()
{
    int a;
    int n, m;
    while(~scanf("%d%d",&n,&m))
    {
        int tmp , ok = 0;
        for(int i = 1; i <= n ; i ++)
        {
            scanf("%d",&a);
            if(a == m)
            {
                tmp = i;
                ok = 1;
            }
            if(!ok)
                s[i].l = 0;
            else
                s[i].l = 1;
            if(a > m)
                a = 1;
            else if(a < m)
                a = -1;
            else
                a = 0;
            s[i].sum = s[i - 1].sum + a;
        }
        int ans = 0;
        for(int i = tmp; i <= n; i ++)
        {
            if(s[i].sum == 0)
                ans ++;
        }
        sort(&s[1], &s[n + 1], cmp);
        int l = 0, r = 0;
        for(int i = 1; i <= n; i ++)
        {
            if(tmp == s[i].sum)
            {
                if(s[i].l)
                    l ++;
                else
                    r ++;
            }
            else
            {
                ans += l * r;
                l = r = 0;
                tmp = s[i].sum;
                if(s[i].l)
                    l ++;
                else
                    r ++;
            }
        }
        ans += l * r;
        printf("%d\n", ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值