Codeforces Round #501 (Div. 3)

A. Points in Segments

Description:

You are given a set of n n n segments on the axis O x Ox Ox, each segment has integer endpoints between 1 1 1 and m m m inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is characterized by two integers l i l_i li and r i r_i ri ( 1 l i r i m 1 \le l_i \le r_i \le m 1lirim) — coordinates of the left and of the right endpoints.
Consider all integer points between 1 1 1 and m m m inclusive. Your task is to print all such points that don’t belong to any segment. The point x x x belongs to the segment [ l ; r ] [l; r] [l;r] if and only if l x r l \le x \le r lxr.

Input:

The first line of the input contains two integers n n n and m m m ( 1 n , m 100 1 \le n, m \le 100 1n,m100) — the number of segments and the upper bound for coordinates.
The next n n n lines contain two integers each l i l_i li and r i r_i ri ( 1 l i r i m 1 \le l_i \le r_i \le m 1lirim) — the endpoints of the i i i-th segment. Segments may intersect, overlap or even coincide with each other. Note, it is possible that l i = r i l_i=r_i li=ri, i.e. a segment can degenerate to a point.

Output

In the first line print one integer k k k — the number of points that don’t belong to any segment.
In the second line print exactly k k k integers in any order — the points that don’t belong to any segment. All points you print should be distinct.
If there are no such points at all, print a single integer 0 0 0 in the first line and either leave the second line empty or do not print it at all.

Sample Input:

3 5
2 2
1 2
5 5

Sample Output:

2
3 4

Sample Input:

1 7
1 7

Sample Output:

0

题目链接

给出几组区间,求不在区间内的所有点。

直接用一个数组标记记录即可。

最开始读题以为要输出所有区间,一直WA…

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x) {
	Finish_read = 1;
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') {
			f = -1;
		}
		if (ch == EOF) {
			return;
		}
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	x *= f;
	Finish_read = 1;
};

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int n, m;
	read(n); read(m);
	vector<int> dot(m + 1, 0);
	for (int i = 0, l, r; i < n; ++i) {
		read(l); read(r);
		for (int i = l; i <= r; ++i) {
			if (!dot[i]) {
				dot[i] = 1;
			}
		}
	}
	vector<int> ans;
	for (int i = 1; i <= m; ++i) {
		if (!dot[i]) {
			ans.pb(i);
		}
	}
	printf("%d\n", int(ans.size()));
	for (auto i : ans) {
		printf("%d ", i);
	}
	printf("\n");
#ifndef ONLINE_JUDGE
    fclose(stdin);
	fclose(stdout);
	system("gedit out.txt");
#endif
    return 0;
}

B. Obtaining the String

Description:

You are given two strings s s s and t t t. Both strings have length n n n and consist of lowercase Latin letters. The characters in the strings are numbered from 1 1 1 to n n n.
You can successively perform the following move any number of times (possibly, zero):
swap any two adjacent (neighboring) characters of s s s (i.e. for any i = { 1 , 2 , , n 1 } i = \{1, 2, \dots, n - 1\} i={1,2,,n1} you can swap s i s_i si and s i + 1 ) s_{i + 1}) si+1). You can’t apply a move to the string t t t. The moves are applied to the string s s s one after another.
Your task is to obtain the string t t t from the string s s s. Find any way to do it with at most 1 0 4 10^4 104 such moves.
You do not have to minimize the number of moves, just find any sequence of moves of length 1 0 4 10^4 104 or less to transform s s s into t t t.

Input:

The first line of the input contains one integer n n n ( 1 n 50 1 \le n \le 50 1n50) — the length of strings s s s and t t t.
The second line of the input contains the string s s s consisting of n n n lowercase Latin letters.
The third line of the input contains the string t t t consisting of n n n lowercase Latin letters.

Output

If it is impossible to obtain the string t t t using moves, print “-1”.
Otherwise in the first line print one integer k k k — the number of moves to transform s s s to t t t. Note that k k k must be an integer number between 0 0 0 and 1 0 4 10^4 104 inclusive.
In the second line print k k k integers c j c_j cj ( 1 c j &lt; n 1 \le c_j &lt; n 1cj<n), where c j c_j cj means that on the j j j-th move you swap characters s c j s_{c_j} scj and s c j + 1 s_{c_j + 1} scj+1.
If you do not need to apply any moves, print a single integer 0 0 0 in the first line and either leave the second line empty or do not print it at all.

