训练赛20190304

3 篇文章 0 订阅
3 篇文章 0 订阅

D - Balanced Lineup

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

Input

Line 1: Two space-separated integers, N and Q
Lines 2.. N+1: Line i+1 contains a single integer that is the height of cow i 
Lines N+2.. NQ+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

Output

Lines 1.. Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

根据题意可得知这个题就是给出N个数,多次询问在一个范围内,最大值和最小值的差,很明显的一个RMQ模板题,当然也有大佬用线段树做了。。。RMQ可以看一下我的这个博客

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
const int N=1e5+10;
int n,q;
int maxn[N][25],minx[N][25];
void rmq(int n)
{
    for(int i=1;i<20;i++){
        for(int j=1;j+(1<<i)-1<=n;j++){
            maxn[j][i]=max(maxn[j][i-1],maxn[j+(1<<(i-1))][i-1]);
            minx[j][i]=min(minx[j][i-1],minx[j+(1<<(i-1))][i-1]);
        }
    }
}
int main()
{
    while(~sc(n)){
        scanf("%d",&q);
        for(int i=1;i<=n;i++){
            sc(maxn[i][0]);
            minx[i][0]=maxn[i][0];
        }
        rmq(n);
        int l,r;
        while(q--){
            scanf("%d%d",&l,&r);
            int k=(int)(log(r-l+1.0)/log(2.0));
        int Maxn=max(maxn[l][k],maxn[r-(1<<k)+1][k]);
        int Minx=min(minx[l][k],minx[r-(1<<k)+1][k]);
        printf("%d\n",Maxn-Minx);
        }
    }
}

F - 敌兵布阵

C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。 
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的. 

Input

第一行一个整数T,表示有T组数据。 
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。 
接下来每行有一条命令,命令有4种形式: 
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30) 
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30); 
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数; 
(4)End 表示结束,这条命令在每组数据最后出现; 
每组数据最多有40000条命令 

Output

对第i组数据,首先输出“Case i:”和回车, 
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。 

Sample Input

1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End 

Sample Output

Case 1:
6
33
59

这也是个树状数组的模板题,而且这个题我之前做过。。。

这算一个单点修改,区间查询,算是树状数组里最简单的一种题型了,也可以看一下我的这个博客

#include<bits/stdc++.h>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
const int N=1e5+10;
int n,c[N];
void update(int i,int val)
{
    while(i<=n){
        c[i]+=val;
        i+=lowbit(i);
    }
}
int sum(int i)
{
    int ans=0;
    while(i>0){
        ans+=c[i];
        i-=lowbit(i);
    }
    return ans;
}
int main()
{
    int t;
    sc(t);
    for(int i=1;i<=t;i++){
        sc(n);
        met(c,0);
        int x;
        for(int j=1;j<=n;j++){
            sc(x);
            update(j,x);
        }
                char p[10];
        printf("Case %d:\n",i);
        scanf("%s",p);
        int y;
        while(p[0]!='E'){
            scanf("%d%d",&x,&y);
            if(p[0]=='A')
                update(x,y);
            else if(p[0]=='S')
                update(x,-y);
            else
                printf("%d\n",sum(y)-sum(x-1));
            scanf("%s",&p);
        }
    }
}

G - Oulipo

 

The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book:

Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…

Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces.

So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {'A', 'B', 'C', …, 'Z'} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line with the word W, a string over {'A', 'B', 'C', …, 'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
  • One line with the text T, a string over {'A', 'B', 'C', …, 'Z'}, with |W| ≤ |T| ≤ 1,000,000.

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.

Sample Input

3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN

Sample Output

1
3
0

题意:给定一个串t,和一个串s,问t在s中可以出现过几次。

也是一个KMP的模板,学习KMP的话tyk在群里发的视频就不错......

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
const int N=1e5+10;
int next1[N],l1,l2,ans;
char str1[1000001],str2[10001];
void getnext()
{
	int i = 0, j = -1;
	next1[i] = j;
	while(i < l2) {
		if(j == -1 || str2[i] == str2[j]) {
			++i; ++j;
            next1[i] = j;
		}
		else j = next1[j];
	}
}
int kmp()
{
    getnext();
    int i=0;
    int j=0;
    int ans=0;
    while(i<l1){
        if(j==-1||str1[i]==str2[j]){
            i++;
            j++;
        }
        else j=next1[j];
        if(j==l2){
           // j=0;
            ans++;
            //i--;
        }
    }
    return ans;
}
int main()
{
    int t;
    sc(t);
    while(t--){
            ans=0;
        scanf("%s%s",str2,str1);
        l1=strlen(str1);
        l2=strlen(str2);
        printf("%d\n",kmp());
    }
}

H - Power Strings

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.
Sample Output
1
4
3

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

 

题意:给出一个字符串s,找由它的几个最小循环子串能够组成字符串s?

这个题如果暴力的话显然很容易t掉,这里我们可以用到next数组了,next数组的意义是什么呢?

next[i]代表到以i位置截止的s串(不加第一个字符)与s串的前缀的最大公共串。

那么len(长度)-next[len]即为最小循环节,然后直接判断即可

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
const int N=1e5+10;
int next1[N],l1,l2,ans;
char str1[1000001],str2[10001];
void getnext()
{
	int i = 0, j = -1;
	next1[i] = j;
	while(i < l2) {
		if(j == -1 || str2[i] == str2[j]) {
			++i; ++j;
            next1[i] = j;
		}
		else j = next1[j];
	}
}
int kmp()
{
    getnext();
    int i=0;
    int j=0;
    int ans=0;
    while(i<l1){
        if(j==-1||str1[i]==str2[j]){
            i++;
            j++;
        }
        else j=next1[j];
        if(j==l2){
           // j=0;
            ans++;
            //i--;
        }
    }
    return ans;
}
int main()
{
    int t;
    sc(t);
    while(t--){
            ans=0;
        scanf("%s%s",str2,str1);
        l1=strlen(str1);
        l2=strlen(str2);
        printf("%d\n",kmp());
    }
}

L - 搬果子

果园里面有n堆果子,每堆果子有xi个,每个果子的重量为1,小明每次把i,j两堆果子移成一堆,需要花费的体力为xi+xj。最后移成一堆,求最小花费体力值。
其中1<=n<=10000,1<=m<=10000。均为正整数。

Input

    每组数据第一行输入一个正整数n,表示有n堆果子。

    接下来一行有n个正整数,表示每堆果子的重量。

    输入以EOF结尾。

Output

    每组数据单独一行,输出所花费的最小体力值。

Sample Input

3

1 2 9

5

1 3 9 18 30

Sample Output

15

109

水题,用优先队列做做即可,一开始把它想成区间dp了,尴尬。。

#include<bits/stdc++.h>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
int n,a[10001];
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
    while(~sc(n)){
        met(a,0);
        for(int i=1;i<=n;i++){
            sc(a[i]);
            q.push(a[i]);
        }
        int p1,p2;
        int ans=0;
        while(!q.empty()){
            p1=q.top();
            q.pop();
            if(q.empty()){
                printf("%d\n",ans);
                break;
            }
            p2=q.top();
            q.pop();
            p1+=p2;
            ans+=p1;
            q.push(p1);
        }

    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值