Codeforces Gym 101608 - 2017 ACM Jordanian Collegiate Programming Contest

A. Chrome Tabs
time limit per test
2.0 s
memory limit per test
256 MB
input
tabs.in
output
standard output

Before participating in any online contest, you only keep the tab of the online contest open and close everything else.

You have n open tabs, numbered from 1 to n from left to right. Tab number k is the tab of the online contest and it is the current selected one. You have only two buttons:

  • Close all tabs to the left of the selected tab.
  • Close all tabs to the right of the selected tab.

What is the minimum number of clicks needed to close all tabs except tab number k?

Input

The first line of input contains a single integer T (1 ≤ T ≤ 5050), the number of test cases.

Each test case consists of a single line that contains two space-separated integers n and k(1 ≤ k ≤ n ≤ 100), the number of open tabs, and the tab of the online contest, respectively.

Output

For each test case, print the minimum number of clicks needed to close all tabs except the tab of the online contest.

Example
input
4
72 1
5 2
10 7
64 64
output
1
2
2
1


水题

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);

int main() {
	freopen("tabs.in","r",stdin);
	int cas;
	scanf("%d",&cas);
	while (cas--) {
		int n,k,pos,ans;
		scanf("%d%d",&n,&k);
		if (k==1||k==n) ans=1; else ans=2;
		if (k==1&&n==1) ans=0;
		printf("%d\n",ans);
	}
fclose(stdin);
	return 0;
}

B. OverCode
time limit per test
1.0 s
memory limit per test
256 MB
input
overcode.in
output
standard output

In a competitive coding contest called OverCode, matches are held between two teams. Each team must have exactly 6 coders. In order to form fair matches between coders, the absolute difference between the ratings of any two members of the same team shouldn't exceed 1000.

What is the maximum number of teams that can be formed out of n coders if you were given their ratings, such that each coder is a member of at most one team?

Input

The first line of input contains a single integer T (1 ≤ T ≤ 512), the number of test cases.

The first line of each test case contains a single integer n (1 ≤ n ≤ 500), the number of coders participating in OverCode.

The next line contains n space-separated integers r1, r2, ..., rn (1 ≤ ri ≤ 4000)ri is the rating of the ithcoder.

The total sum of n overall test cases doesn't exceed 1.5 × 105.

Output

For each test case, print the maximum number of teams that can be formed, on a single line.

Example
input
3
5
4 1 5 3 2
6
1 1001 2 4 3 5
13
7 9 7 2 1000 1001 2 3 9 4 5 2 5
output
0
1
2


排序之后贪心。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=505,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int a[maxn];

int main() {
	freopen("overcode.in","r",stdin);
	int cas;
	scanf("%d",&cas);
	while (cas--) {
		int n,i,j,ans=0;
		scanf("%d",&n);
		for (i=1;i<=n;i++) scanf("%d",&a[i]);
		sort(a+1,a+n+1);
		int cnt=1;j=1;
		for (i=2;i<=n;i++) {
			if (abs(a[i]-a[j])>1000) {
				ans+=cnt/6;
				while (abs(a[i]-a[j])>1000) j++;
				cnt=i-j+1;
			} else {
				cnt++;
				if (cnt==6) ans++,cnt=0,j=i+1; 
			}
		}
		printf("%d\n",ans);
	}
	fclose(stdin);
	return 0;
}
C. A message for you!
time limit per test
3.0 s
memory limit per test
256 MB
input
scoreboard.in
output
standard output

You’re participating in the ACM JCPC for the first time. Your team solved k out of the 13 problems. Since your last successful submission, you have been trying to solve new problems with no luck.

After all the frustration from the wrong answers, you decided to quit. You raised your head looking for a volunteer, then noticed a big white screen with some red and green dashes. You put your glasses on and, it is a scoreboard! You didn't know this contest has a scoreboard! Now that you do, you noticed that some problems have been solved by many teams! You decided to focus and try to solve the problem that has been solved by the maximum number of teams.

Input

The first line of input contains a single integer T (1 ≤ T ≤ 53235), the number of test cases.

The first line of each test case contains a single integer k (1 ≤ k < 13), the number of problems your team successfully solved.

The second line contains a string of k distinct uppercase English letters that represent the problems your team solved. The letters are given in the alphabetical order. Each problem from 1 to 13 has a corresponding letter from 'A' to 'M'.

The third line contains 13 integers t1, t2, ..., t13 (0 ≤ ti ≤ 92)ti is the number of teams that solved problem the ith problem.

