## Description:

A necklace can be described as a string of links ('-') and pearls ('o'), with the last link or pearl connected to the first one. You can remove a link or a pearl and insert it between two other existing links or pearls (or between a link and a pearl) on the necklace. This process can be repeated as many times as you like, but you can't throw away any parts.

Can you make the number of links between every two adjacent pearls equal? Two pearls are considered to be adjacent if there is no other pearl between them.

Note that the final necklace should remain as one circular part of the same length as the initial necklace.

## Input:

The only line of input contains a string s (3≤|s|≤100), representing the necklace, where a dash '-' represents a link and the lowercase English letter 'o' represents a pearl.

## Output:

Print "YES" if the links and pearls can be rejoined such that the number of links between adjacent pearls is equal. Otherwise print "NO".

You can print each letter in any case (upper or lower).

-o-o--

YES

-o---

YES

-o---o-

NO

ooo

YES

## AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> p;
const int INF = 0x3f3f3f3f;
const int maxn = 1e2+5;
const int mod = 1e9+7;
const double eps = 1e-5;
const double pi = asin(1.0)*2;
const double e = 2.718281828459;
void fre() {
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
}

string str;
int o_cnt, _cnt;

int main() {
//fre();
cin >> str;
o_cnt = 0, _cnt = 0;
for (int i = 0; str[i] != '\0'; ++i) {
if (str[i] == 'o') {
o_cnt++;
}
else {
_cnt++;
}
}
if (_cnt == 0 || o_cnt == 0) {
cout << "YES";
}
else if (o_cnt > _cnt) {
cout << "NO";
}
else {
if (_cnt % o_cnt == 0) {
cout << "YES";
}
else {
cout << "NO";
}
}
return 0;
}

# B. Marlin

## Description:

The city of Fishtopia can be imagined as a grid of 4 rows and an odd number of columns. It has two main villages; the first is located at the top-left cell (1,1), people who stay there love fishing at the Tuna pond at the bottom-right cell (4,n). The second village is located at (4,1)and its people love the Salmon pond at (1,n).

The mayor of Fishtopia wants to place k hotels in the city, each one occupying one cell. To allow people to enter the city from anywhere, hotels should not be placed on the border cells.

A person can move from one cell to another if those cells are not occupied by hotels and share a side.

Can you help the mayor place the hotels in a way such that there are equal number of shortest paths from each village to its preferred pond?

## Input:

The first line of input contain two integers, n and k (3≤n≤99, 0≤k≤2×(n−2)), n is odd, the width of the city, and the number of hotels to be placed, respectively.

## Output:

Print "YES", if it is possible to place all the hotels in a way that satisfies the problem statement, otherwise print "NO".

If it is possible, print an extra 4 lines that describe the city, each line should have n characters, each of which is "#" if that cell has a hotel on it, or "." if not.

7 2

## Sample Output:

YES
.......
.#.....
.#.....
.......

5 3

## Sample Output:

YES
.....
.###.
.....
.....

## AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> p;
const int INF = 0x3f3f3f3f;
const int maxn = 1e2+5;
const int mod = 1e9+7;
const double eps = 1e-5;
const double pi = asin(1.0)*2;
const double e = 2.718281828459;
void fre() {
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
}

int n, k;
char ans[maxn];

int main() {
//fre();
scanf("%d%d", &n, &k);
printf("YES\n");
mem(ans, '.');
if (k % 2) {
ans[(n + 1) >> 1] = '#';
k--;
int temp = (n - 1) >> 1;
while (k && temp > 1) {
ans[temp] = '#';
ans[(n + 1) - temp] = '#';
temp--; k -= 2;
}
temp = 2;
while (k) {
ans[temp] = '#';
ans[(n + 1) - temp] = '#';
temp++; k-= 2;
}
}
else {
int temp = 2;
while (k) {
ans[temp] = '#';
ans[temp] = '#';
temp++; k -= 2;
}
}
for (int i = 1; i < 5; ++i) {
for (int j = 1; j <= n; ++j) {
printf("%c", ans[i][j]);
}
printf("\n");
}
return 0;
}

