哈理工训练赛20190304

https://vjudge.net/contest/285796

A Watto and Mechanism

B Infinite Inversions

C Pashmak and Parmida’s problem

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… N+ Q+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

解析

求一段序列内的最大值-最小值.正经方法用RMQ,不会
直接线段树维护区间最大最小值,两次查询相减.用读入优化,如果使用cin会tle.

#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#ifndef NULL
#define NULL 0
#endif
using namespace std;

typedef struct Segment_Tree* Node;
struct Segment_Tree {
	long long maxx,minn;
	int left, right;
	Node lson, rson;
}*root;
long long read()
{
	long long f = 1,x = 0;
	char s = getchar();
	while (s<'0' || s>'9') {
		if (s == '-')
			f = -1;
		s = getchar();
	}
	while (s >= '0'&&s <= '9') {
		x = x * 10 + s - '0';
		s = getchar();
	}
	x *= f;
	return x;
}
Node built(int left, int right)
{
	Node p = new(Segment_Tree);
	p->left = left;
	p->right = right;
	if (left == right) {
		p->maxx=read();
		p->minn = p->maxx;
		p->lson = NULL;
		p->rson = NULL;
	}
	else {
		int mid = (left + right) / 2;
		p->lson = built(left, mid);
		p->rson = built(mid + 1, right);
		p->maxx = max(p->lson->maxx , p->rson->maxx);
		p->minn = min(p->lson->minn, p->rson->minn);
	}
	return p;
}
long long find1(Node p, int x, int y)  //区间查找
{
	if (p->left == x && p->right == y)
		return p->maxx;
	int mid = (p->left + p->right) / 2;
	if (y <= mid)
		return find1(p->lson, x, y);
	if (x > mid)
		return find1(p->rson, x, y);
	return max(find1(p->lson, x, mid) , find1(p->rson, mid + 1, y));
}
long long find2(Node p, int x, int y)  //区间查找
{
	if (p->left == x && p->right == y)
		return p->minn;
	int mid = (p->left + p->right) / 2;
	if (y <= mid)
		return find2(p->lson, x, y);
	if (x > mid)
		return find2(p->rson, x, y);
	return min(find2(p->lson, x, mid), find2(p->rson, mid + 1, y));
}
void close(Node p)
{
	if (p != NULL) {
		close(p->lson);
		close(p->rson);
		delete(p);
	}
	return;
}
int main()
{
	int n, m;
	n = read(), m = read();
	root = built(1, n);
	for (int i = 0; i < m; i++) {
		long long x, y;
		x = read(), y = read();
		cout << find1(root, x, y)-find2(root,x,y) << endl;
	}
	close(root);
	return 0;
}

E Snowflake Snow Snowflakes

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

解析

裸的线段树,要lazy-tag优化,读入不能用cin,否则tle

扫描二维码关注公众号,回复: 8656701 查看本文章
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#ifndef NULL
#define NULL 0
#endif
using namespace std;

typedef struct Segment_Tree* Node;
struct Segment_Tree {
	long long d, lazy;
	int left, right;
	Node lson, rson;
}*root;
int read()
{
	int f = 1,x = 0;
	char s = getchar();
	while (s<'0' || s>'9') {
		if (s == '-')
			f = -1;
		s = getchar();
	}
	while (s >= '0'&&s <= '9') {
		x = x * 10 + s - '0';
		s = getchar();
	}
	x *= f;
	return x;
}
Node built(int left, int right)
{
	Node p = new(Segment_Tree);
	p->left = left;
	p->right = right;
	p->lazy = 0;
	if (left == right) {
		p->d=read();
		p->lson = NULL;
		p->rson = NULL;
	}
	else {
		int mid = (left + right) / 2;
		p->lson = built(left, mid);
		p->rson = built(mid + 1, right);
		p->d = p->lson->d + p->rson->d;
	}
	return p;
}
void pushdown(Node p)
{
	if (p->lson != NULL) {
		p->lson->lazy += p->lazy;
		p->lson->d += (p->lson->right - p->lson->left + 1)*p->lazy;
		p->rson->lazy += p->lazy;
		p->rson->d += (p->rson->right - p->rson->left + 1)*p->lazy;
	}
	p->lazy = 0;
}
long long find(Node p, int x, int y)  //区间查找
{
	if (p->lazy != 0)
		pushdown(p);
	if (p->left == x && p->right == y)
		return p->d;
	int mid = (p->left + p->right) / 2;
	if (y <= mid)
		return find(p->lson, x, y);
	if (x > mid)
		return find(p->rson, x, y);
	return find(p->lson, x, mid) + find(p->rson, mid + 1, y);
}
long long update(Node p, int x, int y, int k)  //区间
{
	if (p->lazy != 0)
		pushdown(p);
	if (p->left == x && p->right == y) {
		p->lazy = k;
		return p->d += k * (y - x + 1);
	}
	int mid = (p->left + p->right) / 2;
	if (y <= mid)
		return p->d = p->rson->d + update(p->lson, x, y, k);
	if (x > mid)
		return p->d = p->lson->d + update(p->rson, x, y, k);
	return p->d = update(p->lson, x, mid, k) + update(p->rson, mid + 1, y, k);
}
void close(Node p)
{
	if (p != NULL) {
		close(p->lson);
		close(p->rson);
		delete(p);
	}
	return;
}
int main()
{
	int t,n;
	t = read();
	for (int i = 1; i <= t; i++) {
		string s;
		int x, y;
		n=read();
		root = built(1, n);
		cout << "Case " << i << ":" << endl;
		while (cin >> s) {
			if (s == "End")
				break;
			x=read(),y=read();
			if (s == "Query")
				cout << find(root, x, y) << endl;
			else if (s == "Add")
				update(root, x,x, y);
			else if (s == "Sub")
				update(root, x,x, -y);
		}
		close(root);
	}
	return 0;
}

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

