codeforces 676 A到D题题解 A - XORwice/B - Putting Bricks in the Wall/C - Palindromifier/D - Hexagons

A - XORwice
In order to celebrate Twice’s 5th anniversary, Tzuyu and Sana decided to play a game.

Tzuyu gave Sana two integers a and b and a really important quest.

In order to complete the quest, Sana has to output the smallest possible value of (a⊕x) + (b⊕x) for any given x, where ⊕ denotes the bitwise XOR operation.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). Description of the test cases follows.

The only line of each test case contains two integers a and b (1≤a,b≤109).

Output
For each testcase, output the smallest possible value of the given expression.

Example
inputCopy
6
6 12
4 9
59 832
28 14
4925 2912
1 1
outputCopy
10
13
891
18
6237
0
Note
For the first test case Sana can choose x=4 and the value will be (6⊕4) + (12⊕4) = 2+8 = 10. It can be shown that this is the smallest possible value.

题意
x为任意值,求 (a⊕x) + (b⊕x) 最小值

思路
a,b对应位相同时,对应位可化为0;当a,b对应位不相同时,对应位必定为1,答案就是a与b的异或。

代码

#include <bits/stdc++.h>
typedef unsigned long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {
    inline void input(int& res) {
        char c = getchar();res = 0;int f = 1;
        while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
        while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
        res = f ? res : -res;
    }
    inline ll qpow(ll a, ll b) {
        ll ans = 1, base = a;
        while (b) {
            if (b & 1) ans = (ans * base % mod +mod )%mod;
            base = (base * base % mod + mod)%mod;
            b >>= 1;
        }
        return ans;
    }
}
using namespace fastIO;
const int N = 3e5+5;
int Case,a,b,n; 
int main(){
	Case=1;
	input(Case);
	while(Case--){
		input(a),input(b);
		printf("%d\n",a^b);
	}
	return 0;
}

B - Putting Bricks in the Wall
Pink Floyd are pulling a prank on Roger Waters. They know he doesn’t like walls, he wants to be able to walk freely, so they are blocking him from exiting his room which can be seen as a grid.

Roger Waters has a square grid of size n×n and he wants to traverse his grid from the upper left (1,1) corner to the lower right corner (n,n). Waters can move from a square to any other square adjacent by a side, as long as he is still in the grid. Also except for the cells (1,1) and (n,n) every cell has a value 0 or 1 in it.

Before starting his traversal he will pick either a 0 or a 1 and will be able to only go to cells values in which are equal to the digit he chose. The starting and finishing cells (1,1) and (n,n) are exempt from this rule, he may go through them regardless of picked digit. Because of this the cell (1,1) takes value the letter ‘S’ and the cell (n,n) takes value the letter ‘F’.

For example, in the first example test case, he can go from (1,1) to (n,n) by using the zeroes on this path: (1,1), (2,1), (2,2), (2,3), (3,3), (3,4), (4,4)

The rest of the band (Pink Floyd) wants Waters to not be able to do his traversal, so while he is not looking they will invert at most two cells in the grid (from 0 to 1 or vice versa). They are afraid they will not be quick enough and asked for your help in choosing the cells. Note that you cannot invert cells (1,1) and (n,n).

We can show that there always exists a solution for the given constraints.

Also note that Waters will pick his digit of the traversal after the band has changed his grid, so he must not be able to reach (n,n) no matter what digit he picks.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤50). Description of the test cases follows.

The first line of each test case contains one integers n (3≤n≤200).

The following n lines of each test case contain the binary grid, square (1,1) being colored in ‘S’ and square (n,n) being colored in ‘F’.

The sum of values of n doesn’t exceed 200.

Output
For each test case output on the first line an integer c (0≤c≤2) — the number of inverted cells.

In i-th of the following c lines, print the coordinates of the i-th cell you inverted. You may not invert the same cell twice. Note that you cannot invert cells (1,1) and (n,n).

Example
inputCopy
3
4
S010
0001
1000
111F
3
S10
101
01F
5
S0101
00000
01111
11111
0001F
outputCopy
1
3 4
2
1 2
2 1
0
Note
For the first test case, after inverting the cell, we get the following grid:

S010
0001
1001
111F

