74.Interesting Sequence(有趣的数列)(拓扑排序)
Interesting Sequence(有趣的数列)[Special judge]
题目概述:是否存在一个长度为n的整数数列,其任意连续p项之和为正数而任意连续q项之和为负数?
方法:连续项a[i],a[i+1]...a[j]和的性质容易联想到以前n项和的方式表达,即s[j]-s[i],简洁明了。
由此发现只需求一个满足要求的s[0]~s[n]。
其中s[i+p]-s[i]>0,s[i]-s[i-q]<0。即s[i]
同理有s[i]>s[i-p],s[i+q]。
这是拓扑排序的雏形。
更幸运的是,由于需要存在初始s[i]最大(拓扑排序起点),即要求存在i,使得s[i+p],s[i-q]不存在,故i+p>n,i-q<0,=>p+q>=n+2。
这表明s[i+p],s[i-q]最多只有一个存在(每个节点最多只有一个入度)。
s[i-p],s[i+q]也最多只有一个存在(每个节点最多只有一个出度)。
因此拓扑排序时只要删除一个入度就能进队列,无需额外记录节点入度。
代码:
#include
#include
using namespace std;
//sort记录s[i]的大小,
//sort[i]>sort[j]=>s[i]>s[j],
//sort[i]-sort[0]=s[i]-s[0]=s[i],
//a[i]=sort[i]-sort[i-1]。
int sort[1000001];
int main(){
int t,n,p,q,m,i,j;
queue mq;
scanf("%d",&t);
while(t--){
scanf("%d %d %d",&n,&p,&q);
if(p+q
printf("Impossible\n");
continue;
}
m=n;
for(i=n-p+1;i<=q-1;i++)
mq.push(i);
while(!mq.empty()){ //拓扑排序
i=mq.front();
mq.pop();
sort[i]=m--;
if(i-p>=0)
mq.push(i-p);
else if(i+q<=n)
mq.push(i+q);
}
if(m==-1){
for(i=1;i<=n-1;i++)
printf("%d
",sort[i]-sort[i-1]);
printf("%d\n",sort[n]-sort[n-1]);
}
else
printf("Impossible\n");
}
return 0;
}
74.Interesting Sequence(有趣的数列)(拓扑排序)的更多相关文章
- nyoj 349 (poj 1094) (拓扑排序)
Sorting It All Out 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 An ascending sorted sequence of distinct ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
- 洛谷P2017 [USACO09DEC]晕牛Dizzy Cows [拓扑排序]
题目传送门 晕牛Dizzy Cows 题目背景 Hzwer 神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. 题目描述 The cows have taken to racing each o ...
- [USACO09DEC]晕牛Dizzy Cows (拓扑排序)
https://www.luogu.org/problem/P2017 题目背景 Hzwer 神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. 题目描述 The cows have taken ...
- 拓扑排序 POJ2367Genealogical tree[topo-sort]
---恢复内容开始--- Genealogical tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4875 A ...
- ACM/ICPC 之 拓扑排序范例(POJ1094-POJ2585)
两道拓扑排序问题的范例,用拓扑排序解决的实质是一个单向关系问题 POJ1094(ZOJ1060)-Sortng It All Out 题意简单,但需要考虑的地方很多,因此很容易将code写繁琐了,会给 ...
- 2016"百度之星" - 初赛(Astar Round2A)Gym Class(拓扑排序)
Gym Class Accepts: 849 Submissions: 4247 Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65 ...
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
- [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
随机推荐
- C - K-inversions URAL - 1523 (dp + 线段树)
题目链接:https://cn.vjudge.net/contest/275079#problem/C 具体思路:我们可以分层的去建立,假设我们要找k层,我们可以先把满足1.2....k-1层的满足情 ...
- 关于boost 的smart_ptr 的使用问题
boost 的smart_ptr 库中含有好几种智能指针,大家用的最多的应该是shared_ptr ,为啥呢?好用,不用管他啥时候会自动删除等等,而且拷贝和复制都很到位, 但实际上,这个库也有问题,连 ...
- apache 各种配置
//apache 的网站配置文件 /usr/local/apache2/conf/extra/httpd-vhosts.conf -->在编辑这个文件前需要去httpd.conf把这个文件的注释 ...
- 使用常见的网络命令查看当前网络状态——Mac OS X篇
转载自:http://blog.csdn.net/zkh90644/article/details/50539948 操作系统拥有一套通用的实用程序来查明本地主机的有线或者无线链路状态和IP的连接情况 ...
- pyQt: eg3
import sys import urllib2 from PyQt4 import QtCore from PyQt4 import QtGui class Form(QtGui.QDialog) ...
- 1、CentOS 6 安装GitLab
1.安装和配置必需的依赖项 在CentOS上将系统防火墙打开HTTP和SSH访问. sudo yum install -y curl policycoreutils-python openssh-se ...
- Java容器---Arrays & Collections工具类
1.Array & Arrays 与Collection & Collections区别 (1)Collection": 是一个接口,与其子类共同组成一个Collection ...
- [BZOJ4945][Noi2017]游戏 2-sat
对于所有的x,我们枚举他的地图类型,事实上我们只需要枚举前两种地形就可以覆盖所有的情况. 之后就变成了裸的2-sat问题. 对于一个限制,我们分类讨论: 1.h[u]不可选,跳过 2.h[v]不可选, ...
- java EE : tomacat 基础
tomacat 目录结构 conf 配置文件 server.xml
- thinkphp5.0Traits引入
ThinkPHP 5.0开始采用trait功能(PHP5.4+)来作为一种扩展机制,可以方便的实现一个类库的多继承问题. Traits 是一种为类似 PHP 的单继承语言而准备的代码复用机制.Trai ...