F.小红的矩阵构造
解题思路
- 若
是4的倍数,则可以分4份放在2x2的一个小正方形中
- 是任意行或列异或和为0
- 若
不是4的倍数,由于
,且
为偶数
- 所以
,多出来两个1
- 要使这两个1也消去,则至少还需要4个1(每个所在行要有一个,所在列要有一个)
- 如上图构造,星表示
,
的表格只用用到上图的部分,其他为0
- 特判
,无解
import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;
public class Main{
static long md=(long)998244353;
static long Linf=Long.MAX_VALUE/2;
static int inf=Integer.MAX_VALUE/2;
public static void main(String[] args) throws IOException{
AReader input=new AReader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n=input.nextInt();
int m=input.nextInt();
int x=input.nextInt();
if(x==2) {
out.print("-1");
}else {
int[][] map=new int[n+1][m+1];
int a=0;
if(x%4!=0) {
a=x/4-1;
map[1][1]=a;map[1][2]=a;
map[2][1]=a;map[2][2]=a;
map[2][3]=1;map[2][4]=1;
map[3][1]=1;map[3][3]=1;
map[4][1]=1;map[4][4]=1;
}else {
a=x/4;
map[1][1]=a;map[1][2]=a;
map[2][1]=a;map[2][2]=a;
}
for(int i=1;i<=n;++i) {
for(int j=1;j<=m;++j)out.print(map[i][j]+" ");
out.println();
}
}
out.flush();
out.close();
}
//System.out.println();
//out.println();
static
class AReader{
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public AReader(){
bf=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
bw=new BufferedWriter(new OutputStreamWriter(System.out));
}
public String nextLine() throws IOException{
return bf.readLine();
}
public String next() throws IOException{
while(!st.hasMoreTokens()){
st=new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public char nextChar() throws IOException{
//确定下一个token只有一个字符的时候再用
return next().charAt(0);
}
public int nextInt() throws IOException{
return Integer.parseInt(next());
}
public long nextLong() throws IOException{
return Long.parseLong(next());
}
public double nextDouble() throws IOException{
return Double.parseDouble(next());
}
public float nextFloat() throws IOException{
return Float.parseFloat(next());
}
public byte nextByte() throws IOException{
return Byte.parseByte(next());
}
public short nextShort() throws IOException{
return Short.parseShort(next());
}
public BigInteger nextBigInteger() throws IOException{
return new BigInteger(next());
}
public void println() throws IOException {
bw.newLine();
}
public void println(int[] arr) throws IOException{
for (int value : arr) {
bw.write(value + " ");
}
println();
}
public void println(int l, int r, int[] arr) throws IOException{
for (int i = l; i <= r; i ++) {
bw.write(arr[i] + " ");
}
println();
}
public void println(int a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(int a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(String a) throws IOException{
bw.write(a);
bw.newLine();
}
public void print(String a) throws IOException{
bw.write(a);
}
public void println(long a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(long a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(double a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(double a) throws IOException{
bw.write(String.valueOf(a));
}
public void print(char a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(char a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
}
}
G.小红的元素交换
解题思路
- 对于无序的排列,用最少步骤改为升序,则用n元置换,对每个置换环内进行交换(k阶轮换)
- 本题还加了新的限制,即只有不同颜色才能交换
- 所以若一个k元置换环内有不同颜色,则可以换,所用步数为k-1
- 若一个k元置换环为单色,则只能从外面借一个不同颜色的进行k+1元交换,然后再换回去,所用步数为((k+1)-1)+1=k+1
- 需要注意其实两种颜色可以相互使用,即可以一次处理两个不同单颜色的置换环(k,v)
- 这样只需要(k+v-1)+1=k+v,(多用一步进行调整),而不是(k+1)+(v+1)=k+v+2
- 一次处理两个,至多减少
- 较少的要留下一个到最后与另一种单色直接交换,中间仅借用,即可实现
- 用双色环和一个单色环一同进行交换,共用k+v-1+1=k+v,与分开(k+1)+(v-1)=k+v
- 对于结果没有影响,只影响有无解
- 环大小只有1不用换
- 对于初始只有单颜色环且需要调整的无解
package Tx;
import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;
public class Tx1{
static long md=(long)998244353;
static long Linf=Long.MAX_VALUE/2;
static int inf=Integer.MAX_VALUE/2;
static int[] hscol;
static int[] size;
public static void main(String[] args) throws IOException{
AReader input=new AReader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n=input.nextInt();
boolean need=false;//判断是否需要调整
int[] a=new int[n+1];
for(int i=1;i<=n;++i) {
a[i]=input.nextInt();
if(a[i]!=i)need=true;
}
String string=" "+input.next();
char[] s=string.toCharArray();
hscol=new int[n+1];
size=new int[n+1];
if(!need) {
out.print(0);
out.flush();
out.close();
return;
}
boolean[] vis=new boolean[n+1];
Queue<Integer> q=new LinkedList<Integer>();
Vector<Integer> ww=new Vector<Integer>();//W单色环
Vector<Integer> rr=new Vector<Integer>();//R单色环
Vector<Integer> wr=new Vector<Integer>();//双色环
for(int i=1;i<=n;++i) {
if(vis[i])continue;
size[i]=1;hscol[i]=s[i]=='W'?1:2;
q.add(i);
vis[i]=true;
while(!q.isEmpty()) {
int id=q.peek();
q.poll();
vis[id]=true;
if(s[id]!=s[i])hscol[i]=3;
if(a[id]==i)continue;
size[i]++;
q.add(a[id]);
}
if(hscol[i]==1)ww.add(size[i]);
else if(hscol[i]==2)rr.add(size[i]);
else wr.add(size[i]);
}
int wnum=ww.size();
int rnum=rr.size();
int wrnum=wr.size();
if(wnum==0&&wrnum==0||rnum==0&&wrnum==0) {
out.print("-1");
}else {
int ans=0;
for(int x:ww) {
if(x==1) {//环大小为1,不用换
wnum--;
continue;
}
ans+=x+1;
}
for(int x:rr) {
if(x==1) {
rnum--;
continue;
}
ans+=x+1;
}
for(int x:wr) {
ans+=x-1;
}
int jian=Math.min(wnum, rnum);
ans-=2*jian;
out.print(ans);
}
out.flush();
out.close();
}
//System.out.println();
//out.println();
static
class AReader{
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public AReader(){
bf=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
bw=new BufferedWriter(new OutputStreamWriter(System.out));
}
public String nextLine() throws IOException{
return bf.readLine();
}
public String next() throws IOException{
while(!st.hasMoreTokens()){
st=new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public char nextChar() throws IOException{
//确定下一个token只有一个字符的时候再用
return next().charAt(0);
}
public int nextInt() throws IOException{
return Integer.parseInt(next());
}
public long nextLong() throws IOException{
return Long.parseLong(next());
}
public double nextDouble() throws IOException{
return Double.parseDouble(next());
}
public float nextFloat() throws IOException{
return Float.parseFloat(next());
}
public byte nextByte() throws IOException{
return Byte.parseByte(next());
}
public short nextShort() throws IOException{
return Short.parseShort(next());
}
public BigInteger nextBigInteger() throws IOException{
return new BigInteger(next());
}
public void println() throws IOException {
bw.newLine();
}
public void println(int[] arr) throws IOException{
for (int value : arr) {
bw.write(value + " ");
}
println();
}
public void println(int l, int r, int[] arr) throws IOException{
for (int i = l; i <= r; i ++) {
bw.write(arr[i] + " ");
}
println();
}
public void println(int a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(int a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(String a) throws IOException{
bw.write(a);
bw.newLine();
}
public void print(String a) throws IOException{
bw.write(a);
}
public void println(long a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(long a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(double a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(double a) throws IOException{
bw.write(String.valueOf(a));
}
public void print(char a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(char a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
}
}