解析

kmp模板,读入不能用cin,否则超时

#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#ifndef NULL
#define NULL 0
#endif
using namespace std;

typedef long long ll;
char t[1000100], s[1000100];
int len1, len2, n[1000100];
ll read()
{
	ll f = 1,x = 0;
	char s = getchar();
	while (s<'0' || s>'9') {
		if (s == '-')
			f = -1;
		s = getchar();
	}
	while (s >= '0'&&s <= '9') {
		x = x * 10 + s - '0';
		s = getchar();
	}
	x *= f;
	return x;
}
int KMP(char *s, char *t)
{
	int ans = 0;
	for (int i = 0, j = -1; i < len1; i++) {
		while (j != -1 && t[j + 1] != s[i])
			j = n[j];
		if (t[j + 1] == s[i])
			j++;
		if (j == len2 - 1) {
			ans++;
			j = n[j];
		}
	}
	return ans;
}
void getnext(char *t)
{
	n[0] = -1;
	for (int i = 1, j = -1; i < len2; i++) {
		while (j != -1 && t[i] != t[j + 1])
			j = n[j];
		if (t[i] == t[j + 1])
			j++;
		n[i] = j;
	}
}
int main()
{
	int m;
	m = read();
	while (m--) {
		memset(n, 0, sizeof(n));
		scanf("%s %s", &t, &s);
		len1 = strlen(s);
		len2 = strlen(t);
		getnext(t);
		cout<<KMP(s, t)<<endl;
	}
	return 0;
}

H Power Strings

Given two strings a and b we define ab to be their concatenation. For example, if a = “abc” and b = “def” then ab = “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.

解析

KMP的next数组以及一个定理.不能用string和cin,会tle和re

假设S的长度为len,则S存在循环子串,当且仅当,len可以被len - next[len]整除,最短循环子串为S[len - next[len]]

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#ifndef NULL
#define NULL 0
#endif
using namespace std;

typedef long long ll;
char t[10000010];
int len, n[10001000];
ll read()
{
	ll f = 1,x = 0;
	char s = getchar();
	while (s<'0' || s>'9') {
		if (s == '-')
			f = -1;
		s = getchar();
	}
	while (s >= '0'&&s <= '9') {
		x = x * 10 + s - '0';
		s = getchar();
	}
	x *= f;
	return x;
}
void getnext(char *t)
{
	n[0] = -1;
	for (int i = 1, j = -1; i < len; i++) {
		while (j != -1 && t[i] != t[j + 1])
			j = n[j];
		if (t[i] == t[j + 1])
			j++;
		n[i] = j;
	}
}
int main()
{
	while (~scanf("%s", &t)) {
		if (t[0] == '.')
			break;
		memset(n, 0, sizeof(n));
		len=strlen(t);
		getnext(t);
		len--;
		if ((len+1) % (len - n[len]) == 0)
			cout << (len+1) / (len - n[len]) << endl;
		else
			cout << 1 << endl;
	}
	return 0;
}

I.Period

For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as A K ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input

The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the
number zero on it.

Output

For each test case, output “Test case #” and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.
Sample Input
3
aaa
12
aabaabaabaab
0
Sample Output
Test case #1
2 2
3 3

Test case #2
2 2
6 2
9 3
12 4

解析

魔改一下KMP的next数组
最后多求一个全部字符串的next值,即可把每组数据的最后一行答案求出来。

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#ifndef NULL
#define NULL 0
#endif
using namespace std;

typedef long long ll;
char t[10000010];
int len, n[10001000], nest[1000100];
ll read() {
  ll f = 1, x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-')
      f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
  x *= f;
  return x;
}
void getnext(char *t) {
  n[0] = -1;
  for (int i = 0, j = -1; i < len; i++) {
    while (j != -1 && t[i] != t[j])
      j = n[j];
    j++;
    n[i + 1] = j;
  }
}
int main() {
  int k = 1;
  while (len = read(), len) {
    scanf("%s", &t);
    getnext(t);
    cout << "Test case #" << k << endl;
    for (int i = 1; i <= len; i++)
      if (i %( i - n[i])== 0 && i / (i - n[i]) > 1)
        printf("%d %d\n", i, i /( i - n[i]));
    k++;
    cout<<endl;
  }
}

J Hat’s Words

K Black Box

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

解析

贪心水题.
每次将堆里的两个最小的合并,用优先队列维护,由于c++默认大根堆,故改变每个数的符号,即可达到小根堆的目的.(比赛节约时间就没有手写小根堆,也懒得写cmp)

#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#ifndef NULL
#define NULL 0
#endif
using namespace std;

int a[100010];
typedef long long ll;
ll read()
{
	ll f = 1,x = 0;
	char s = getchar();
	while (s<'0' || s>'9') {
		if (s == '-')
			f = -1;
		s = getchar();
	}
	while (s >= '0'&&s <= '9') {
		x = x * 10 + s - '0';
		s = getchar();
	}
	x *= f;
	return x;
}
int main()
{
	int n;
	while (cin >> n) {
		ll ans = 0;
		priority_queue<ll >q;
		for (int i = 0; i < n; i++) {
			ll x = -read();
			q.push(x);
		}
		while (q.size() > 1) {
			ll x = q.top(); q.pop();
			ll y = q.top(); q.pop();
			ans += x + y;
			q.push(x + y);
		}
		q.pop();
		cout << -ans << endl;
	}
	return 0;
}
发布了62 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/UnKfrozen/article/details/88138810