题意
一个地图除了起点(1,1)和终点(n,n),其他点非0即1,规定只能走数字一样的点,现在问你操作2次以内,使起点到不了终点。

思路
起点附近就两个点,终点也是,就4个点,分类讨论下进行。

代码

#include <bits/stdc++.h>
typedef unsigned long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {
    inline void input(int& res) {
        char c = getchar();res = 0;int f = 1;
        while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
        while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
        res = f ? res : -res;
    }
    inline ll qpow(ll a, ll b) {
        ll ans = 1, base = a;
        while (b) {
            if (b & 1) ans = (ans * base % mod +mod )%mod;
            base = (base * base % mod + mod)%mod;
            b >>= 1;
        }
        return ans;
    }
}
using namespace fastIO;
const int N = 3e5+5;
int Case,a,b,n; 
char s[205][205];
int main(){
	Case=1;
	input(Case);
	while(Case--){
		input(n);
		for(int i=1;i<=n;i++){
			scanf("%s",s[i]+1);
		}
		if(s[1][2]==s[2][1]&&s[n-1][n]==s[n][n-1]&&s[1][2]!=s[n-1][n]) puts("0");
		else if(s[1][2]==s[2][1]&&s[n-1][n]==s[n][n-1]&&s[1][2]==s[n-1][n]){
			puts("2");
			puts("1 2");
			puts("2 1");
		}
		else if(s[1][2]!=s[2][1]&&s[n-1][n]!=s[n][n-1]){
			puts("2");
			if(s[1][2]=='0'&&s[n-1][n]=='1')  puts("1 2"),printf("%d %d\n",n-1,n);
			else if(s[2][1]=='0'&&s[n-1][n]=='1') puts("2 1"),printf("%d %d\n",n-1,n);
			else if(s[1][2]=='0'&&s[n][n-1]=='1') puts("1 2"),printf("%d %d\n",n,n-1);
			else puts("2 1"),printf("%d %d\n",n,n-1);
		}
		else{
			puts("1");
			if(s[1][2]==s[2][1]){
				if(s[1][2]=='0'){
					if(s[n][n-1]=='0')
						printf("%d %d\n",n,n-1);
					else
						printf("%d %d\n",n-1,n);
				}
				else{
					if(s[n][n-1]=='1')
						printf("%d %d\n",n,n-1);
					else
						printf("%d %d\n",n-1,n);
				}
			}
			else{
				if(s[n][n-1]=='0'){
					if(s[1][2]=='0')
						puts("1 2");
					else
						puts("2 1");
				}
				else{
					if(s[1][2]=='1')
						puts("1 2");
					else
						puts("2 1");
				}
			}
		}
	}
	return 0;
}

C - Palindromifier
Ringo found a string s of length n in his yellow submarine. The string contains only lowercase letters from the English alphabet. As Ringo and his friends love palindromes, he would like to turn the string s into a palindrome by applying two types of operations to the string.

The first operation allows him to choose i (2≤i≤n−1) and to append the substring s2s3…si (i−1 characters) reversed to the front of s.

The second operation allows him to choose i (2≤i≤n−1) and to append the substring sisi+1…sn−1 (n−i characters) reversed to the end of s.

Note that characters in the string in this problem are indexed from 1.

For example suppose s=abcdef. If he performs the first operation with i=3 then he appends cb to the front of s and the result will be cbabcdef. Performing the second operation on the resulted string with i=5 will yield cbabcdefedc.

Your task is to help Ringo make the entire string a palindrome by applying any of the two operations (in total) at most 30 times. The length of the resulting palindrome must not exceed 106

It is guaranteed that under these constraints there always is a solution. Also note you do not have to minimize neither the number of operations applied, nor the length of the resulting string, but they have to fit into the constraints.

Input
The only line contains the string S (3≤|s|≤105) of lowercase letters from the English alphabet.

Output
The first line should contain k (0≤k≤30) — the number of operations performed.

Each of the following k lines should describe an operation in form L i or R i. L represents the first operation, R represents the second operation, i represents the index chosen.

The length of the resulting palindrome must not exceed 106.

Examples
inputCopy
abac
outputCopy
2
R 2
R 5
inputCopy
acccc
outputCopy
2
L 4
L 2
inputCopy
hannah
outputCopy
0
Note
For the first example the following operations are performed:

abac → abacab → abacaba