It is guaranteed that the input is valid, and there's at least one problem not solved by your team and solved by other teams.

Output

For each test case, print an uppercase letter that represents the next problem to be solved, which is the one that has been completed by the maximum number of teams.

It is guaranteed that the answer is unique.

Example
input
3
2
CE
3 0 47 0 76 0 0 2 0 0 0 0 0
7
ADEFHLM
1 1 2 1 1 1 1 1 0 1 1 1 1
2
DG
35 14 40 13 11 18 1 7 41 29 8 19 43
output
A
C
M




水题,模拟。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=55,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int c[maxn];
char s[maxn];
bool a[maxn];

int main() {
	freopen("scoreboard.in","r",stdin);
	int cas;
	scanf("%d",&cas);
	while (cas--) {
		int n,i,ans;
		for (i=0;i<13;i++) a[i]=true;
		scanf("%d",&n);
		scanf("%s",s);
		for (i=0;i<n;i++) {
			a[s[i]-'A']=false;
		} 
		ans=-1;
		for (i=0;i<13;i++) {
			scanf("%d",&c[i]);
			if (a[i]) 
				if (ans==-1) ans=i; else 
					if (c[ans]<c[i]) ans=i;
		}
		printf("%c\n",(char)('A'+ans));
	}
	fclose(stdin);
	return 0;
}

D. Test Cases
time limit per test
2.0 s
memory limit per test
256 MB
input
cases.in
output
standard output

You want to prepare test cases for a problem with the following description:

"Given an array of n positive integers, and a number of queries. Each query will ask about a range [l, r](1 ≤ l ≤ r ≤ n), where each of the values between index l and index r (both inclusive) occurs an even number of times except one value. The answer to each query is the value that occurs an odd number of times in the given range."

You have generated an array of n positive integers, you need to know the number of valid queries you can ask on this array. A query is valid if it has an answer, and that answer is unique.

Input

The first line of input contains a single integer T (1 ≤ T ≤ 64), the number of test cases.

The first line of each test case contains a single integer n (1 ≤ n ≤ 5000), the size of the array.

The next line contains n space-separated integers, a1, a2, ..., an (1 ≤ ai ≤ 106), the values of the array.

The total sum of n overall test cases doesn't exceed 72000.

Output

For each test case, print a single line with the number of valid queries you can ask.

Example
input
1
7
9 1 1 2 1 9 9
output
12

暴力大法好,O(n^2)水过

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=5005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int a[maxn],cnt[1000005];

int main() {
	freopen("cases.in","r",stdin);
	int cas;
	scanf("%d",&cas);
	mem0(cnt);
	while (cas--) {
		int n,i,j,ans=0,o,e;
		scanf("%d",&n);
		for (i=1;i<=n;i++) {
			scanf("%d",&a[i]);
		}
		for (i=1;i<=n;i++) {
			o=e=0;
			for (j=i;j<=n;j++) {
				cnt[a[j]]++;
				if (cnt[a[j]]==1) e++; else 
					if (cnt[a[j]]%2) e++,o--; else e--,o++;
				if (e==1) ans++;
			}
			for (j=i;j<=n;j++) cnt[a[j]]=0;
		}
		printf("%d\n",ans);
	}
	fclose(stdin);
	return 0;
}

G. WiFi Password
time limit per test
2.0 s
memory limit per test
256 MB
input
wifi.in
output
standard output

Just days before the JCPC, your internet service went down. You decided to continue your training at the ACM club at your university. Sadly, you discovered that they have changed the WiFi password. On the router, the following question was mentioned, the answer is the WiFi password padded with zeros as needed.

A subarray [l, r] of an array A is defined as a sequence of consecutive elements Al, Al + 1, ..., Ar, the length of such subarray is r - l + 1. The bitwise OR of the subarray is defined as: Al OR Al + 1 OR ... OR Ar, where OR is the bitwise OR operation (check the notes for details).

Given an array A of n positive integers and an integer v, find the maximum length of a subarray such that the bitwise OR of its elements is less than or equal to v.

Input

The first line contains an integer T (1 ≤ T ≤ 128), where T is the number of test cases.

The first line of each test case contains two space-separated integers n and v (1 ≤ n ≤ 105)(1 ≤ v ≤ 3 × 105).

The second line contains n space-separated integers A1, A2, ..., An (1 ≤ Ai ≤ 2 × 105), the elements of the array.

The sum of n overall test cases does not exceed 106.

Output

