AtCoder Beginner Contest 277

AtCoder Beginner Contest 277

A - ^{-1}

Problem Statement

You are given a sequence P P P that is a permutation of ( 1 , 2 , … , N ) (1,2,…,N) (1,2,,N), and an integer X X X. The i i i-th term of P P P has a value of P i P_i Pi. Print k k k such that P k = X P_k = X Pk=X.

Constraints

  • 1 ≤ N ≤ 100 1 \leq N \leq 100 1N100
  • 1 ≤ X ≤ N 1 \leq X \leq N 1XN
  • P P P is a permutation of ( 1 , 2 , … , N ) (1,2,…,N) (1,2,,N).
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N N N X X X P 1 P_1 P1 P 2 P_2 P2 … \ldots P N P_N PN

Output

Print the answer.

Sample Input 1
4 3
2 3 1 4
Sample Output 1
2

We have P = ( 2 , 3 , 1 , 4 ) P = (2,3,1,4) P=(2,3,1,4), so P 2 = 3 P_2 = 3 P2=3. Thus, you should print 2 2 2.

Sample Input 2
5 2
3 5 1 4 2
Sample Output 2
5

Sample Input 3
6 6
1 2 3 4 5 6
Sample Output 3
6

题目大意:

给定一个 1 ∼ n 1 \sim n 1n 的排列 P P P,以及一个数字 X X X 1 ≤ X ≤ n 1 \le X \le n 1Xn)。求排列中,第几位的元素为 X X X

分析:

N N N只有100,直接从前向后查找。

#include<bits/stdc++.h>
using namespace std;
int n,x,a;
int main(){
	cin>>n>>x;
	for(int i=1;i<=n;++i){
		cin>>a;
		if(a==x){
			cout<<i<<endl;
			break;
		}
	}
	return 0;
}

B - Playing Cards Validation

Problem Statement

You are given N N N strings, each of length 2 2 2, consisting of uppercase English letters and digits. The i i i-th string is S i S_i Si.
Determine whether the following three conditions are all satisfied.
・For every string, the first character is one of H, D, C, and S.
・For every string, the second character is one of A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K.
・All strings are pairwise different. That is, if i ≠ j i \neq j i=j, then S i ≠ S j S_i \neq S_j Si=Sj​.

Constraints

  • 1 ≤ N ≤ 52 1 \leq N \leq 52 1N52
  • S i S_i Si is a string of length 2 2 2 consisting of uppercase English letters and digits.

Input

The input is given from Standard Input in the following format:

N N N S 1 S_1 S1 S 2 S_2 S2 ⋮ \vdots S N S_N SN

Output

If the three conditions are all satisfied, print Yes; otherwise, print No.


Sample Input 1
4
H3
DA
D3
SK
Sample Output 1
Yes

One can verify that the three conditions are all satisfied.


Sample Input 2
5
H3
DA
CK
H3
S7
Sample Output 2
No

Both S 1 S_1 S1 and S 4 S_4 S4 are H3, violating the third condition.


Sample Input 3
4
3H
AD
3D
KS
Sample Output 3
No

Sample Input 4
5
00
AA
XX
YY
ZZ
Sample Output 4
No

题目大意:

N N N个长度为 2 2 2的字符串,如果满足:

各字符串第一个字符为H,D,CS,且第二个字符为A 2~9 或T J Q K,并且都不相同。

则输出Yes。否则输出No。

分析:

硬核模拟。

#include<bits/stdc++.h>
using namespace std;
int n,len=0;
string s,st[53];//st数组记录出现过了哪些字符串
int main(){
	cin>>n;
	bool flag=true;
	for(int i=1;i<=n;++i){
		cin>>s;
		for(int j=1;j<=len;++j)
			if(st[j]==s){//重复
				flag=false;
				break;
			}
		if(!flag)
			break;
		char c=s[0];
		if(c=='H'||c=='D'||c=='C'||c=='S'){
			char x=s[1];
			if(!(x=='A'||'2'<=x&&x<='9'||x=='T'||x=='J'||x=='Q'||x=='K'))
				flag=false;//不合题意
		}else//不合题意
			flag=false;
		if(!flag)
			break;
		st[++len]=s;
	}if(flag)
		cout<<"Yes"<<endl;
	else
		cout<<"No"<<endl;
	return 0;
}

