跳过正文
  1. Posts/

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

·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}

相关文章

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

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

codeforces 334 B. Eight Point Sets (暴力)

·1 分钟
题目链接 题意:给出8个点,问能否构成一个8元素集合,使得x1/* *********************************************** Author :111qqz Created Time :2016年03月31日 星期四 13时39分27秒 File Name :code/cf/problem/334B.cpp ************************************************ */

codeforces croc2016 A. Amity Assessment (暴力)

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