For each test case, if no subarray meets the requirement, print 0. Otherwise, print the maximum length of a subarray that meets the requirement.

Example
input
3
5 8
1 4 5 3 1
5 10
8 2 6 1 10
4 2
9 4 5 8
output
5
3
0
Note

To get the value of x OR y, consider both numbers in binary (padded with zeros to make their lengths equal), apply the OR operation on the corresponding bits, and return the result into decimal form. For example, the result of 10 OR 17 = 01010 OR 10001 = 11011 = 27.



把每个数都拆成二进制记录每一位"1"的最晚出现时间,每次把合法区间的右界向右移一格,左界跟着一起移动(如果这时区间的或和超过所给上界),贪心的选择移动最少的方案。复杂度O(nlogn)。

贪心的依据是OR运算的单调不减性

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);
int a[maxn],last[25];

int main() {
	freopen("wifi.in","r",stdin);
	int cas;
	scanf("%d",&cas);
	while (cas--) {
		int n,i,k;
		scanf("%d%d",&n,&k);
		int u=k,p=0,div;
		while (u) {
			u/=2;
			p++;
		}
		div=p-1;
		for (i=0;i<=25;i++) last[i]=0;
		for (i=1;i<=n;i++) {
			scanf("%d",&a[i]);
		}
		int now=0,j=1,ans=0;
		for (i=1;i<=n;i++) {
			if (a[i]>k) {
				now=0;j=i+1;
				continue;
			}
			now=now|a[i];
			u=a[i],p=0;
			while (u) {
				if (u%2) last[p]=i;
				u/=2;
				p++;
			}
			if (now>k) 
		/*	if (last[div]!=i) {
				j=last[div]+1;
				now=0;
				for (int q=0;q<div;q++) {
					if (last[q]>=j) now=now|(1<<q);
				}
			} else */{
				int x=now,w=a[i],dig=inf;
				for (int q=0;q<=div;q++) {
					if (x%2==1&&w%2==0&&(now-(1<<q))<=k) {
						dig=min(dig,last[q]+1);
					}
					x/=2;w/=2;
				}
				if (dig==inf) dig=i;
				j=dig;
				now=0;
				for (int q=0;q<=div;q++) {
					if (last[q]>=j) now=now|(1<<q);
				}
			}
			ans=max(ans,i-j+1);
		}
		printf("%d\n",ans);
	}
	fclose(stdin);
	return 0;
}

M. Winning Cells
time limit per test
1.0 s
memory limit per test
256 MB
input
chess.in
output
standard output

You and your friend decided to play a new game using a squared chessboard of size n × n and one rook. Rows are numbered from 1 to n from top to bottom, and columns are numbered from 1 to n from left to right. Each cell is identified by a pair (r, c) which means that the cell is located at row r and column c.

The rules are simple, you and your friend will take turns, and you will start first. In each turn a player can move the rook at least 1 cell, and at most k cells in only one direction, either up or left, without going outside the chessboard. The player who moves the rook to the top-left cell (1, 1) wins.

You will choose the starting position for the rook. You are not allowed to choose the top-left cell. If you both will play optimally, how many cells can you choose as a starting position to win the game.

Input

The first line of input contains an single integer T (1 ≤ T ≤ 2 × 104), the number of test cases.

Each test case consists of a single line that contains two space-separated integers n and k(1 ≤ k < n ≤ 109), where n is the size of the chessboard, and k is the maximum allowed move.

Output

For each test case, print a single line that contains the number cells you can choose as a starting position to win the game.

Example
input
3
2 1
3 1
9 4
output
2
4
64



画图可以发现,对角线不行,对于n*n方阵相比(n-1)*(n-1)多出来的部分,横竖各每隔k个格子有一个不合法。由此可以推出公式。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int inf=0x3f3f3f3f;  
const ll llinf=0x3f3f3f3f3f3f3f3f;   
const ld pi=acos(-1.0L);

int main() {
	freopen("chess.in","r",stdin);
	int cas;
	scanf("%d",&cas);
	while (cas--) {
		ll n,k,ans;
		scanf("%I64d%I64d",&n,&k);
		if (n==k+1) ans=n*n-n; else {
			ll m=(n-1)/(k+1);
			ll a=m*2+1;
			ans=n*n-(k+1)-(k+1)*(3+a)*m/2;
			if (n%(k+1)!=0) 
				ans+=((k+1)-n%(k+1))*a;
		}
		printf("%I64d\n",ans);
	}
	fclose(stdin);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值