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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章