noip模拟赛 分组


分析:暴力分挺多,也挺好想的,个人感觉两个特殊性质没什么卵用.
对于K=1,n ≤ 1024的情况,从后往前贪心地分,如果能和上一组分在一起就分在一起,否则就再开一组,这样可以保证字典序最小.ai ≤ 2就看前面有没有2.有就不能分在一组.n ≤ 131072就不能再这样二重循环枚举了,因为两个数的和顶多只有262114 = 512^2,从1枚举到512,看看它的平方有没有被占用就可以了,就把问题从序列上转化到了值域上.
对于K=2,其实做法和K=1没什么两样,只是每一组可以分成两个对立的小组,非常像noip2010关押罪犯,加一个并查集就好了.用一个vector存每个值的兔子的位置,判断一下有没有冲突就好了.
暴力+正解:
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ; vector <int>E[maxn * + ];
int n, k, a[], ans[], cnt, fa[], vis[], T;
bool flag = true; bool check2(int x)
{
for (int i = ; i <= ; i++)
{
if (i * i - x >= && vis[i * i - x])
return false;
}
return true;
} int find(int x)
{
if (x == fa[x])
return x;
return fa[x] = find(fa[x]);
} bool check(int x, int y)
{
int t = x + y;
int p = sqrt(t);
if (p * p == t)
return false;
return true;
} void solve1()
{
if (n <= )
{
int cur = n;
for (int i = n; i >= ; i--)
{
for (int j = i + ; j <= cur; j++)
{
if (!check(a[i], a[j]))
{
ans[++cnt] = i;
cur = i;
}
}
}
printf("%d\n", cnt + );
for (int i = cnt; i >= ; i--)
printf("%d ", ans[i]);
printf("\n");
}
else
if (flag)
{
int cur = n;
bool flag2 = false;
for (int i = n; i >= ; i--)
{
if (flag2 && a[i] == )
{
ans[++cnt] = i;
cur = i;
}
if (a[i] == )
flag2 = ;
}
printf("%d\n", cnt + );
for (int i = cnt; i >= ; i--)
printf("%d ", ans[i]);
printf("\n");
}
else
{
int cur = n;
for (int i = n; i >= ; i--)
{
if (!check2(a[i]))
{
for (int j = i + ; j <= cur; j++)
vis[a[j]] = ;
ans[++cnt] = i;
cur = i;
}
vis[a[i]] = ;
}
printf("%d\n", cnt + );
for (int i = cnt; i >= ; i--)
printf("%d ", ans[i]);
printf("\n");
}
} void hebing(int x, int y)
{
x = find(x), y = find(y);
fa[x] = y;
} bool check3(int x)
{
for (int i = ; i <= ; i++)
{
if (i * i - a[x] >= && vis[i * i - a[x]] == T)
{
int now = i * i - a[x];
for (int j = ; j < E[now].size(); j++)
{
if (find(x) == find(E[now][j]))
return false;
hebing(x + n, E[now][j]);
hebing(x, E[now][j] + n);
}
}
}
return true;
} void solve2()
{
if (flag)
{
int flag2 = ;
for (int i = n; i >= ; i--)
{
if (flag2 == && a[i] == )
{
ans[++cnt] = i;
flag2 = ;
}
if (a[i] == )
flag2++;
}
printf("%d\n", cnt + );
for (int i = cnt; i >= ; i--)
printf("%d ", ans[i]);
printf("\n");
}
else
if (n <= )
{
for (int i = ; i <= n * ; i++)
fa[i] = i;
int cur = n;
for (int i = n; i >= ; i--)
{
for (int j = i + ; j <= cur; j++)
{
if (!check(a[i], a[j]))
{
if (find(i) == find(j))
{
ans[++cnt] = i;
cur = i;
}
else
{
int xx = find(i + n), yy = find(j + n);
fa[i] = yy;
fa[j] = xx;
}
}
}
}
printf("%d\n", cnt + );
for (int i = cnt; i >= ; i--)
printf("%d ", ans[i]);
printf("\n");
}
else
{
T = ;
for (int i = ; i <= n * ; i++)
fa[i] = i;
for (int i = n; i >= ; i--)
{
if (!check3(i))
{
T++;
ans[++cnt] = i;
}
if (vis[a[i]] != T)
{
vis[a[i]] = T;
E[a[i]].clear();
}
E[a[i]].push_back(i);
}
printf("%d\n", cnt + );
for (int i = cnt; i >= ; i--)
printf("%d ", ans[i]);
printf("\n");
}
} int main()
{
scanf("%d%d", &n, &k);
for (int i = ; i <= n; i++)
{
scanf("%d", &a[i]);
if (a[i] > )
flag = false;
} if (k == )
solve1();
if (k == )
solve2(); return ;
}
noip模拟赛 分组的更多相关文章
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
- NOIP模拟赛 by hzwer
2015年10月04日NOIP模拟赛 by hzwer (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
- CH Round #52 - Thinking Bear #1 (NOIP模拟赛)
A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...
随机推荐
- 转】MYSQL性能调优与架构设计之select count(*)的思考
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/5/ 感谢! Posted: Feb 7, 2013 Tag ...
- poj1781In Danger(约瑟夫) 问题
链接 之前队内赛中的一道题目 当时怎么想也没想到,就一直放到了今天,刚才看另一题的讲解突然看到时拿这个题作为引子来讲的,就仔细看了下. 参考<<具体数学>> p7. Josep ...
- [转]Resolve Team Foundation Version Control conflicts
本文转自:http://msdn.microsoft.com/en-us/library/ms181432.aspx An advantage of using Team Foundation ver ...
- vue.js学习参考手册
参考手册 示例:www.51siyuan.cn/161.html
- Partial(部分方法,局部方法),virtual(虚方法),abstract(抽象方法)
Partial 部分方法顾明思议是方法的一部分,不完整的,在ide编译时候,会将所有部分方法加载到一起统一编译,如果分部方法没有被实现,编译器就不会.对他们进行编译. 局部类型的限制 (1) 局部类型 ...
- 用idea+maven编译打包spark project core错误:java.lang.RuntimeException: Unable to load a Suite class
Discovery starting. *** RUN ABORTED *** java.lang.RuntimeException: Unable to load a Suite class tha ...
- 迅为4412嵌入式安卓开发板兼容3G网络|4G网络
iTOP-Exynos4412开发板内置有无线 WIFI 模块.Bluetooth.GPS.Camera.3G等模组,陀螺仪等,支持 HDMI1.4(1080P/60Hz)显示,客户可以直接从开发平台 ...
- (转)淘淘商城系列——SSM框架整合之Service层整合
http://blog.csdn.net/yerenyuan_pku/article/details/72721108 上文我们一起学习了Dao层的整合,本文将教大家如何整合Service层. 我们需 ...
- Java HTTP 组件库选型看这篇就够了
最近项目需要使用 Java 重度调用 HTTP API 接口,于是想着封装一个团队公用的 HTTP client lib. 这个库需要支持以下特性: 连接池管理,包括连接创建和超时.空闲连接数控制.每 ...
- python3安装opencv及电子书籍(百度云)
不能直接 pip install opencv 正解: pip install opencv-python 记得:请确保网络良好!!!!! (1)这个是我学习的电子书籍:opencv-python ...