博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1653: [Usaco2006 Feb]Backward Digit Sums
阅读量:6568 次
发布时间:2019-06-24

本文共 2399 字,大约阅读时间需要 7 分钟。

1653: [Usaco2006 Feb]Backward Digit Sums

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 285  Solved: 215
[][]

Description

FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4) might go like this: 3 1 2 4 4 3 6 7 9 16 Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities. Write a program to help FJ play the game and keep up with the cows.

Input

* Line 1: Two space-separated integers: N and the final sum.

Output

* Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.

Sample Input

4 16

Sample Output

3 1 2 4
OUTPUT DETAILS:
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4
is the lexicographically smallest.

HINT

 

Source

 题解:这个嘛,本来还想什么高端洋气的算法的,可是再想想果断决定——弃疗——10!不过才3628800而已嘛,(具体说算法嘛,很显然对于原数列,每个数依次的最终累计次数即是杨辉三角形第N行的对应数字,别的没了),可是再一想,有点不对——你不是每次还要判断此组解是否合法么?这样子复杂度可还要再×10哦(36288000,3kW多了,这下子可危险啊,虽然事实上只要有解的话,由于杨辉三角形的对称性,那么最多理论上一半的时间即可找到解)。。。可是结果是——228kb 60ms Accept我也是醉了。。。

 

1 var 2    i,j,k,l,m,n:longint; 3    a:array[0..20] of longint; 4    b:array[0..20,0..20] of longint; 5 procedure swap(var x,Y:longint); 6           var z:longint; 7           begin 8                z:=x;x:=y;y:=z; 9           end;10 procedure sort(l,r:longint);11           var i,j,x,y:longint;12           begin13                i:=l;j:=r;x:=a[(l+r) div 2];14                repeat15                      while a[i]
x do dec(j);17 if i<=j then18 begin19 swap(a[i],a[j]);20 inc(i);dec(j);21 end;22 until i>j;23 if l
a[j] do dec(j);46 k:=n;47 while a[j-1]>a[k] do dec(k);48 swap(a[j-1],a[k]);49 sort(j,n);50 end;51 end.52

 

转载于:https://www.cnblogs.com/HansBug/p/4170437.html

你可能感兴趣的文章
python 网络爬虫学习笔记(一)
查看>>
gvim 实现自动全文排版
查看>>
云计算如何帮助直播行业发展
查看>>
基于Spring可扩展Schema提供自定义配置支持(spring配置文件中 配置标签支持)
查看>>
搞懂OpenLDAP
查看>>
数组名和数组名取地址的区别
查看>>
hive常用sql语句
查看>>
nc 命令传文件
查看>>
IIS中的sc-win32-status——Win32状态详细说明
查看>>
我的友情链接
查看>>
利用数据存储技术实现数据安全合理备份
查看>>
我的友情链接
查看>>
js_sqlite_ADODB.Connection
查看>>
hibernate开启二级缓存
查看>>
jsp自定义标签学习
查看>>
最短路径问题经典题目汇总
查看>>
iOS培训教程——设置默认语言
查看>>
zabbix登山路——简单监控_各项参数解析
查看>>
关于链表和指针变量的使用说明,可用于框架设计
查看>>
12306新版上线 还是不能选上下铺
查看>>