Sample Input:

6
abcdef
abdfec

Sample Output:

4
3 5 4 5

Sample Input:

4
abcd
accd

Sample Output:

-1

题目链接

求第一个字符串相邻字符如何交换可以与第二个字符串相同。

先判断两个字符串内每个字符个数是否都相同,再在第二个字符串中从后往前找两个字符串中不同的字符进行交换。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x) {
	Finish_read = 1;
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') {
			f = -1;
		}
		if (ch == EOF) {
			return;
		}
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	x *= f;
	Finish_read = 1;
};

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int n;
	read(n);
	string s, t;
	cin >> s >> t;
	for (int i = 0; i < 26; ++i) {
		if (count(s.begin(), s.end(), char('a' + i)) != count(t.begin(), t.end(), char('a' + i))) {
			printf("-1\n");
			return 0;
		}
	}
	vector<int> ans;
	for (int i = n - 1; i >= 0; --i) {
		if (s[i] != t[i]) {
			int index = 0;
			for (int j = i - 1; j >= 0; --j) {
				if (s[j] == t[i]) {
					index = j;
					break;
				}
			}
			for (int j = index; j < i; ++j) {
				swap(s[j], s[j + 1]);
				ans.pb(j + 1);
			}
		}
	}
	printf("%d\n", int(ans.size()));
	for (auto i : ans) {
		printf("%d ", i);
	}
	printf("\n");
#ifndef ONLINE_JUDGE
    fclose(stdin);
	fclose(stdout);
	system("gedit out.txt");
#endif
    return 0;
}

C. Songs Compression

Description:

Ivan has n n n songs on his phone. The size of the i i i-th song is a i a_i ai bytes. Ivan also has a flash drive which can hold at most m m m bytes in total. Initially, his flash drive is empty.
Ivan wants to copy all n n n songs to the flash drive. He can compress the songs. If he compresses the i i i-th song, the size of the i i i-th song reduces from a i a_i ai to b i b_i bi bytes ( b i &lt; a i b_i &lt; a_i bi<ai).
Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most m m m. He can compress any subset of the songs (not necessarily contiguous).
Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to m m m).
If it is impossible to copy all the songs (even if Ivan compresses all the songs), print “-1”. Otherwise print the minimum number of songs Ivan needs to compress.

Input:

The first line of the input contains two integers n n n and m m m ( 1 n 1 0 5 , 1 m 1 0 9 1 \le n \le 10^5, 1 \le m \le 10^9 1n105,1m109) — the number of the songs on Ivan’s phone and the capacity of Ivan’s flash drive.
The next n n n lines contain two integers each: the i i i-th line contains two integers a i a_i ai and b i b_i bi ( 1 a i , b i 1 0 9 1 \le a_i, b_i \le 10^9 1ai,bi109, a i &gt; b i a_i &gt; b_i ai>bi) — the initial size of the i i i-th song and the size of the i i i-th song after compression.

Output

If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print “-1”. Otherwise print the minimum number of the songs to compress.

Sample Input:

4 21
10 8
7 4
3 1
5 4

Sample Output:

2

Sample Input:

4 16
10 8
7 4
3 1
5 4

Sample Output:

-1

题目链接

每首歌有a大小,压缩后是b大小,求最少压缩几首歌可以达到要求。

按照压缩变化大小(a-b)降序排序,优先压缩变化大的歌曲,直到达到要求。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x) {
	Finish_read = 1;
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') {
			f = -1;
		}
		if (ch == EOF) {
			return;
		}
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	x *= f;
	Finish_read = 1;
};

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int n, m;
	read(n); read(m);
	ll sum = 0, ans = 0;
	int cnt = 0;
	vector<ll> c;
	for (int i = 0, a, b; i < n; ++i) {
		read(a); read(b);
		ans += a;
		sum += b;
		c.pb(a - b);
	}
	if (sum > m) {
		printf("-1\n");
	}
	else {
		sort(c.begin(), c.end());
		reverse(c.begin(), c.end());
		for (int i = 0; i < n && ans > m; ++i) {
			ans -= c[i];
			cnt++;
		}
		printf("%d\n", cnt);
	}
