The 2018 ACM-ICPC Asia Qingdao Regional Contest

目录

  • A Live Love(思维)
  • C Halting Problem(模拟)
  • H Traveling on the Axis(前缀和+思维)
  • K  XOR Clique(异或&&思维)

A Live Love(思维)

题目链接ZOJ-4047

DreamGrid is playing the music game Live Love. He has just finished a song consisting of n notes and got a result sequence A​1​​,A​2​​,...,A​n​​ (A​i​​∈ {PERFECT, NON-PERFECT}). The score of the song is equal to the max-combo of the result sequence, which is defined as the maximum number of continuous PERFECTs in the sequence.

Formally speaking, max-combo(A)=max { k | k is an integer and there exists an integer i (1≤i≤n−k+1) such that A​i​​=A​i+1​​=A​i+2​​=...=A​i+k−1​​= PERFECT }. For completeness, we define max(∅)=0.

As DreamGrid is forgetful, he forgets the result sequence immediately after finishing the song. All he knows is the sequence length n and the total number of PERFECTs in the sequence, indicated by m. Any possible score s he may get must satisfy that there exists a sequence A​′​​ of length n containing exactly m PERFECTs and (n−m) NON-PERFECTs and max-combo(A​′​​)=s. Now he needs your help to find the maximum and minimum s among all possible scores.

Input

There are multiple test cases. The first line of the input contains an integer T (1≤T≤100), indicating the number of test cases. For each test case:

The only line contains two integers n and m (1≤n≤10​3​​, 0≤m≤10​3​​, m≤n), indicating the sequence length and the number of PERFECTs DreamGrid gets.

Output

For each test case output one line containing two integers s​max​​ and s​min​​, indicating the maximum and minimum possible score.

Sample Input

5
5 4
100 50
252 52
3 0
10 10

Sample Output

4 2
50 1
52 1
0 0
10 10

Hint

Let's indicate a PERFECT as P and a NON-PERFECT as N.

For the first sample test case, the sequence (P,P,P,P,N) leads to the maximum score and the sequence (P,P,N,P,P) leads to the minimum score.

#include
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        int maxx=m,minn;
        if(n==m)minn=maxx;
        else if(m==0)minn=0;
        else if(m-1<=n-m)minn=1;
        else minn=n/(n-m+1);
        printf("%d %d\n",maxx,minn);
    }
    return 0;
}

 

C Halting Problem(模拟)

题目链接ZOJ-4049

In computability theory, the halting problem is the problem of determining, from a description of an arbitrary computer program, whether the program will finish running (i.e., halt) or continue to run forever.

Alan Turing proved in 1936 that a general algorithm to solve the halting problem cannot exist, but DreamGrid, our beloved algorithm scientist, declares that he has just found a solution to the halting problem in a specific programming language -- the Dream Language!

Dream Language is a programming language consists of only 5 types of instructions. All these instructions will read from or write to a 8-bit register r, whose value is initially set to 0. We now present the 5 types of instructions in the following table. Note that we denote the current instruction as the i-th instruction.

Instruction Description
add v Add v to the register r. As r is a 8-bit register, this instruction actually calculates (r+v)mod256 and stores the result into r, i.e. r←(r+v)mod256. After that, go on to the (i+1)-th instruction.
beq v k If the value of r is equal to v, jump to the k-th instruction, otherwise go on to the (i+1)-th instruction.
bne v k If the value of r isn't equal to v, jump to the k-th instruction, otherwise go on to the (i+1)-th instruction.
blt v k If the value of r is strictly smaller than v, jump to the k-th instruction, otherwise go on to the (i+1)-th instruction.
bgt v k If the value of r is strictly larger than v, jump to the k-th instruction, otherwise go on to the (i+1)-th instruction.

A Dream Language program consisting of n instructions will always start executing from the 1st instruction, and will only halt (that is to say, stop executing) when the program tries to go on to the (n+1)-th instruction.

As DreamGrid's assistant, in order to help him win the Turing Award, you are asked to write a program to determine whether a given Dream Language program will eventually halt or not.

Input

There are multiple test cases. The first line of the input is an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤10​4​​), indicating the number of instructions in the following Dream Language program.

For the following n lines, the i-th line first contains a string s (s∈{“add”,“beq”,“bne”,“blt”,“bgt”}), indicating the type of the i-th instruction of the program.

  • If s equals to "add", an integer v follows (0≤v≤255), indicating the value added to the register;
  • Otherwise, two integers v and k follow (0≤v≤255, 1≤k≤n), indicating the condition value and the destination of the jump.

It's guaranteed that the sum of n of all test cases will not exceed 10​5​​.

Output

For each test case output one line. If the program will eventually halt, output "Yes" (without quotes); If the program will continue to run forever, output "No" (without quotes).

Sample Input

4
2
add 1
blt 5 1
3
add 252
add 1
bgt 252 2
2
add 2
bne 7 1
3
add 1
bne 252 1
beq 252 1

Sample Output

Yes
Yes
No
No

Hint

For the second sample test case, note that r is a 8-bit register, so after four "add 1" instructions the value of r will change from 252 to 0, and the program will halt.

For the third sample test case, it's easy to discover that the value of r will always be even, so it's impossible for the value of r to be equal to 7, and the program will run forever.

【题意】给你n天指令,对于每条指令执行相关操作。问最后能否退出。