C - Ladder Takahashi

Problem Statement

There is a 1 0 9 10^9 109-story building with N N N ladders.
Takahashi, who is on the 1 1 1-st (lowest) floor, wants to reach the highest floor possible by using ladders (possibly none).
The ladders are numbered from 1 1 1 to N N N, and ladder i i i connects the A i A_i Ai-th and B i B_i Bi-th floors. One can use ladder i i i in either direction to move from the A i A_i Ai-th floor to the B i B_i Bi​-th floor or vice versa, but not between other floors.
Takahashi can freely move within the same floor, but cannot move between floors without using ladders.
What is the highest floor Takahashi can reach?

Constraints

  • 1 ≤ N ≤ 2 × 1 0 5 1 \leq N \leq 2 \times 10^5 1N2×105
  • 1 ≤ A i , B i ≤ 1 0 9 1 \leq A_i, B_i \leq 10^9 1Ai,Bi109
  • A i ≠ B i A_i \neq B_i Ai=Bi
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N N N A 1 A_1 A1 B 1 B_1 B1 A 2 A_2 A2 B 2 B_2 B2 … \ldots A N A_N AN B N B_N BN

Output

Print an integer representing the answer.

Sample Input 1
4
1 4
4 3
4 10
8 3
Sample Output 1
10

He can reach the 10 10 10-th floor by using ladder 1 1 1 to get to the 4 4 4-th floor and then ladder 3 3 3 to get to the 10 10 10-th floor.


Sample Input 2
6
1 3
1 5
1 12
3 5
3 12
5 12
Sample Output 2
12

Sample Input 3
3
500000000 600000000
600000000 700000000
700000000 800000000
Sample Output 3
1

He may be unable to move between floors.

题目大意:

有一座很高的楼,Takahashi现在在第一层。

n n n 个梯子,每个梯子连接第 a i a_i ai 层与 b i b_i bi 层。梯子是双向的。

请你求出Takahashi能到达的最高楼层。

分析:

  • 1 ≤ N ≤ 2 × 1 0 5 1 \leq N \leq 2 \times 10^5 1N2×105
  • 1 ≤ A i , B i ≤ 1 0 9 1 \leq A_i, B_i \leq 10^9 1Ai,Bi109
  • A i ≠ B i A_i \neq B_i Ai=Bi

我们可以看到这个数据范围非常的阴间,普通的dp是开不了这么大的,但是N只有2e5,所以可以用离散实现。

#include<bits/stdc++.h>
using namespace std;
int n;
map<int,vector<int> >g;//存图
map<int,int>dp;//first为楼层,second为first楼层能到达的最高层
int dfs(int fl){
	if(g.count(fl)==0)
		return fl;
	if(dp.count(fl)==1)
		return dp[fl];
	int h=fl;
	dp[fl]=fl;//次此处必须把当前楼层开出来,否则遇到1~2~3~1一类的情况会死循环
	for(int i=0;i<g[fl].size();++i)//这里的g[fl].size()被at的编译器警告了
		h=max(h,dfs(g[fl][i]));
	return dp[fl]=h;//记忆化返回
}
int main(){
	cin>>n;
	int a,b;
	for(int i=1;i<=n;++i){
		cin>>a>>b;
		g[a].push_back(b);
		g[b].push_back(a);
	}cout<<dfs(1)<<endl;//输出
	return 0;
}

D - Takahashi’s Solitaire

Problem Statement

Takahashi has N N N cards in his hand. For i = 1 , 2 , … , N i = 1, 2, \ldots, N i=1,2,,N, the i i i-th card has an non-negative integer A i A_i Ai written on it.

First, Takahashi will freely choose a card from his hand and put it on a table. Then, he will repeat the following operation as many times as he wants (possibly zero).

  • Let X X X be the integer written on the last card put on the table. If his hand contains cards with the integer X X X or the integer ( X + 1 )   m o d   M (X+1)\bmod M (X+1)modM written on them, freely choose one of those cards and put it on the table. Here, ( X + 1 )   m o d   M (X+1)\bmod M (X+1)modM denotes the remainder when ( X + 1 ) (X+1) (X+1) is divided by M M M.

Print the smallest possible sum of the integers written on the cards that end up remaining in his hand.

