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 ...
随机推荐
- AngularJs 文件上传(实现Multipart/form-data 文件的上传)
<!-- 上传yml文件 --> <div class="blackBoard" ng-show="vm.showUpop==true"> ...
- linux的fwrite()使用方法,当前时间写入文本的程序
fwrite函数 1.函数功能 用来读写一个数据块. 2.一般调用形式 fwrite(buffer,size,count,fp); 3.说明 (1)buffer:是一个指针,对fread来说,它是读入 ...
- CGIC简明教程(转摘)
CGIC简明教程 本系列的目的是演示如何使用C语言的CGI库“CGIC”完成Web开发的各种要求. ********************************* 基础知识 1 ...
- 重置HTML标签样式
;;} header,footer,section,article,aside,nav,hgroup,address,figure,figcaption,menu,details{display:bl ...
- HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...
- hive(七)hive-运行方式、GUI接口、权限管理
1.Hive运行方式: 命令行方式cli:控制台模式 脚本运行方式(实际生产环境中用最多) JDBC方式:hiveserver2 web GUI接口 (hwi.hue等) 1.1Hive在CLI模 ...
- LeetCode679. 24 Game
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...
- csu 1806 & csu 1742 (simpson公式+最短路)
1806: Toll Time Limit: 5 Sec Memory Limit: 128 MB Special JudgeSubmit: 256 Solved: 74[Submit][Sta ...
- 深入浅出Mysql索引
索引的出现其实就是为了提高数据查询的效率,就像书的目录一样. 索引模型有三种常见.也比较简单的数据结构分别是哈希表.有序数组和搜索树. 哈希表 哈希表是一种以键 - 值(key-value)存储数据的 ...
- 什么是VC、PE、LP、GP?
天使基金主要关注原创项目构思和小型初创项目,投资规模大多在300万元以下:风险投资关注初创时期企业长期投资,规模在1000万元以下:私募股权投资主要关注3年内可以上市的成熟型企业. VC即ventur ...