本文主要是介绍The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online A,K,C,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
K XOR Clique
BaoBao has a sequence a1,a2,...,an. He would like to find a subset S of {1,2,...,n} such that ∀i,j∈S, ai⊕aj<min(ai,aj) and ∣S∣ is maximum, where ⊕ means bitwise exclusive or.
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≤105), indicating the length of the sequence.
The second line contains n integers: a1,a2,...,an (1≤ai≤109), indicating the sequence.
It is guaranteed that the sum of n in all cases does not exceed 105.
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
题意:给N个数,从中选取一些书作为集合S,并且 ∀i,j∈S, ai⊕aj<min(ai,aj),求max(|S|)。
题解:思路应该比较简单,就是求这N个数里面2进制最高位位数有数时对应范围的数的个数谁最多,例如最高第三位有数,则100~111,最高第二位有数时,10~11;
AC代码:
#include <iostream>
#include <algorithm>
#include<cmath>
using namespace std;
const int maxn=1e5+5;
int a[maxn];
int main()
{int m,n,t;cin>>t;while(t--){cin>>n;for(int i=1;i<=n;i++)cin>>a[i];sort(a+1,a+n+1);int flag=0;int tj=0;int tflag=0;for(int i=1;i<=n;i++){/*if(a[i]==1&&i==1)continue;if(a[i]==1&&a[i-1]==1){tflag++;continue;}if(a[i]==1&&a[i+1]!=1){flag=max(flag,tflag);tflag=0;continue;}*/if(pow(2,tj)<=a[i]){ while(1){tj++;if(pow(2,tj)>a[i]){flag=max(flag,tflag);tflag=0;break;}}}if(a[i]<pow(2,tj)){tflag++;//cout<<tflag<<endl;continue;}}flag=max(tflag,flag);cout<<flag<<endl;}return 0;
}
C Halting Problem
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≤104), 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 105.
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.
题意:就是一般的模拟,然后判断是否为无限循环或者不是无限循环
题解:就是用一个二维数组记录下是否经过这一步,经过则为无限循环。
AC代码:
//C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>using namespace std;
typedef long long llong;
const int MAXN = 10000 + 10;typedef struct
{int p, v, k;
} node;bool book[MAXN][260];int main()
{int t, n, i, r, sgn;char str[7];int p, v, k;node a[MAXN];scanf("%d", &t);getchar();while(t--){scanf("%d", &n);getchar();for(i = 1; i <= n; i++){scanf("%s", str);getchar();if(str[1] == 'd'){scanf("%d", &k);getchar();p = 1;a[i] = (node){p, v, k};continue;}scanf("%d%d", &v, &k);getchar();if(str[1] == 'e'){p = 2;}else if(str[1] == 'n'){p = 3;}else if(str[1] == 'l'){p = 4;}else if(str[1] == 'g'){p = 5;}a[i] = (node){p, v, k};}memset(book, false, sizeof(book));i = 1; r = 0;book[i][r] = true;sgn = 1;while(i <= n){p = a[i].p;v = a[i].v;k = a[i].k;if(p == 1){r += k;r %= 256;i++;}else if(p == 2){i = (r == v) ? k : i + 1;}else if(p == 3){i = (r != v) ? k : i + 1;}else if(p == 4){i = (r < v) ? k : i + 1;}else if(p == 5){i = (r > v) ? k : i + 1;}if(book[i][r] == true){sgn = 0;break;}book[i][r] = true;}if(sgn == 1){printf("Yes\n");}else{printf("No\n");}}return 0;
}
A Live Love
DreamGrid is playing the music game Live Love. He has just finished a song consisting of nnotes and got a result sequence A1,A2,...,An (Ai∈ {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 Ai=Ai+1=Ai+2=...=Ai+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 samong 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≤103, 0≤m≤103, m≤n), indicating the sequence length and the number of PERFECTs DreamGrid gets.
Output
For each test case output one line containing two integers smax and smin, 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.
题解:这题是我队友做的,感觉也比较简单,直接上代码了。
AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
int n,m;
int main()
{int T;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);int nc = n - m + 1;printf("%d %d\n",m,n/nc);}return 0;
}
这篇关于The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online A,K,C的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!