Constraints

  • 1 ≤ N ≤ 2 × 1 0 5 1 \leq N \leq 2 \times 10^5 1N2×105
  • 2 ≤ M ≤ 1 0 9 2 \leq M \leq 10^9 2M109
  • 0 ≤ A i < M 0 \leq A_i \lt M 0Ai<M
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N N N M M M A 1 A_1 A1 A 2 A_2 A2 … \ldots A N A_N AN

Output

Print the answer.


Sample Input 1
9 7
3 0 2 5 5 3 0 6 3
Sample Output 1
11

Assume that he first puts the fourth card ( 5 5 5 is written) on the table and then performs the following.

  • Put the fifth card ( 5 5 5 is written) on the table.
  • Put the eighth card ( 6 6 6 is written) on the table.
  • Put the second card ( 0 0 0 is written) on the table.
  • Put the seventh card ( 0 0 0 is written) on the table.

Then, the first, third, sixth, and ninth cards will end up remaining in his hand, and the sum of the integers on those cards is 3 + 2 + 3 + 3 = 11 3 + 2 + 3 +3 = 11 3+2+3+3=11. This is the minimum possible sum of the integers written on the cards that end up remaining in his hand.


Sample Input 2
1 10
4
Sample Output 2
0

Sample Input 3
20 20
18 16 15 9 8 8 17 1 3 17 11 9 12 11 7 3 2 14 3 12
Sample Output 3
99

题目大意:

给定 n n n 张牌,每张牌上有一个数字 a i a_i ai

你要先选一张牌放在桌子上。假设当前最后一张放置的牌为 X X X,接下来,你每次只能放写着 X X X ( X + 1 )   m o d   M (X + 1) \bmod M (X+1)modM 的牌。

一直操作下去。你需要让你手上剩下的牌的总和最小。

分析:

寻找一个连续自然数列,使得乘积最大,注意判环。

#include<bits/stdc++.h>
using namespace std;
#define MAXN 200010
#define ll long long
int n,m,a[MAXN],ex[MAXN],len=0;
ll dp[MAXN],all=0;//ll
map<int,ll>vh;//second用ll是为了省去下面的强类型转换
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;++i){
		cin>>a[i];
		all+=a[i];//all表示所有数之和
		if(vh.count(a[i])==0)
			ex[++len]=a[i];//ex数组存放所有出现的数字
		++vh[a[i]];//vh存放a[i]出现的次数
	}sort(ex+1,ex+1+len);
	dp[len]=ex[len]*vh[ex[len]];//dp[i]表示在数字ex[i]即之后,连续的ex[i]*vh[ex[i]]之和
	for(int i=len-1;i>=1;--i){
		if(ex[i+1]-ex[i]==1)
			dp[i]=dp[i+1];
		dp[i]+=ex[i]*vh[ex[i]];
	}ll maxx=0;//maxx表示放到桌子上的最大总数
	if(ex[len]==m-1&&ex[1]==0){//首尾相连,m-1的下一个是0
		if(len==m)//1~m都出现了
			maxx=all;
		else{
			dp[len]+=dp[1];
			for(int i=len-1;i>=1;--i){
				if(ex[i+1]-ex[i]!=1)
					break;
				dp[i]+=dp[1];
			}//处理环
		}
	}for(int i=1;i<=len;++i)
		maxx=max(maxx,dp[i]);//在所有dp之中找一个最大值
	cout<<all-maxx<<endl;
	return 0;
}

E - Crystal Switches

Problem Statement

You are given an undirected graph consisting of N N N vertices and M M M edges.
For i = 1 , 2 , … , M i = 1, 2, \ldots, M i=1,2,,M, the i i i-th edge is an undirected edge connecting vertex u i u_i ui and v i v_i vi that is initially passable if a i = 1 a_i = 1 ai=1 and initially impassable if a i = 0 a_i = 0 ai=0. Additionally, there are switches on K K K of the vertices: vertex s 1 s_1 s1, vertex s 2 s_2 s2, … … \ldots… , vertex s K s_K sK.

Takahashi is initially on vertex 1 1 1, and will repeat performing one of the two actions below, Move or Hit Switch, which he may choose each time, as many times as he wants.

  • Move: Choose a vertex adjacent to the vertex he is currently on via an edge, and move to that vertex.
  • Hit Switch: If there is a switch on the vertex he is currently on, hit it. This will invert the passability of every edge in the graph. That is, a passable edge will become impassable, and vice versa.

