A

/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll LLmaxn = 2e18;
int main()
{
string a;
cin >> a;
int b=, c=;
for (int i = ; i < a.size(); i++)
{
if (a[i] == '-')
{
b++;
}
else
{
c++;
}
}
//cout<<b<<" "<<c<<endl;
if (c == )
{
cout << "YES" << endl;
return ;
}
if (b % c != )
{
cout << "NO" << endl;
}
else
{
cout << "YES" << endl;
}
return ;
}

B

解:

注意只要是上下对称或者是左右对称就可以使得 1-4 有一条路径的话 2-3 也有相对应的一条路径

剩下的就容易构造了

/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll LLmaxn = 2e18;
char ans[][];
int main()
{
int n, k;
cin >> n >> k;
for (int i = ; i <= ; i++)
{
for (int j = ; j <= n; j++)
{
ans[i][j] = '.';
}
}
int sum = (n - ) * ;
if (k > sum)
{
cout << "NO" << endl;
return ;
}
cout << "YES" << endl;
if (k % == )
{
int cur = k;
for (int j = ; j <= n - && cur; j++)
{
for (int i = ; i <= && cur; i++)
{
ans[i][j] = '#';
cur--;
}
}
}
else
{
k--;
ans[][n / + ] = '#';
for (int i = ; i <= && k; i++)
{
for (int j = ; j <= n / && k; j++)
{
ans[i][j] = ans[i][n - j + ] = '#';
k -= ;
}
}
}
for (int i = ; i <= ; i++)
{
for (int j = ; j <= n; j++)
{
cout << ans[i][j];
}
cout << endl;
}
return ;
}

C

解:

可以用并查集维护每个点最小到达的地方 也因为N<=1E5,K<=256 直接暴力模拟也可以

/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll LLmaxn = 2e18;
char ans[][];
int main()
{
int n, k;
cin >> n >> k;
for (int i = ; i <= ; i++)
{
for (int j = ; j <= n; j++)
{
ans[i][j] = '.';
}
}
int sum = (n - ) * ;
if (k > sum)
{
cout << "NO" << endl;
return ;
}
cout << "YES" << endl;
if (k % == )
{
int cur = k;
for (int j = ; j <= n - && cur; j++)
{
for (int i = ; i <= && cur; i++)
{
ans[i][j] = '#';
cur--;
}
}
}
else
{
k--;
ans[][n / + ] = '#';
for (int i = ; i <= && k; i++)
{
for (int j = ; j <= n / && k; j++)
{
ans[i][j] = ans[i][n - j + ] = '#';
k -= ;
}
}
}
for (int i = ; i <= ; i++)
{
for (int j = ; j <= n; j++)
{
cout << ans[i][j];
}
cout << endl;
}
return ;
}

D

卡题意...

给出一个数组,把里面的数字分组,使得每一个组里面的数两两相乘都是完全平方数.

问最少可以分成的组数k是多少.

现在一个人有一个数组,他想知道这个数组的连续子数组中,使得上面的问题答案分别为1到n的数组有多少个.

第一种做法:

注意当一个数X的因数有完全平方数Y的时候 把这个数替换为X/Y并不影响结果

当一个数是0的时候要特判 要因为0乘任何数都是0可以加入任意一组

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
#include <math.h>
using namespace std;
const int N = ;
int a[N];
int b[N];
int c[N];
bool vis[N];
int mabs(int x)
{
return x >= ? x : -x;
}
int main()
{
int n;
int cnt = ;
scanf("%d",&n);
for (int i = ;i < n;i++)
{
scanf("%d",&a[i]);
if (a[i] != ) cnt++;
}
if (cnt == )
{
printf("%d ",n * (n + ) / );
for (int i = ;i < n;i++) printf("0 ");
}
else
{
for (int i = ;i < n;i++)
{
if (a[i] == ) continue;
int tmp = mabs(a[i]);
for (int j = ;j * j <= tmp;j++)
{
int t = j * j;
while (a[i] % t == )
{
a[i] /= t;
}
//if (a[i] < t) break;加了这个就wa,卡了一晚上,考虑的应该是绝对值的情况
}
}
for (int i = ;i < n;i++) c[i] = a[i];
sort(c,c+n);
int js = unique(c,c+n) - c;
for (int i = ;i < n;i++)
{
if (a[i] == ) continue;
int p = lower_bound(c,c+js,a[i]) - c + ;
a[i] = p;
}
for (int i = ;i < n;i++)
{
memset(vis,,sizeof(vis));
int num = ;
for (int j = i;j < n;j++)
{
if (!vis[a[j]] && a[j] != )
{
num++;
vis[a[j]] = ;
}
int tt = max(num,);
b[tt]++;
}
}
for (int i = ;i <= n;i++)
{
printf("%d ",b[i]);
}
}
return ;
}

