参考答案


感觉这两题目都挺好的~~


T1 交朋友

简单描述:有n个人依次进入教室,每个人进入会找一个身高绝对值相差最小的人交朋友(相同时更想和高的交朋友),求每个人交的朋友.

Solution:

Sort,求出每个人的排名

逆向思维,从后往前(每次删除,然后剩余的都是可以选的)

链表存储前一个和后一个,每次删除发生改变

发组福利数据

in:

6
4 6 5 3 1 7

out:

2:1
3:2
4:1
5:4
6:2

// <T1.cpp> - Sun Oct 23 22:05:36 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is. #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define INF 0x7fffffff
using namespace std;
const int MAXN=100010;
inline int gi() {
register int w=0,q=0;register char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')q=1,ch=getchar();
while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
return q?-w:w;
}
int as[MAXN],rank[MAXN],h[MAXN],pre[MAXN],nex[MAXN];
bool cmp(int a,int b){return h[a]<h[b];}
int main()
{
freopen("T1.in","r",stdin);
freopen("T1.out","w",stdout);
int n=gi(),s;
for(int i=1;i<=n;i++)h[i]=gi(),rank[i]=i;
sort(rank+1,rank+1+n,cmp);
for(int i=1;i<=n;i++)
pre[rank[i]]=rank[i-1],nex[rank[i]]=rank[i+1];
for(int i=n;i>=2;i--){
s=INF;
if(!pre[i])s=h[i]-h[pre[i]];
if(!nex[i])s+=h[i]-h[nex[i]];
if(s<0)as[i]=pre[i];
else as[i]=nex[i];
nex[pre[i]]=nex[i];
pre[nex[i]]=pre[i];
}
for(int i=2;i<=n;i++)printf("%d:%d\n",i,as[i]);
return 0;
}

T2 交通中断

大意:给一个无向图,问第x个点中断时(与其他点连边全部删掉),从1号点到多少个点的最短路改变(包括不能到达,不包括x)

Solution:

先求出1到每个点的最短路,然后举删掉了哪个点,从1开始bfs(到达的是最短路,加入队列),这样贪心保证正确性,因为一个点可能可以从多个点跑相同长度的最短路到达.

发组福利数据

in:

5 7
1 2 1
1 2 0
1 3 4
2 3 3
4 5 1
3 4 2
1 4 3

out:

2:1
3:0
4:1
5:0

// <T2.cpp> - Sun Oct 23 22:05:36 2016
// This file is made by YJinpeng,created by XuYike's black technology automatically.
// Copyright (C) 2016 ChangJun High School, Inc.
// I don't know what this program is. #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#define IN inline
#define RG register
#define INF 0x7fffffff
using namespace std;
inline int gi() {
register int w=0,q=0;register char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')q=1,ch=getchar();
while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=getchar();
return q?-w:w;
}
struct SPFA{
static const int N=6010,M=100010;
queue<int>q;bool u[N];
int n,m,t;int d[N],fr[N];int to[M],ne[M],W[M];
IN void link(RG int u,RG int v,RG int w){
to[++t]=v;ne[t]=fr[u];fr[u]=t;W[t]=w;
}
void read(){
n=gi(),m=gi();
while(m--){
int u=gi(),v=gi(),w=gi();
link(u,v,w);link(v,u,w);
}
}
void Spfa(int begin){
for(int i=1;i<=n;i++)d[i]=INF;
q.push(begin);d[begin]=0;memset(u,0,sizeof(u));
while(!q.empty()){
int x=q.front();q.pop();u[x]=0;
for(int o=fr[x],y;y=to[o],o;o=ne[o])
if(d[x]+W[o]<d[y]){
d[y]=d[x]+W[o];
if(!u[y])u[y]=1,q.push(y);
}
}
}
void Work(){
read();Spfa(1);
while(!q.empty())q.pop();
for(int i=2;i<=n;i++){
memset(u,0,sizeof(u));
q.push(1);u[1]=1;
while(!q.empty()){
int x=q.front();q.pop();
for(int o=fr[x],y;y=to[o],o;o=ne[o])
if(y!=i&&d[x]+W[o]==d[y]&&!u[y])
u[y]=1,q.push(y);
}int ans=0;
for(int j=1;j<=n;j++)ans+=1-u[j];
printf("%d:%d\n",i,ans-1);
}
}
}e;
int main()
{
freopen("T2.in","r",stdin);
freopen("T2.out","w",stdout);
e.Work();
return 0;
}

  