The second sample performs the following operations: acccc → cccacccc → ccccacccc

The third example is already a palindrome so no operations are required.

题意
有两种操作 L i 操作为 s2 到 si 倒转插入开头,R i 为 si 到 sn-1倒置插入结尾,i小于n,最多30次操作,使字符串成为一个回文串。

思路
我们先设字符串为abcde
我们先进行 L len-1变为 dcbabcde
我们自然会进行 R len-1 dcbabcdedcba
这时候我们思考怎么成为回文串,先考虑L 发现不可行,再考虑R 发现我们可以将dcb倒置后置 即R 2len-1,十分巧妙地找到了。

代码

#include <bits/stdc++.h>
typedef unsigned long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {
    inline void input(int& res) {
        char c = getchar();res = 0;int f = 1;
        while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
        while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
        res = f ? res : -res;
    }
    inline ll qpow(ll a, ll b) {
        ll ans = 1, base = a;
        while (b) {
            if (b & 1) ans = (ans * base % mod +mod )%mod;
            base = (base * base % mod + mod)%mod;
            b >>= 1;
        }
        return ans;
    }
}
using namespace fastIO;
const int N = 3e5+5;
int Case,len;
string str; 
int main(){
	Case=1;
	cin>>str;
	len=str.size();
	puts("3");
	printf("L %d\nR %d\nR %d\n",len-1,len-1,2*len-1);
	return 0;
}

D - Hexagons
Lindsey Buckingham told Stevie Nicks “Go your own way”. Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.

Consider a hexagonal tiling of the plane as on the picture below.
在这里插入图片描述

Nicks wishes to go from the cell marked (0,0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0,0) to (1,1) will take the exact same cost as going from (−2,−1) to (−1,0). The costs are given in the input in the order c1, c2, c3, c4, c5, c6 as in the picture below.

在这里插入图片描述

Print the smallest cost of a path from the origin which has coordinates (0,0) to the given cell.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). Description of the test cases follows.

The first line of each test case contains two integers x and y (−109≤x,y≤109) representing the coordinates of the target hexagon.

The second line of each test case contains six integers c1, c2, c3, c4, c5, c6 (1≤c1,c2,c3,c4,c5,c6≤109) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).

Output
For each testcase output the smallest cost of a path from the origin to the given cell.

Example
inputCopy
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
outputCopy
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c3 3 times and c2 once, amounting to 5+5+5+3=18.
题意
六边形蜂窝状,六个方向代价大小不同,求从(0,0)走到(x,y)最小代价。
思路
贪心加向量分解求出来每个方向的最小花费
代码

#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {
    inline void input(ll& res) {
        char c = getchar();res = 0;int f = 1;
        while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
        while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
        res = f ? res : -res;
    }
    inline ll qpow(ll a, ll b) {
        ll ans = 1, base = a;
        while (b) {
            if (b & 1) ans = (ans * base % mod +mod )%mod;
            base = (base * base % mod + mod)%mod;
            b >>= 1;
        }
        return ans;
    }
}
using namespace fastIO;
const int N = 3e5+5;
ll Case,x,y,ans,y1,y2;
ll c[10];
int main(){
	Case=1;
	input(Case);
	while(Case--){
		input(x),input(y);
		for(int i=1;i<=6;i++) input(c[i]);
		c[1]=min(c[1],c[2]+c[6]);
		c[2]=min(c[2],c[1]+c[3]);
		c[3]=min(c[3],c[2]+c[4]);
		c[4]=min(c[4],c[3]+c[5]);
		c[5]=min(c[5],c[4]+c[6]);
		c[6]=min(c[6],c[1]+c[5]);
		if(x>=0&&y>=0)
			if(x>y) ans=c[1]*abs(y)+c[6]*abs(x-y);
			else ans=c[1]*abs(x)+c[2]*abs(y-x);
		else if(x<=0&&y<=0)
			if(x>y) ans=c[4]*abs(x)+c[5]*abs(x-y);
			else ans=c[4]*abs(y)+c[3]*abs(y-x);
		else if(x<=0&&y>=0)
			ans=c[3]*abs(x)+c[2]*abs(y);
		else
			ans=c[6]*abs(x)+c[5]*abs(y); 
		printf("%lld\n",ans); 
	}
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值