↓ 跳过正文
  1. Posts/

BZOJ 1653: [Usaco2006 Feb]Backward Digit Sums(暴力)

·560 字·2 分钟

1653: [Usaco2006 Feb]Backward Digit Sums
#

Time Limit: 5 Sec Memory Limit: 64 MB Submit: 349 Solved: 258 [Submit][Status][Discuss]

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 4OUTPUT DETAILS:

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

思路:n很小。。一开始做得时候把公式推错了。。3+3算成了4.我的内心是崩溃的。。

其实直接暴力就好。可以用next_permutation来生成全排列,然后判断是否合法。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年04月12日 星期二 10时54分23秒
 4File Name :code/bzoj/1653.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33int n;
34int sum;
35int a[15];
36int tmp[15];
37int main()
38{
39	#ifndef  ONLINE_JUDGE
40	freopen("code/in.txt","r",stdin);
41  #endif
42
43	scanf("%d %d",&n,&sum);
44	for ( int i = 1 ; i <= n ; i++) a[i] = i;
45
46	do
47	{
48	    for ( int i = 1 ; i <= n ; i++) tmp[i] = a[i];
49	    for ( int i = 1 ; i < n ; i++)
50		for ( int j = 1 ; j <= n - i ; j ++)
51		    tmp[j]+=tmp[j+1];
52	    if (tmp[1]==sum)
53	    {
54		for ( int i = 1 ; i< n ; i++)
55		    printf("%d ",a[i]);
56		printf("%d\n",a[n]);
57		break;
58	    }
59	}while (next_permutation(a+1,a+n+1));
60
61
62  #ifndef ONLINE_JUDGE
63  fclose(stdin);
64  #endif
65    return 0;
66}

相关文章

BZOJ 1622: [Usaco2008 Open]Word Power 名字的能量 (暴力)

·808 字·2 分钟
1622: [Usaco2008 Open]Word Power 名字的能量 # Time Limit: 5 Sec Memory Limit: 64 MB Submit: 462 Solved: 228 [Submit][Status][Discuss] Description # 约翰想要计算他那N(1≤N≤1000)只奶牛的名字的能量.每只奶牛的名字由不超过1000个字待构成,没有一个名字是空字体串, 约翰有一张“能量字符串表”,上面有M(1≤M≤100)个代表能量的字符串.每个字符串由不超过30个字体构成,同样不存在空字符串.一个奶牛的名字蕴含多少个能量字符串,这个名字就有多少能量.所谓“蕴含”,是指某个能量字符串的所有字符都在名字串中按顺序出现(不一定一个紧接着一个). 所有的大写字母和小写字母都是等价的.比如,在贝茜的名字“Bessie”里,蕴含有“Be” “sI”“EE”以及“Es”等等字符串,但不蕴含“lS”或“eB”.请帮约翰计算他的奶牛的名字的能量.

bzoj 1599: [Usaco2008 Oct]笨重的石子 (暴力)

·624 字·2 分钟
1599: [Usaco2008 Oct]笨重的石子 # Time Limit: 10 Sec Memory Limit: 162 MB Submit: 886 Solved: 614 [Submit][Status][Discuss] Description # 贝西喜欢棋盘游戏和角色扮演类游戏所以她说服Farmer John把她带到玩具店,在那里,她购买了三个不同的骰子,这三个质量均匀的骰子,分别有S1,S2,S3个面。(2 <= S1 <= 20; 2 <= S2 <= 20; 2 <= S3 <= 40). 贝西掷啊掷啊掷啊,想要知道出现几率最大的和是多少。 问题给出三个骰子的面数,让你求出出现几率最大的和是多少。如果有很多种和出现的几率相同,那么就输出小的那一个。

codeforces #346 div 2 C. Tanya and Toys (暴力乱搞)

·613 字·2 分钟
题目链接 题意:有1E9个礼物,第i个礼物价钱是i,然后现在已经有n个不重复的礼物,a[i],m元钱,想尽可能多得买不同种类的礼物,还能买多少个。 思路:先不考虑已经买的,从1连续买到k,然后考虑在这个区间内已经买的,等于实际上没有花钱。 反正就是暴力搞啊搞啊。。我也不知道怎么搞。。 结果最后。。方案数为0的时候。。。最后一个答案我是单独输出的。。忘了判断了。。。所以会输出两个0.宝宝心里苦啊。。。。。。。。

codeforces 334 B. Eight Point Sets (暴力)

·188 字·1 分钟
题目链接 题意:给出8个点,问能否构成一个8元素集合,使得x1/* *********************************************** Author :111qqz Created Time :2016年03月31日 星期四 13时39分27秒 File Name :code/cf/problem/334B.cpp ************************************************ */ #include #include #include #include #include #include #include #include #include #include #include #include #define fst first #define sec second #define lson l,m,rt«1 #define rson m+1,r,rt«1|1 #define ms(a,x) memset(a,x,sizeof(a)) typedef long long LL; #define pi pair < int ,int > #define MP make_pair

codeforces croc2016 A. Amity Assessment (暴力)

·340 字·1 分钟
题目链接 题意:2×2的格子,有三个位置分别放”A“ “B” “C” ,一个位置为空。只有和空位相邻位置上的字母能移动到空位。没有其他移动规则。现在给出两个状态。问能否互相转化。 思路: 貌似可以dfs…?但是一共才2*2,可以直接暴力枚举。 手写一种变换最多能有12种。