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 ...
随机推荐
- 39、请用代码简答实现stack
栈和队列是两种基本的数据结构,同为容器类型.两者根本的区别在于: stack:后进先出 queue:先进先出 PS:stack和queue是不能通过查询具体某一个位置的元素而进行操作的.但是他们的排列 ...
- 设计模式之Composite
设计模式总共有23种模式这仅仅是为了一个目的:解耦+解耦+解耦...(高内聚低耦合满足开闭原则) Composite定义? 将对象以树形结构组织起来,以达成“部分-整体” 的层次结构. 想到Compo ...
- Shell-history命令加记录用户IP
记录输入的命令 history命令可以查看用户输入过的命令,一个典型history命令输出如下: 980 2017-05-29 20:17:37 cd - 981 2017-05-29 20:17:4 ...
- sicily 1017. Rate of Return
Description Jill has been investing in a mutual fund for a while. Since her income has varied, the a ...
- idea关于断点的补充
黑背景版: 先编译好要调试的程序.1.设置断点
- leetcode 之trap water(8)
这题不太好想.可以先扫描找到最高的柱子,然后分别处理两边:记录下当前的局部最高点,如果当前点小于局部最高点,加上, 反则,替换当前点为局部最高点. int trapWater(int A[], int ...
- Effective C++笔记(三):资源管理
参考:http://www.cnblogs.com/ronny/p/3745098.html 资源:动态分配的内存.文件描述器.互斥锁.图形界面中的字型与笔刷.数据库连接以及网络sockets等, ...
- C语言 五子棋
#include <stdlib.h> #include <stdio.h> #include <conio.h> #include <string.h> ...
- iOS客户端学习之AES加密
数据加密在解密在软件开发过程中举足轻重的作用,可能有的公司在加密的时候有自己公司内部一套设计的算法,而在这方面不想浪费太大精力就可以去考虑使用第三方提供的加密算法,如AES加密算法,本篇内容介绍开源中 ...
- 【严蔚敏】【数据结构题集(C语言版)】1.16 自大至小依次输出读入的三个整数X,Y,Z
#include <stdio.h> #include<stdlib.h> int main() { int x,y,z,temp; scanf("%d%d%d&qu ...