【NOIP 2016】初赛-完善程序 & 参考答案的更多相关文章

  1. NOIP初赛:完善程序做题技巧

    最近写的文章好像还很多的.那么今天我们来讨论NOIP初赛的题型--完善程序.完善程序相对是比较难的题目了.全卷100分,完善程序占了大概26分,占比非常大.如果和英语考试试卷做比较,相当于首字母填空( ...

  2. NOIP提高组—— 问题求解 与 完善程序

    问题求解1: 甲乙丙丁四人在考虑周末要不要外出郊游. 已知①如果周末下雨,并且乙不去,则甲一定不去:②如果乙去,则丁一定去:③如果丙去,则丁一定不去:④如果丁不去,而且甲不去,则丙一定不去.如果周末丙 ...

  3. [NOIP]2016天天爱跑步

    [NOIP]2016天天爱跑步 标签: LCA 树上差分 NOIP Description 小C同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.<天天爱跑步>是 ...

  4. C#复习⑨(附带C#参考答案仅限参考)

    C#复习⑨ 2016年6月22日 14:28 C#考试题&参考答案:http://pan.baidu.com/s/1sld4K13 Main XML Comments & Pointe ...

  5. s15day12作业:MySQL练习题参考答案

    MySQL练习题参考答案   导出现有数据库数据: mysqldump -u用户名 -p密码 数据库名称 >导出文件路径           # 结构+数据 mysqldump -u用户名 -p ...

  6. 【转载】经典10道c/c++语言经典笔试题(含全部所有参考答案)

    经典10道c/c++语言经典笔试题(含全部所有参考答案) 1. 下面这段代码的输出是多少(在32位机上). char *p; char *q[20]; char *m[20][20]; int (*n ...

  7. 《招聘一个靠谱的iOS》面试题参考答案(下)

    相关文章: <招聘一个靠谱的iOS>面试题参考答案(上) 说明:面试题来源是微博@我就叫Sunny怎么了的这篇博文:<招聘一个靠谱的 iOS>,其中共55题,除第一题为纠错题外 ...

  8. python编程快速上手之第10章实践项目参考答案

      本章主要讲了python程序的调试,当程序有BUG或异常的时候,我们如何调试代码找出问题点.其实在本章之前的章节我们做练习的时候都会遇到各种各样的错语和异常,最初当不知道程序哪里出错的情况下不可否 ...

  9. python编程快速上手之第9章实践项目参考答案

    本章介介绍了shutil,zipfile模块的使用,我们先来认识一下这2个模块吧. 一.shutil模块 shutil模块主要用于对文件或文件夹进行处理,包括:复制,移动,改名和删除文件,在shuti ...

随机推荐

  1. 安装配置Keepalived

    一.在haproxy容器安装Keepalived 1.进入haproxy容器: docker exec -it h1 bash 2.apt-get update(因为haproxy容器为Ubuntu) ...

  2. 【面试题】LRU算法及编码实现LRU策略缓存

    概念 LRU(least recently used)就是将最近不被访问的数据给淘汰掉,LRU基于一种假设:认为最近使用过的数据将来被使用的概率也大,最近没有被访问的数据将来被使用的概率比较低. 原理 ...

  3. add list of symbols -- latex

    * add list of symbols -- latexinclude a *toc.tex* file in the *main.tex* in *main.tex*#+BEGIN_SRC la ...

  4. MT4系统自带指标代码

    MT4系统自带指标代码 ~ Accelerator Oscillator 震荡加速指标:                   double iAC() ~ Accumulation/Distribut ...

  5. github some rank

    github some rank http://githubrank.com/

  6. 【06】对AJAX的总结(转)

    对AJAX的总结   通过前面对 AJAX 的讲解,我们可以将 AJAX 请求分成以下几个步骤: 创建 XMLHttpRequest 对象: 设置事件处理函数,处理返回的数据: 初始化并发送请求. 可 ...

  7. js用for...in 这种遍历的方式

    var arr = new Array("first", "second", "third") for(var item in arr) { ...

  8. HDU 5420 Victor and Proposition

    Victor and Proposition Time Limit: 6000ms Memory Limit: 524288KB This problem will be judged on HDU. ...

  9. bzoj 2653 middle (可持久化线段树)

    middle Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 1981  Solved: 1097[Submit][Status][Discuss] D ...

  10. POJ 1061 青蛙的约会【扩欧】

    题意: 两只青蛙在地球同一纬度不同位置x,y向同一方向跳,每只青蛙跳的长度不同m,n,纬线总长度l,问两只青蛙是否能相遇,跳几次才相遇. 分析: 可知,问题可转化为求(m−n)∗a≡(y−x)(mod ...