Determine whether Takahashi can reach vertex N N N, and if he can, print the minimum possible number of times he performs Move before reaching vertex N N N.

Constraints

  • 2   ≤   N   ≤   2   ×   1 0 5 2\ \leq\ N\ \leq\ 2\ \times\ 10^5 2  N  2 × 105
  • 1   ≤   M   ≤   2   ×   1 0 5 1\ \leq\ M\ \leq\ 2\ \times\ 10^5 1  M  2 × 105
  • 0   ≤   K   ≤   N 0\ \leq\ K\ \leq\ N 0  K  N
  • 1   ≤   u i ,   v i   ≤   N 1\ \leq\ u_i,\ v_i\ \leq\ N 1  ui, vi  N
  • u i   ≠   v i u_i\ \neq\ v_i ui = vi
  • a i   ∈   {   0 ,   1 } a_i\ \in\ \lbrace\ 0,\ 1\rbrace ai  { 0, 1}
  • 1   ≤   s 1   <   s 2   <   ⋯   <   s K   ≤   N 1\ \leq\ s_1\ \lt\ s_2\ \lt\ \cdots\ \lt\ s_K\ \leq\ N 1  s1 < s2 <  < sK  N

Input

The input is given from Standard Input in the following format:

N N N M M M K K K u 1 u_1 u1 v 1 v_1 v1 a 1 a_1 a1 u 2 u_2 u2 v 2 v_2 v2 a 2 a_2 a2 ⋮ \vdots u M u_M uM v M v_M vM a M a_M aM s 1 s_1 s1 s 2 s_2 s2 … \ldots s K s_K sK

Output

If Takahashi cannot reach vertex N N N, print − 1 -1 1; if he can, print the minimum possible number of times he performs Move before reaching vertex N N N.


Sample Input 1
5 5 2
1 3 0
2 3 1
5 4 1
2 1 1
1 4 0
3 4
Sample Output 1
5

Takahashi can reach vertex N N N as follows.

  • Move from vertex 1 1 1 to vertex 2 2 2.
  • Move from vertex 2 2 2 to vertex 3 3 3.
  • Hit the switch on vertex 3 3 3. This inverts the passability of every edge in the graph.
  • Move from vertex 3 3 3 to vertex 1 1 1.
  • Move from vertex 1 1 1 to vertex 4 4 4.
  • Hit the switch on vertex 4. This again inverts the passability of every edge in the graph.
  • Move from vertex 4 4 4 to vertex 5 5 5.

Here, Move is performed five times, which is the minimum possible number.


Sample Input 2
4 4 2
4 3 0
1 2 1
1 2 0
2 1 1
2 4
Sample Output 2
-1

The given graph may be disconnected or contain multi-edges. In this sample input, there is no way for Takahashi to reach vertex N N N, so you should print − 1 -1 1.

题目大意:

给定一张 n n n 个点 m m m 条边的无向图。每条边有一个权值 w ∈ { 0 , 1 } w \in \{0, 1\} w{0,1} w = 0 w = 0 w=0 表示这条边无法通过, w = 1 w = 1 w=1 则可以通过。

k k k 个点上面有按钮 s i s_i si

你现在位于 1 1 1 号点。每次,你可以做两件事情中的一件:

  1. 移动。移到相邻的一个点上,注意这条边一定是可以通行的。
  2. 按开关。此时,全部路的边权取反。即: w = 0 w = 0 w=0 变成 1 1 1 w = 1 w = 1 w=1 变成 0 0 0

请问你是否能够到达 n n n 号点。如果可以,求出最少移动次数。

【输入格式】

第一行三个数 n , m , k n, m, k n,m,k

接下来 m m m 行,每行三个数 u i , v i , w i u_i, v_i, w_i ui,vi,wi表示一条连接 u i u_i ui v i v_i vi 的边。

最后一行 k k k 个数,表示按钮的位置。

【输出格式】

如果无法到达,输出 − 1 -1 1。否则输出最少移动次数。

【数据范围】

2 ≤ n ≤ 2 × 1 0 5 2 \le n \le 2 \times 10^5 2n2×105

1 ≤ m ≤ 2 × 1 0 5 1 \le m \le 2 \times 10^5 1m2×105

1 ≤ k ≤ n 1 \le k \le n 1kn