# C. Posterized

## Description:

Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked his students to implement the Posterization Image Filter.

Their algorithm will be tested on an array of integers, where the i-th integer represents the color of the i-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive).

To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the group’s key. In order to preserve image details, the size of a group must not be greater than k, and each color should belong to exactly one group.

Finally, the students will replace the color of each pixel in the array with that color’s assigned group key.

To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing k to the right. To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array.

## Input:

The first line of input contains two integers n and $k (1≤n≤10^5, 1≤k≤256)$, the number of pixels in the image, and the maximum size of a group, respectively.

The second line contains n integers $p_1,p_2,…,p_n (0≤p_i≤255)$, where $p_i$ is the color of the i-th pixel.

## Output:

Print n space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter.

4 3
2 14 3 4

0 12 3 3

5 2
0 2 1 255 254

0 1 1 254 254

## AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 2e2+6e1;
const int mod = 1e9+7;
const double eps = 1e-5;
const double pi = asin(1.0)*2;
const double e = 2.718281828459;
void fre() {
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
}

int n, k;
int p[maxn];
int input;
int temp;

int main() {
//fre();
scanf("%d%d", &n, &k);
mem(p, -1);
for (int i = 0; i < n; ++i) {
scanf("%d", &input);
if (p[input] == -1) {
temp = (input - k + 1) < 0 ? 0 : (input - k + 1);
for (int j = temp; j <= input; ++j) {
if (p[j] == -1 || p[j] == j) {
for (int l = j; l <= input; ++l) {
p[l] = j;
}
break;
}
}
}
printf("%d ", p[input]);
}
return 0;
}

# D. Perfect Groups

## Description:

SaMer has written the greatest test case of all time for one of his problems. For a given array of integers, the problem asks to find the minimum number of groups the array can be divided into, such that the product of any pair of integers in the same group is a perfect square.

Each integer must be in exactly one group. However, integers in a group do not necessarily have to be contiguous in the array.

SaMer wishes to create more cases from the test case he already has. His test case has an array A of n integers, and he needs to find the number of contiguous subarrays of A that have an answer to the problem equal to k for each integer k between 1 and n (inclusive).

## Input:

The first line of input contains a single integer nn (1≤n≤5000), the size of the array.

The second line contains n integers $a_1,a_2,…,a_n (−10^8≤a_i≤10^8)$, the values of the array.

## Output:

Output n space-separated integers, the k-th integer should be the number of contiguous subarrays of A that have an answer to the problem equal to k.

2
5 5

3 0

5
5 -4 2 1 8

5 5 3 2 0

1
0

1

## AC代码:

#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const int maxn = 5e3+5;
const int mod = 1e9+7;
const double eps = 1e-8;
const double pi = asin(1.0)*2;
const double e = 2.718281828459;
void fre() {
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
}

int n;
int a[maxn];
int p[maxn];
int ans[maxn];
bool vis[maxn];
int len;

int main() {
//fre();
mem(ans, 0);
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
for (int j = 2; j * j <= abs(a[i]); ++j) {
while (a[i] % (j * j) == 0) {
a[i] /= (j * j);
}
}
p[i] = a[i];
}
sort(p, p + n);
len = unique(p, p + n) - p;
for (int i = 0; i < n; ++i) {
a[i] = lower_bound(p, p + len, a[i]) - p;
}
for (int i = 0; i < n; ++i) {
mem(vis, 0);
int cnt = 0;
bool zero = 0;
for (int j = i; j < n; ++j) {
if (!vis[a[j]]) {
vis[a[j]] = 1;
cnt++;
}
if (p[a[j]] == 0) {
zero = 1;
}
ans[max(1, cnt - zero)]++;
}
}
for (int i = 1; i <= n; ++i) {
printf("%d ", ans[i]);
}
return 0;
}

Last modification：March 29th, 2019 at 03:54 pm