#ifndef ONLINE_JUDGE
    fclose(stdin);
	fclose(stdout);
	system("gedit out.txt");
#endif
    return 0;
}

D. Walking Between Houses

Description:

There are n n n houses in a row. They are numbered from 1 1 1 to n n n in order from left to right. Initially you are in the house 1 1 1.
You have to perform k k k moves to other house. In one move you go from your current house to some other house. You can’t stay where you are (i.e., in each move the new house differs from the current house). If you go from the house x x x to the house y y y, the total distance you walked increases by x y |x-y| xy units of distance, where a |a| a is the absolute value of a a a. It is possible to visit the same house multiple times (but you can’t visit the same house in sequence).
Your goal is to walk exactly s s s units of distance in total.
If it is impossible, print “NO”. Otherwise print “YES” and any of the ways to do that. Remember that you should do exactly k k k moves.

Input:

The first line of the input contains three integers n n n, k k k, s s s ( 2 n 1 0 9 2 \le n \le 10^9 2n109, 1 k 2 1 0 5 1 \le k \le 2 \cdot 10^5 1k2105, 1 s 1 0 18 1 \le s \le 10^{18} 1s1018) — the number of houses, the number of moves and the total distance you want to walk.

Output

If you cannot perform k k k moves with total walking distance equal to s s s, print “NO”.
Otherwise print “YES” on the first line and then print exactly k k k integers h i h_i hi ( 1 h i n 1 \le h_i \le n 1hin) on the second line, where h i h_i hi is the house you visit on the i i i-th move.
For each j j j from 1 1 1 to k 1 k-1 k1 the following condition should be satisfied: h j h j + 1 h_j \ne h_{j + 1} hj̸=hj+1. Also h 1 1 h_1 \ne 1 h1̸=1 should be satisfied.

Sample Input:

10 2 15

Sample Output:

YES
10 4

Sample Input:

10 9 45

Sample Output:

YES
10 1 10 1 2 1 2 1 6

Sample Input:

10 9 81

Sample Output:

YES
10 1 10 1 10 1 10 1 10

Sample Input:

10 9 82

Sample Output:

NO

题目链接

有1~n栋房子,起始位置是1,输出进过k次移动正好移动s距离的移动过程。

最大可以移动n-1距离,从n-1遍历到1,优先移动远距离,最后用1进行剩下的移动即可。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x) {
	Finish_read = 0;
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') {
			f = -1;
		}
		if (ch == EOF) {
			return;
		}
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	x *= f;
	Finish_read = 1;
};

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	ll n, k, s;
	read(n); read(k); read(s);
	vector<ll> ans;
	ans.pb(1);
	if (k > s || (n - 1) * k < s) {
		printf("NO\n");
		return 0;
	}
	for (int i = n - 1; i > 0; --i) {
		while (s - i >= k - 1 && k > 0) {
			if (ans.back() + i <= n) {
				ans.pb(ans.back() + i);
				s -= i;
				k--;
			}
			else if (ans.back() - i > 0) {
				ans.pb(ans.back() - i);
				s -= i;
				k--;
			}
		}
	}
	printf("YES\n");
	for (int i = 1; i < int(ans.size()); ++i) {
		printf("%lld ", ans[i]);
	}
	printf("\n");
#ifndef ONLINE_JUDGE
	fclose(stdin);
	fclose(stdout);
	system("gedit out.txt");
#endif
    return 0;
}

E1. Stars Drawing (Easy Edition)

Description:

A star is a figure of the following type: an asterisk character ‘*’ in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length 0 0 0 are not allowed).
Let’s consider empty cells are denoted by ‘.’, then the following figures are stars:

