noip模拟赛 都市



分析:是一道非常有意思的题,30分的暴力的话枚举每个位置是什么数,然后排个序,用map判一下重就好了,比较麻烦.
满分做法显然不可能讨论每个位置所有的情况,肯定是有规律的,现将这n*(n-1)/2个数排序,假设N个数组成的排列是a1,a2,......,aN,并且a1≤a2≤......≤aN.那么最小的那个和肯定是a1 + a2,次小的那个和肯定是a1 + a3,第三小的就不好确定了,如果能把a2 + a3给求出来,那么就能把a1,a2,a3给解出来,所以枚举a2+a3是哪一个,把a1+a2,a2+a3,a1+a3给求出来后从原数组中删掉,那么剩下的最小的数就是a1+a4,a4可以解出来,再把a2,a3与a4相加,把得到的数给删掉,再对a5进行同样的操作,就能得到整个序列了,所以枚举a2+a3的位置,并check一下就好了.
check的时候要先判断这个ai+a1在原数组中存不存在,是否都已经被占用了,要判断好所有的情况才行.
30分暴力:
#include <cstdio>
#include <map>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int n, cnt, a[], flag[], ans,tot,tag[];
int p[],cnt2,anss[][]; map<long long,bool> vis; struct node
{
int a[];
node() { memset(a, , sizeof(a)); }
}e[]; bool cmp(node x,node y)
{
for (int i = ; i <= n; i++)
if (x.a[i] > y.a[i])
return ;
return ;
} void print()
{
for (int i = ; i <= n; i++)
printf("%d ", flag[i]);
printf("\n");
} void solve()
{
long long res = ;
//print();
memcpy(tag,flag,sizeof(flag));
sort(tag + ,tag + + n);
for (int i = ; i <= n; i++)
res = (res * + tag[i]);
if (vis[res])
return;
vis[res] = ;
cnt2 = ;
for (int i = ; i <= n; i++)
for (int j = ; j < i; j++)
p[++cnt2] = flag[i] + flag[j];
sort(p + , p + + cnt2);
for (int i = ; i <= cnt; i++)
if (p[i] != a[i])
return;
tot++;
for (int i = ; i <= n; i++)
e[tot].a[i] = tag[i];
ans++;
} void dfs(int dep)
{
if (dep == n + )
{
solve();
return;
}
for (int i = ; i >= ; i--)
{
flag[dep] = i;
dfs(dep + );
}
} int main()
{
scanf("%d", &n);
cnt = n * (n - ) / ;
for (int i = ; i <= cnt; i++)
scanf("%d", &a[i]);
sort(a + ,a + + cnt);
dfs();
printf("%d\n", ans);
sort(e + ,e + + tot,cmp);
for (int i = ; i <= tot; i++)
{
for (int j = ; j <= n; j++)
printf("%d ", e[i].a[j]);
printf("\n");
} return ;
}
正解:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm> using namespace std; int n, a[], cnt, tot, ans[][], val[];
bool vis[]; void solve(int x)
{
memset(vis, , sizeof(vis));
int temp = (a[] + a[] + a[x]) / ;
val[] = temp - a[];
val[] = temp - a[];
val[] = temp - a[x];
vis[] = vis[] = vis[x] = ;
int cur = ;
for (int i = ; i <= n; i++)
{
while (cur <= cnt && vis[cur])
cur++;
if (cur > cnt)
return;
val[i] = a[cur] - val[];
vis[cur] = ;
for (int j = ; j < i; j++)
{
if (val[j] > val[i])
return;
int v = val[j] + val[i];
int pos = lower_bound(a + , a + cnt + , v) - a;
if (a[pos] != v)
return;
int pos2 = pos;
while (pos2 && a[pos2] == a[pos])
pos2--;
pos2++;
while (pos2 <= cnt && a[pos2] == a[pos] && vis[pos2])
pos2++;
if (a[pos2] != a[pos] || vis[pos2])
return;
vis[pos2] = ;
}
}
++tot;
for (int i = ; i <= n; i++)
ans[tot][i] = val[i];
} int main()
{
scanf("%d", &n);
cnt = n * (n - ) / ;
for (int i = ; i <= cnt; i++)
scanf("%d", &a[i]);
sort(a + , a + + cnt);
for (int i = ; i <= cnt; )
{
solve(i);
int j = i;
while (j <= cnt && a[j] == a[i])
j++;
i = j;
}
printf("%d\n", tot);
for (int i = ; i <= tot; i++)
{
for (int j = ; j <= n; j++)
printf("%d ", ans[i][j]);
printf("\n");
} 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 ...
随机推荐
- bzoj 1782: [Usaco2010 Feb]slowdown 慢慢游【dfs序+线段树】
考虑每头牛到达之后的影响,u到达之后,从1到其子树内的点需要放慢的都多了一个,p为u子树内点的牛ans会加1 用线段树维护dfs序,每次修改子树区间,答案直接单点查询p即可 #include<i ...
- 解方程 2014NOIP提高组 (数学)
解方程 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 输入描述 Input Description 输入文 ...
- 在sql语句中使用关键字
背景 开发过程中遇到了遇到了一句sql语句一直报错,看了一下字段名和表名都对应上了,但是还是一直报错 sql语句如下: update table set using = ""hh ...
- Java实现日期时间对象的使用
利用类对象计算日期 在利用Java语言进行信息系统开发中,经常需要对日期进行计算和转换,比如,设置了某活动的开始日期和结束日期,系统需要判断当前是否是该活动时间,在Java开发的信息系统中,通常日期以 ...
- Oracle11gR2设置连接数process与会话session值
近日构建的Web应用用户数量有所上升,后台总是打印无法打开数据库连接的错误信息: 000000a3 SystemOut O 9月 ::, ERROR - msg:打开数据库出错. 经查询发现需要更改数 ...
- Python---查看安装路径
python是解释型脚本语言,在执行时,逐句解释执行,不需要进行预编译.但需要有自身的Python解释器. 所以在执行Python代码时,需要指定python解释器. 指定解释器方法: 在文件开头添加 ...
- hdu5122 K.Bro Sorting
思路: 模拟. 实现: #include <iostream> #include <cstdio> using namespace std; ], n, t; int main ...
- phpstorm设置代码片段
tab 向后推进 shift+tab 向前推进 ctrl+d 复制整行 ctrl+Y删除整行 代码片段就是代码快捷键,如果你设置了www.baidu.com这些内容,但是不想一直重复的打,可以设置个代 ...
- 第2章 JavaScript语法
1.最好的做法是把<script>标签放到html文档的最后,</body>标签之前. 举例: ...... <script src="file.js" ...
- 百度人脸识别AI实践.doc
0, 前言 百度开放了很多AI能力,其中人脸识别就是其中之一. 本文对百度人脸识别AI进行实践检验,看看其使用效果如何. 鉴于是最为基础的实践,基本都是在其接口范例代码修改而来. 百度人脸识别AI网站 ...