第二种做法:

可以证明当A*B为完全平方数B*C也为完全平方数时 A*C也是完全平方数

所以只要N^2遍历 用并查集维护每个数属于哪个集合就行 注意0可以属于任意一个集合

#include<bits/stdc++.h>
using namespace std;
const int N = ;
long long a[N];
int ans[N], fa[N], d[N];
int bz[N];
int n, sum, tot;
bool check(long long x)
{
if (x < )
{
return ;
}
long long y = sqrt(x);
return y * y == x || (y - ) * (y - ) == x || (y + ) * (y + ) == x;
}
int main()
{
ios::sync_with_stdio(false);
cin >> n;
for (int i = ; i <= n; i++)
{
fa[i] = i;
}
for (int i = ; i <= n; i++)
{
cin >> a[i];
if (a[i])
{
for (int j = ; j <= d[]; j++)
if (check(a[i]*a[d[j]]))
{
fa[i] = d[j];
break;
}
if (fa[i] == i)
{
d[++d[]] = i;
}
}
}
for (int i = ; i <= n; i++)
{
sum = ;
tot++;
for (int j = i; j <= n; j++)
{
if (a[j] && bz[fa[j]] != tot)
{
bz[fa[j]] = tot, sum++;
}
ans[max(sum, )]++;
}
}
for (int i = ; i <= n; i++)
{
cout << ans[i] << ' ';
}
return ;
}

E

给你N个点 组成的一颗树 分别从1标号到N 每个点的粉丝数量为2^i个

要求是选择K个点删除 使得剩下没被删的点保持连通且剩下的粉丝数量最大

一旦某个点被删除则其不能通过且该点的粉丝数量清零

假如做法顺着做 找出需要删除那些点的话 因为要保证连通性所以删除一个点需要删除掉他所有子树的点 不好做

题目提示你K<N 所以点N是一定可以保留的 就以N为根倍增预处理祖先 倒着做 找出不需要删除的点即可

/* Huyyt */
#include <bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
#define pb push_back
using namespace std;
typedef long long ll;
const long long mod = 1e9 + ;
const int N = 1e6 + ;
inline int readint()
{
char c = getchar();
int ans = ;
while (c < '' || c > '')
{
c = getchar();
}
while (c >= '' && c <= '')
{
ans = ans * + c - '', c = getchar();
}
return ans;
}
vector<int> tree[N];
bool check[N];
int father[N][];
int deep[N];
void dfs(int x, int level)
{
for (int i = ; father[father[x][i]][i]; i++)
{
father[x][i + ] = father[father[x][i]][i];
}
deep[x] = level;
for (int i = ; i < tree[x].size(); i++)
{
int to = tree[x][i];
if (to == father[x][])
{
continue;
}
father[to][] = x;
dfs(to, level + );
}
}
int main()
{
int n, k;
n = readint(), k = readint();
//cout << n << " " << k << endl;
int u, v;
for (int i = ; i < n; i++)
{
u = readint(),v = readint();
tree[u].pb(v);
tree[v].pb(u);
}
k = n - k;
k--, check[n] = ;
dfs(n, );
for (int i = n - ; i >= && k; i--)
{
int aim = -;
int now = i;
if (check[i])
{
continue;
}
for (int j = ; j >= ; j--)
{
if (father[now][j] == || check[father[now][j]])
{
continue;
}
now = father[now][j];
}
if (deep[i] - deep[now] + <= k)
{
now = i;
while (now != && !check[now])
{
check[now] = ;
k--;
now = father[now][];
}
}
}
for (int i = ; i <= n - ; i++)
{
if (!check[i])
{
cout << i << " ";
}
}
return ;
}