The leftmost figure is a star of size 1 1 1, the middle figure is a star of size 2 2 2 and the rightmost figure is a star of size 3 3 3. You are given a rectangular grid of size n × m n \times m n×m consisting only of asterisks ‘*’ and periods (dots) ‘.’. Rows are numbered from 1 1 1 to n n n, columns are numbered from 1 1 1 to m m m. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can’t exceed n m n \cdot m nm. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.

In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most n m n \cdot m nm stars.

Input:

The first line of the input contains two integers n n n and m m m ( 3 n , m 100 3 \le n, m \le 100 3n,m100) — the sizes of the given grid.
The next n n n lines contains m m m characters each, the i i i-th line describes the i i i-th row of the grid. It is guaranteed that grid consists of characters ‘*’ and ‘.’ only.

Output

If it is impossible to draw the given grid using stars only, print “-1”.
Otherwise in the first line print one integer k k k ( 0 k n m 0 \le k \le n \cdot m 0knm) — the number of stars needed to draw the given grid. The next k k k lines should contain three integers each — x j x_j xj, y j y_j yj and s j s_j sj, where x j x_j xj is the row index of the central star character, y j y_j yj is the column index of the central star character and s j s_j sj is the size of the star. Each star should be completely inside the grid.

Sample Input:

6 8
....*...
...**...
..*****.
...**...
....*...
........

Sample Output:

3
3 4 1
3 5 2
3 5 1

Sample Input:

5 5
.*...
****.
.****
..**.
.....

Sample Output:

3
2 2 1
3 3 1
3 4 1

Sample Input:

5 5
.*...
***..
.*...
.*...
.....

Sample Output:

-1

Sample Input:

3 3
*.*
.*.
*.*

Sample Output:

-1

题目链接

E1和E2两道题目只是数据范围稍有不同,做法一样。

给出一个由’*‘和’.‘组成的矩阵,有多少种由’*‘组成的’十’型图案,若这些图案可以覆盖全部’*‘字符,则输出每个’十’的中心’*‘坐标和长度,若不能全部覆盖输出’-1’。

枚举每一个’*'为’十’的中心,判断标记即可。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef pair<ll,ll> PLL;
const int INF = 0x3f3f3f3f;
const int maxn = 1e2 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x) {
	Finish_read = 0;
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') {
			f = -1;
		}
		if (ch == EOF) {
			return;
		}
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	x *= f;
	Finish_read = 1;
};

struct Star {
	int i, j, l;
	Star(int _i = 0, int _j = 0, int _l = 0): i(_i), j(_j), l(_l) {}
};

int n, m;
char plat[maxn][maxn];
bool flag[maxn][maxn];
bool ok;
vector<PII> bit;
vector<Star> ans;

void judge(PII x) {
	int res = 0;
	while (x.first - res - 1 >= 1 &&
			x.first + res + 1 <= n &&
			x.second - res - 1 >= 1 &&
			x.second + res + 1 <= m &&
			plat[x.first - res - 1][x.second] == '*' &&
			plat[x.first + res + 1][x.second] == '*' &&
			plat[x.first][x.second - res - 1] == '*' &&
			plat[x.first][x.second + res + 1] == '*') {
		flag[x.first - res - 1][x.second] = 1;
		flag[x.first + res + 1][x.second] = 1;
		flag[x.first][x.second - res - 1] = 1;
		flag[x.first][x.second + res + 1] = 1;
		res++;
	}
	if (res > 0) {
		flag[x.first][x.second] = 1;
		ans.pb(Star {x.first, x.second, res});
	}
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	cin >> n >> m;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			cin >> plat[i][j];
			if (plat[i][j] == '*') {
				bit.pb(mp(i, j));
			}
		}
	}
	mem(flag, 0);
	for (auto it : bit) {
		judge(it);
	}
	ok = 1;
	for (auto it : bit) {
		if (!flag[it.first][it.second]) {
			ok = 0;
			break;
		}
	}
	if (ok) {
		printf("%d\n", int(ans.size()));
		for (auto it : ans) {
			printf("%d %d %d\n", it.i, it.j, it.l);
		}
	}
	else {
		printf("-1\n");
	}
#ifndef ONLINE_JUDGE
	fclose(stdin);
	fclose(stdout);
	system("gedit out.txt");