【分析】当时读题读了好久,在想怎样算正常退出,怎样算非正常退出。然鹅这道题不是问正常还是非正常,而只是问能不能退出的问题。即循环回到之前出现过的某一状态了,这样就说明它进入了循环状态,无法退出了。于是,可以用hash。如果在执行某条指令并且读入该指令r后发现这样的第pos条指令以及r出现过,则说明进入了死循环......好的,这样就是无法退出了。

【代码】

#include
using namespace std;
const int maxn=1e4+5;
const int mod=256;
bool dp[maxn][257];//n条指令,第二维指的是r
struct node{
	char op[5];
	int v,k;
}a[maxn];
bool check(int pos,int r)
{
	if(dp[pos][r])return false;
	else return true;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%s",a[i].op);
			if(!strcmp(a[i].op,"add"))scanf("%d",&a[i].v);
			else scanf("%d%d",&a[i].v,&a[i].k);
		}
		int r=0,pos=1;
		memset(dp,false,sizeof(dp));
		bool flag=true;
		while(pos<=n&&flag)
		{
			flag=check(pos,r);
			if(!flag)break;
			dp[pos][r]=true;
			if(!strcmp(a[pos].op,"add")){
				r=(r+a[pos].v)%mod;
				pos++;
			}
			else if(!strcmp(a[pos].op,"beq")){
				if(r==a[pos].v)pos=a[pos].k;
				else pos++;
			}
			else if(!strcmp(a[pos].op,"bne")){
				if(r!=a[pos].v)pos=a[pos].k;
				else pos++;
			}
			else if(!strcmp(a[pos].op,"blt")){
				if(ra[pos].v)pos=a[pos].k;
				else pos++;
			}
		}
		if(flag)printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

H Traveling on the Axis(前缀和+思维)

题目链接ZOJ-4054

BaoBao is taking a walk in the interval [0,n] on the number axis, but he is not free to move, as at every point (i−0.5) for all i∈[1,n], where i is an integer, stands a traffic light of type t​i​​ (t​i​​∈{0,1}).

BaoBao decides to begin his walk from point p and end his walk at point q (both p and q are integers, and pin order:

  1. Let's say BaoBao is currently at point x, he will then check the traffic light at point (x+0.5). If the traffic light is green, BaoBao will move to point (x+1); If the traffic light is red, BaoBao will remain at point x.
  2. All the traffic lights change their colors. If a traffic light is currently red, it will change to green; If a traffic light is currently green, it will change to red.

A traffic light of type 0 is initially red, and a traffic light of type 1 is initially green.

Denote t(p,q) as the total units of time BaoBao needs to move from point p to point q. For some reason, BaoBao wants you to help him calculate

​p=0​∑​n−1​​​q=p+1​∑​n​​t(p,q)

where both p and q are integers. Can you help him?

Input

There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:

The first and only line contains a string s (1≤∣s∣≤10​5​​, ∣s∣=n, s​i​​∈{‘0’,‘1’} for all 1≤i≤∣s∣), indicating the types of the traffic lights. If s​i​​=‘0’, the traffic light at point (i−0.5) is of type 0 and is initially red; If s​i​​=‘1’, the traffic light at point (i−0.5) is of type 1 and is initially green.

It's guaranteed that the sum of ∣s∣ of all test cases will not exceed 10​6​​.

Output

For each test case output one line containing one integer, indicating the answer.

Sample Input

3
101
011
11010

Sample Output

12
15
43

Hint

For the first sample test case, it's easy to calculate that t(0,1)=1, t(0,2)=2, t(0,3)=3, t(1,2)=2, t(1,3)=3 and t(2,3)=1, so the answer is 1+2+3+2+3+1=12.

For the second sample test case, it's easy to calculate that t(0,1)=2, t(0,2)=3, t(0,3)=5, t(1,2)=1, t(1,3)=3 and t(2,3)=1, so the answer is 2+3+5+1+3+1=15.

 【分析】如果相同,一次加2,否则,+1;

【代码】

#include
using namespace std;
typedef long long ll;
const int maxn=1e6+5;
char a[maxn];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%s",a);
		ll ans=0;
		int len=strlen(a);
		if(a[0]=='0')ans=2;
		else ans=1;
		ll w=ans;
		for(int i=1;i

K  XOR Clique(异或&&思维)

题目链接 ZOJ-4057

BaoBao has a sequence a​1​​,a​2​​,...,a​n​​. He would like to find a subset S of {1,2,...,n} such that ∀i,j∈S, a​i​​⊕a​j​​

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n (1≤n≤10​5​​), indicating the length of the sequence.

The second line contains n integers: a​1​​,a​2​​,...,a​n​​ (1≤a​i​​≤10​9​​), indicating the sequence.

It is guaranteed that the sum of n in all cases does not exceed 10​5​​.

Output

For each test case, output an integer denoting the maximum size of S.

Sample Input

3
3
1 2 3
3
1 1 1
5
1 2323 534 534 5

Sample Output

2
3
2

【题意】求序列中,任意两个数的异或结果比这两个数中的最小值小的个数

【分析】只要两者位数相同,异或后肯定比两个数都小; 位数不同的必然不符合。

#include
using namespace std;
const int maxn=1e5+5;
int a[maxn],b[maxn],c[maxn];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,cnt=0,flag=0;
        scanf("%d",&n);
        for(int i=0;i

 

你可能感兴趣的:(思维,模拟)