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.
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.
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
题目给出一个项链,其中’-'代表链条,'o’代表珍珠,求重新排列项链之后能否使每个珍珠中链条数相等。首先如果珍珠数少于链条数则不可能,其次根据链条数能否被珍珠数整除判断结果。
#include
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;
}
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?
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.
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
YES
.......
.#.....
.#.....
.......
5 3
YES
.....
.###.
.....
.....
在一个4行n的地图上摆放k个障碍物,使左上角到右下角的最短路径数量等于左下角到右上角的最短路径数量。
若k为偶数则障碍物上下对称摆放,若为奇数则左右对称。
#include
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[5][maxn];
int main() {
//fre();
scanf("%d%d", &n, &k);
printf("YES\n");
mem(ans, '.');
if (k % 2) {
ans[2][(n + 1) >> 1] = '#';
k--;
int temp = (n - 1) >> 1;
while (k && temp > 1) {
ans[2][temp] = '#';
ans[2][(n + 1) - temp] = '#';
temp--; k -= 2;
}
temp = 2;
while (k) {
ans[3][temp] = '#';
ans[3][(n + 1) - temp] = '#';
temp++; k-= 2;
}
}
else {
int temp = 2;
while (k) {
ans[2][temp] = '#';
ans[3][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;
}
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.
The first line of input contains two integers n and k ( 1 ≤ n ≤ 1 0 5 , 1 ≤ k ≤ 256 ) k (1≤n≤10^5, 1≤k≤256) k(1≤n≤105,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 ) p_1,p_2,…,p_n (0≤p_i≤255) p1,p2,…,pn(0≤pi≤255), where p i p_i pi is the color of the i-th pixel.
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
对一个数列进行分组,一组不超过k个元素,每组的所有数均换为此组内的最小值,求最小字典序结果。贪心,对每个数字 p i p_i pi寻找 p i − k + 1 p_i-k+1 pi−k+1~ p i p_i pi判断是否已分组,对第一个未分组的数组x从x到 p i p_i pi全部分到x一组。
#include
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;
}
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).
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 ( − 1 0 8 ≤ a i ≤ 1 0 8 ) a_1,a_2,…,a_n (−10^8≤a_i≤10^8) a1,a2,…,an(−108≤ai≤108), the values of the array.
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
这道题目从这篇博文所学习。
已知有这么一道题:给你n个数字,你要将这n个数字打乱后分成k组,使得对于同一个组中的任意一对数字满足两个数相乘一定是个完全平方数,求出最小的k
而这道题的意思是:给你n个数字,这n个数字一共有(1+n)*n/2个连续子序列,对于连续每个子序列你都要求出上面的那个k,最后统计k=1的子序列有多少个,k=2的子序列有多少个……k=n的子序列有多少个
对于x(x!=0),如果存在一个y满足x是y²的倍数,那么将x变为x/y²不会影响答案
这样的话对于每个a[i],暴力所有平方数,如果整除就除掉,最后得到的a[i]必定满足 a[i] = ∏pj,其中pj各不相同且都为质数
结论:这样处理完之后,满足任意两个数相乘都为平方数的组内所有数字必定都相同,换句话说,只有相同的两个数相乘才能是个完全平方数,否则一定不是
这样这题就简单了:给你n个数字,这n个数字一共有(1+n)*n/2个连续子序列,对于每个连续子序列有多少个互不相同的数字,那么k就为多少
离散化或者map(map会超时……)暴力统计一下就ok了,复杂度O(n²)或O(n²logn)
当然要特判0!0乘任意一个数字都是0,所以0可以加入任何一个组中
#include
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;
}