保证 1 ≤ u i , v i ≤ n 1 \le u_i, v_i \le n 1ui,vin,且 u i ≠ v i u_i \ne v_i ui=vi

保证 1 ≤ s 1 < s 2 < ⋯ < s k ≤ n 1 \le s_1 < s_2 < \cdots < s_k \le n 1s1<s2<<skn

分析:

可以看到,按一次按钮,边权取反,这个按钮是两个不同图的通路,则样例1的图相当于是:

A ∼ C = 1 2 ∼ 3 = 1 5 ∼ 4 = 1 2 ∼ 1 = 1 A ∼ D = 1 3 ∼ C = 0 4 ∼ D = 0 A\sim C=1\\ 2\sim3=1\\ 5\sim4=1\\ 2\sim1=1\\ A\sim D=1\\ 3\sim C=0\\ 4\sim D=0 AC=123=154=121=1AD=13C=04D=0

普通SPFA

SPFA会被额外数据hack掉, 但分是给了的

#include<bits/stdc++.h>
using namespace std;
#define MAXN 400010
int n,m,k;
struct nod{
	int to;//邻点
	bool a;//权值
	nod(int _t=0,bool _a=0):to(_t),a(_a){}
};
vector<nod>g[MAXN];//+MAXN/2 是另外一个图(即上文的大写字母部分)
int spfa(int fir,int ned){
	int dis[MAXN];
	bool vh[MAXN];
	queue<int>spf;
	memset(dis,63,sizeof(dis));
	memset(vh,false,sizeof(vh));
	dis[fir]=0;
	spf.push(fir);
	vh[fir]=true;
	while(!spf.empty()){
		int u=spf.front();
		spf.pop();
		vh[u]=false;
		for(int i=0;i<g[u].size();++i){
			int v=g[u][i].to;
			bool a=g[u][i].a;
			if(dis[u]+a<dis[v]){
				dis[v]=dis[u]+a;
				if(!vh[v]){
					spf.push(v);
					vh[v]=true;
				}
			}
		}
	}return min(dis[ned],dis[ned+MAXN/2]);//只要到达N节点即可,不考虑状态
}
int main(){
	cin>>n>>m>>k;
	int u,v,a;
	for(int i=1;i<=m;++i){
		cin>>u>>v>>a;
		if(a==1){
			g[u].push_back(nod(v,1));
			g[v].push_back(nod(u,1));
		}else{
			g[u+MAXN/2].push_back(nod(v+MAXN/2,1));
			g[v+MAXN/2].push_back(nod(u+MAXN/2,1));
		}//存图
	}for(int i=1;i<=k;++i){
		cin>>u;
		g[u].push_back(nod(u+MAXN/2,0));
		g[u+MAXN/2].push_back(nod(u,0));
	}int x=spfa(1,n);
	if(x==1061109567)//这是memset后的结果,没变则表示无法到达
		cout<<-1<<endl;
	else
		cout<<x<<endl;
	return 0;
}
堆优化SPFA

每次选择dis值最小的那个节点扩展,跟dijkstra类似。 (别问为什么不写dijk,问就是没调出来)

#include<bits/stdc++.h>
using namespace std;
#define MAXN 400010
int n,m,k,dis[MAXN];
struct nod{
	int to;
	bool a;
	nod(int _t=0,bool _a=0):to(_t),a(_a){}
};
vector<nod>g[MAXN];
struct cmp{
	bool operator()(int a,int b){
		return dis[a]>dis[b]||dis[a]==dis[b]&&a<b;
	}
};//这里的小根堆是问同学的,应该算是板子
int spfa(int fir,int ned){
	bool vh[MAXN];
	priority_queue<int,vector<int>,cmp>spf;
	memset(dis,63,sizeof(dis));
	memset(vh,false,sizeof(vh));
	dis[fir]=0;
	spf.push(fir);
	vh[fir]=true;
	while(!spf.empty()){
		int u=spf.top();//每次取dis值最小的一个扩展,扩展得到的dis值一定是最优的,只入一次队
		spf.pop();
		vh[u]=false;
		for(int i=0;i<g[u].size();++i){
			int v=g[u][i].to;
			bool a=g[u][i].a;
			if(dis[u]+a<dis[v]){
				dis[v]=dis[u]+a;
				if(!vh[v]){
					spf.push(v);
					vh[v]=true;
				}
			}
		}
	}return min(dis[ned],dis[ned+MAXN/2]);
}
int main(){
	cin>>n>>m>>k;
	int u,v,a;
	for(int i=1;i<=m;++i){
		cin>>u>>v>>a;
		if(a==1){
			g[u].push_back(nod(v,1));
			g[v].push_back(nod(u,1));
		}else{
			g[u+MAXN/2].push_back(nod(v+MAXN/2,1));
			g[v+MAXN/2].push_back(nod(u+MAXN/2,1));
		}
	}for(int i=1;i<=k;++i){
		cin>>u;
		g[u].push_back(nod(u+MAXN/2,0));
		g[u+MAXN/2].push_back(nod(u,0));
	}int x=spfa(1,n);
	if(x==1061109567)
		cout<<-1<<endl;
	else
		cout<<x<<endl;
	return 0;
}