#endif
    return 0;
}

E2. Stars Drawing (Hard Edition)

Description:

A star is a figure of the following type: an asterisk character ‘*’ in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length 0 0 0 are not allowed).
Let’s consider empty cells are denoted by ‘.’, then the following figures are stars:

The leftmost figure is a star of size 1 1 1, the middle figure is a star of size 2 2 2 and the rightmost figure is a star of size 3 3 3. You are given a rectangular grid of size n × m n \times m n×m consisting only of asterisks ‘*’ and periods (dots) ‘.’. Rows are numbered from 1 1 1 to n n n, columns are numbered from 1 1 1 to m m m. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can’t exceed n m n \cdot m nm. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes.

In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most n m n \cdot m nm stars.

Input:

The first line of the input contains two integers n n n and m m m ( 3 n , m 1000 3 \le n, m \le 1000 3n,m1000) — the sizes of the given grid.
The next n n n lines contains m m m characters each, the i i i-th line describes the i i i-th row of the grid. It is guaranteed that grid consists of characters ‘*’ and ‘.’ only.

Output

If it is impossible to draw the given grid using stars only, print “-1”.
Otherwise in the first line print one integer k k k ( 0 k n m 0 \le k \le n \cdot m 0knm) — the number of stars needed to draw the given grid. The next k k k lines should contain three integers each — x j x_j xj, y j y_j yj and s j s_j sj, where x j x_j xj is the row index of the central star character, y j y_j yj is the column index of the central star character and s j s_j sj is the size of the star. Each star should be completely inside the grid.

Sample Input:

6 8


*.


Sample Output:

3
3 4 1
3 5 2
3 5 1

Sample Input:

5 5
.
*
.
.****
.

Sample Output:

3
2 2 1
3 3 1
3 4 1

Sample Input:

5 5
.*…
*
.

.

Sample Output:

-1

Sample Input:

3 3
.
.*.
.

Sample Output:

-1

题目链接

同上。

AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef pair<ll,ll> PLL;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x) {
	Finish_read = 0;
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') {
			f = -1;
		}
		if (ch == EOF) {
			return;
		}
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	x *= f;
	Finish_read = 1;
};

struct Star {
	int i, j, l;
	Star(int _i = 0, int _j = 0, int _l = 0): i(_i), j(_j), l(_l) {}
};

int n, m;
char plat[maxn][maxn];
bool flag[maxn][maxn];
bool ok;
vector<PII> bit;
vector<Star> ans;

void judge(PII x) {
	int res = 0;
	while (x.first - res - 1 >= 1 &&
			x.first + res + 1 <= n &&
			x.second - res - 1 >= 1 &&
			x.second + res + 1 <= m &&
			plat[x.first - res - 1][x.second] == '*' &&
			plat[x.first + res + 1][x.second] == '*' &&
			plat[x.first][x.second - res - 1] == '*' &&
			plat[x.first][x.second + res + 1] == '*') {
		flag[x.first - res - 1][x.second] = 1;
		flag[x.first + res + 1][x.second] = 1;
		flag[x.first][x.second - res - 1] = 1;
		flag[x.first][x.second + res + 1] = 1;
		res++;
	}
	if (res > 0) {
		flag[x.first][x.second] = 1;
		ans.pb(Star {x.first, x.second, res});
	}
}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	cin >> n >> m;
	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			cin >> plat[i][j];
			if (plat[i][j] == '*') {
				bit.pb(mp(i, j));
			}
		}
	}
	mem(flag, 0);
	for (auto it : bit) {
		judge(it);
	}
	ok = 1;
	for (auto it : bit) {
		if (!flag[it.first][it.second]) {
			ok = 0;
			break;
		}
	}
	if (ok) {
		printf("%d\n", int(ans.size()));
		for (auto it : ans) {
			printf("%d %d %d\n", it.i, it.j, it.l);
		}
	}
	else {
		printf("-1\n");
	}
#ifndef ONLINE_JUDGE
	fclose(stdin);
	fclose(stdout);
	system("gedit out.txt");
#endif
    return 0;
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务