Codeforces 980 并查集/模拟贪心最小字典序 找规律/数去除完全平方因子 逆思维倍增预处理祖先标记点的更多相关文章

  1. noip模拟赛 动态仙人掌(并查集,贪心)

    思路: 贪心+并查集 因为45‘,所以可以很方便的算出每个仙人掌的最晚起跳(左端点) 右端点自然也能出来 先按左端点排序 如果他右面的和他相交,就更新 用并查集维护这个更新的关系 更新的同时维护高就好 ...

  2. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

  3. hdu-1198 Farm Irrigation---并查集+模拟(附测试数据)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目大意: 有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种 ...

  4. CodeForces - 1209D 并查集

    题意: 有 n个不同的糖果,从 1到 n编号.有 k个客人.要用糖果招待客人.对于每个客人,这些糖果中恰有两个是其最爱.第 i个客人最爱的糖果编号是 xi和 y.将 k 个客人任意排列,他们按顺序去拿 ...

  5. Vladik and Entertaining Flags CodeForces - 811E (并查集,线段树)

    用线段树维护每一块左右两侧的并查集, 同色合并时若不连通则连通块数-1, 否则不变 #include <iostream> #include <algorithm> #incl ...

  6. CodeForces - 893C-Rumor(并查集变式)

    Vova promised himself that he would never play computer games... But recently Firestorm - a well-kno ...

  7. 洛谷 - P4997 - 不围棋 - 并查集 - 模拟

    https://www.luogu.org/problemnew/show/P4997 首先是改变气的定义,使得容易计算,这个很好理解. 然后使用并查集,因为要维护整个连通块的性质. 最后的难点在于, ...

  8. 0-1-Tree CodeForces - 1156D (并查集)

    大意: 给定树, 边权为黑或白, 求所有有向路径条数, 满足每走过一条黑边后不会走白边. 这题比赛的时候想了个假算法, 还没发现..... 显然所求的路径要么全黑, 要么全白, 要么先全白后全黑, 所 ...

  9. Codeforces 1166F 并查集 启发式合并

    题意:给你一张无向图,无向图中每条边有颜色.有两种操作,一种是询问从x到y是否有双彩虹路,一种是在x到y之间添加一条颜色为z的边.双彩虹路是指:如果给这条路径的点编号,那么第i个点和第i - 1个点相 ...

随机推荐

  1. vscode + php+ftp

    首先,php网站的文件都整理到一个文件夹中: 然后,用vscode的File.Open Folder打开刚才的文件夹: 3,Ctrl+Shift+P,输入SFTP:Config,会打开一个配置文件,编 ...

  2. Kotlin之定义函数

    java: int add (int m ,int n){ return m+n; } void process(int m){ Systrm.out.println(m); } kotlin: fu ...

  3. python之reportlab生成PDF文件

    项目需要,需要自动生成PDF测试报告.经过对比之后,选择使用了reportlab模块. 项目背景:开发一个测试平台,供测试维护测试用例,执行测试用例,并且生成测试报告(包含PDF和excel),将生成 ...

  4. Python学习之==>迭代器

    一.概要 在了解Python的数据结构时,容器(container).可迭代对象(iterable).迭代器(iterator).生成器(generator).列表/集合/字典推导式(list,set ...

  5. 跨域请求配置 Amazon AWS S3 腾讯云 阿里云 COS OSS 文件桶解决方案以及推荐 Lebal:Research

    跨域请求配置 跨域请求指的就是不同的域名和端口之间的访问.由于 ajax 的同源策略影响.跨域请求默认是不被允许的. 使用@font-face外挂字体时,可能遇到跨域请求CROS问题:F12控制台报错 ...

  6. java:IO流(处理流(缓冲流,转换流,数据流),对象的序列化,Properties)

    字节缓冲流:(BufferedInputStream,BufferedOutStream) *按照流的功能来分:节点流和处理流 *节点流可以直接操作数据源: *InputStream *--FileI ...

  7. css换行用省略号代替

    css换行用省略号代替,也可以说是长标题的文章可以使用简单的CSS样式实现省略号控制显示. 一般的文字截断(适用于内联与块): .text-overflow{ display:block;/*内联对象 ...

  8. Day04:继承的意义(下)

    对象转型 向上造型 什么是向上造型? 子类对象赋给父类引用. 父类引用指向子类对象. 子类转成父类 默认进行(父类引用指向子类对象). 为什么需要向上造型? 子类对象可以放入父类类型的数组中. 父类数 ...

  9. 2 Java中常见集合

    1)说说常见的集合有哪些吧? 答:集合有两个基本接口:Collection 和 Map. Collection 接口的子接口有:List 接口.Set 接口和 Queue 接口: List 接口的实现 ...

  10. CF-Div.3-B. Minimize the Permutation【模拟·需要清醒的脑子】

    题目传送门 根据字典序,是个人都会想到依次把目前最小的数尽量往前面移动,直到它不能再往前移动,或者已经到了它的期望位置(就是排列的那个位置 比如$i$就应该在位置$i$)为止. 所以我刚开始是这么写的 ...