F - Sorting a Matrix

Problem Statement

You are given a matrix A A A whose elements are non-negative integers. For a pair of integers ( i , j ) (i,j) (i,j) such that 1 ≤ i ≤ H 1≤i≤H 1iH and 1 ≤ j ≤ W 1≤j≤W 1jW, let A i , j A _{i,j} Ai,j denote the element at the i i i-th row and j j j-th column of A A A.

Let us perform the following procedure on A A A.

  • First, replace each element of A A A that is 0 0 0 with an arbitrary positive integer (if multiple elements are 0 0 0, they may be replaced with different positive integers).
  • Then, repeat performing one of the two operations below, which may be chosen each time, as many times as desired (possibly zero).
    • Choose a pair of integers ( i , j ) (i, j) (i,j) such that 1 ≤ i < j ≤ H 1 \leq i \lt j \leq H 1i<jH and swap the i i i-th and j j j-th rows of A A A.
    • Choose a pair of integers ( i , j ) (i, j) (i,j) such that 1 ≤ i < j ≤ W 1 \leq i \lt j \leq W 1i<jW and swap the i i i-th and j j j-th columns of A A A.

Determine whether A A A can be made to satisfy the following condition.

  • A 1 ,   1   ≤   A 1 ,   2   ≤   ⋯   ≤   A 1 ,   W   ≤   A 2 ,   1   ≤   A 2 ,   2   ≤   ⋯   ≤   A 2 ,   W   ≤   A 3 ,   1   ≤   ⋯   ≤   A H ,   1   ≤   A H ,   2   ≤   ⋯   ≤   A H ,   W A_{1,\ 1}\ \leq\ A_{1,\ 2}\ \leq\ \cdots\ \leq\ A_{1,\ W}\ \leq\ A_{2,\ 1}\ \leq\ A_{2,\ 2}\ \leq\ \cdots\ \leq\ A_{2,\ W}\ \leq\ A_{3,\ 1}\ \leq\ \cdots\ \leq\ A_{H,\ 1}\ \leq\ A_{H,\ 2}\ \leq\ \cdots\ \leq\ A_{H,\ W} A1, 1  A1, 2    A1, W  A2, 1  A2, 2    A2, W  A3, 1    AH, 1  AH, 2    AH, W

  • In other words, for every two pairs of integers ( i , j ) (i, j) (i,j) and ( i ′ , j ′ ) (i', j') (i,j) such that 1 ≤ i , i ′ ≤ H 1 \leq i, i' \leq H 1i,iH and 1 ≤ j , j ′ ≤ W 1 \leq j, j' \leq W 1j,jW, both of the following conditions are satisfied.

    • If i   <   i ′ i\ \lt\ i' i < i ,then A i ,   j   ≤   A i ′ ,   j ′ A_{i,\ j}\ \leq\ A_{i',\ j'} Ai, j  Ai, j
      • If i   =   i ′ i\ =\ i' i = i and j   <   j ′ j\ \lt\ j' j < j ,then A i ,   j   ≤   A i ′ ,   j ′ A_{i,\ j}\ \leq\ A_{i',\ j'} Ai, j  Ai, j

Constraints

  • 2   ≤   H ,   W 2\ \leq\ H,\ W 2  H, W
  • H   ×   W   ≤   1 0 6 H\ \times\ W\ \leq\ 10^6 H × W  106
  • 0   ≤   A i ,   j   ≤   H   ×   W 0\ \leq\ A_{i,\ j}\ \leq\ H\ \times\ W 0  Ai, j  H × W
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

H H H W W W A 1 ,   1 A_{1,\ 1} A1, 1 A 1 ,   2 A_{1,\ 2} A1, 2 … \ldots A 1 ,   W A_{1,\ W} A1, W A 2 ,   1 A_{2,\ 1} A2, 1 A 2 ,   2 A_{2,\ 2} A2, 2 … \ldots A 2 ,   W A_{2,\ W} A2, W ⋮ \vdots A H ,   1 A_{H,\ 1} AH, 1 A H ,   2 A_{H,\ 2} AH, 2 … \ldots A H ,   W A_{H,\ W} AH, W

Output

If A A A can be made to satisfy the condition in the problem statement, print Yes; otherwise, print No.


Sample Input 1
3 3
9 6 0
0 4 0
3 0 3
Sample Output 1
Yes

One can perform the operations as follows to make A A A satisfy the condition in the problem statement, so you should print Yes.

  • First, replace the elements of A A A that are 00, as shown below:
9 6 8
5 4 4
3 1 3
  • Swap the second and third columns. Then, A A A becomes:
9 8 6
5 4 4
3 3 1
  • Swap the first and third rows. Then, A A A becomes:
3 3 1
5 4 4
9 8 6
  • Swap the first and third columns. Then, A A A becomes the following and satisfies the condition in the problem statement.
1 3 3
4 4 5
6 8 9

Sample Input 2
2 2
2 1
1 2
Sample Output 2
No

There is no way to perform the operations to make A A A satisfy the condition in the problem statement, so you should print No.

题目大意:

给一个 H H H W W W列的矩阵 A A A,所有元素都是自然数,可以把 0 0 0改成任意正整数,也可以交换任意次任意行任意列,问是否有一种做法能使得 A 1 ,   1   ≤   A 1 ,   2   ≤   ⋯   ≤   A 1 ,   W   ≤   A 2 ,   1   ≤   A 2 ,   2   ≤   ⋯   ≤   A 2 ,   W   ≤   A 3 ,   1   ≤   ⋯   ≤   A H ,   1   ≤   A H ,   2   ≤   ⋯   ≤   A H ,   W A_{1,\ 1}\ \leq\ A_{1,\ 2}\ \leq\ \cdots\ \leq\ A_{1,\ W}\ \leq\ A_{2,\ 1}\ \leq\ A_{2,\ 2}\ \leq\ \cdots\ \leq\ A_{2,\ W}\ \leq\ A_{3,\ 1}\ \leq\ \cdots\ \leq\ A_{H,\ 1}\ \leq\ A_{H,\ 2}\ \leq\ \cdots\ \leq\ A_{H,\ W} A1, 1  A1, 2    A1, W  A2, 1  A2, 2    A2, W  A3, 1    AH, 1  AH, 2    AH, W,能则输出Yes,否则输出No

分析:

骗分算法:

输出Yes有34个点,No有37个点

错误算法:

运用必要且不充分条件。

单看行,假设每一行的每个元素都是可以任意交换而不影响其他行的,那么有以下进阶骗分代码。

实测有61个点AC,10个点WA。

#include<bits/stdc++.h>
using namespace std;
#define MAXN 1000010
int h,w;
struct nod{
	int b=-1,s=INT_MAX;
}a[MAXN];
bool cmp(nod a,nod b){
	return a.s<b.s||a.s==b.s&&a.b<b.b;
}
int main(){
	cin>>h>>w;
	int el;
	for(int i=1;i<=h;++i){
		for(int j=1;j<=w;++j){
			cin>>el;
			if(el==0)
				continue;
			a[i].b=max(a[i].b,el);
			a[i].s=min(a[i].s,el);
		}if(a[i].b==-1)
			a[i].s=0,a[i].b=0;//找每一行的最大最小值
	}sort(a+1,a+1+h,cmp);//按最小值升序排序
	bool flag=true;
	for(int i=2;i<=h;++i)
		if(a[i-1].b>a[i].s){//如果上一行的最大值比下一行的最小值大,那么不可能
			flag=false;
			break;
		}
	if(flag)
		cout<<"Yes"<<endl;
	else
		cout<<"No"<<endl;
	return 0;
}
标算不会(

标算看蒙了(((

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值