P1118 [USACO06FEB]数字三角形`Backward Digit Su`… 回溯法

  

有这么一个游戏:

写出一个11至NN的排列a_iai?,然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少11,直到只剩下一个数字位置。下面是一个例子:

3,1,2,43,1,2,4

4,3,64,3,6

7,97,9

1616

最后得到1616这样一个数字。

现在想要倒着玩这样一个游戏,如果知道NN,知道最后得到的数字的大小sumsum,请你求出最初序列a_iai?,为11至NN的一个排列。若答案有多种可能,则输出字典序最小的那一个。

[color=red]管理员注:本题描述有误,这里字典序指的是1,2,3,4,5,6,7,8,9,10,11,121,2,3,4,5,6,7,8,9,10,11,12

而不是1,10,11,12,2,3,4,5,6,7,8,91,10,11,12,2,3,4,5,6,7,8,9[/color]

输入输出格式

输入格式:

两个正整数n,sumn,sum。

输出格式:

输出包括11行,为字典序最小的那个答案。

当无解的时候,请什么也不输出。(好奇葩啊)

输入输出样例

输入样例#1: 复制

4 16

输出样例#1: 复制

3 1 2 4

说明

对于40\%40%的数据,n≤7n≤7;

对于80\%80%的数据,n≤10n≤10;

对于100\%100%的数据,n≤12,sum≤12345n≤12,sum≤12345。

一开始用模拟法  超时3个点

然后观察发现  累合的乘数为杨辉三角   用杨辉三角优化 超时2个点

#include<bits/stdc++.h>
using namespace std;
//input
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);i--)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m);
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define inf 0x3f3f3f3f
#define REP(i,N)  for(int i=0;i<(N);i++)
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
#define N 105
int a[16];
int yhsj[15][15];
int main()
{
    int n;
    int sum;
    RII(n,sum);
    rep(i,1,n)
    a[i]=i;

    yhsj[1][1]=1;
    rep(i,2,n)
    rep(j,1,i)
    yhsj[i][j]=yhsj[i-1][j-1]+yhsj[i-1][j];

    int b[16];
    do
    {
        int all=0;
        int ok=1;
        rep(i,1,n)
        {
            all+=a[i]*yhsj[n][i];
            if(all>sum){ok=0;break;}
        }
        if(ok&&all==sum)
        {
            rep(i,1,n)
            {
                if(i!=1)
                    printf(" ");
                printf("%d",a[i]);
            }
            break;
        }

    }
    while(next_permutation(a+1,a+1+n));
    return 0;
}

2 TLE

参考了大佬的做法

其实只要加一个关键剪枝即可

如果加到i处过不去了   把i及其后面的数降序排列好  下一个next就是 累合杨辉三角的最小值了  !!!!(因为杨辉三角中间大  两边小)

#include<bits/stdc++.h>
using namespace std;
//input
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);i--)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m);
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define inf 0x3f3f3f3f
#define REP(i,N)  for(int i=0;i<(N);i++)
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
#define N 105
int a[16];
int yhsj[15][15];
int main()
{
    int n;
    int sum;
    RII(n,sum);
    rep(i,1,n)
    a[i]=i;

    yhsj[1][1]=1;
    rep(i,2,n)
    rep(j,1,i)
    yhsj[i][j]=yhsj[i-1][j-1]+yhsj[i-1][j];

    do
    {
        int all=0;
        int ok=1;
        rep(i,1,n)
        {
            all+=a[i]*yhsj[n][i];
            if(all>sum){ok=0;sort(a+i,a+1+n,greater<int>());   break;}
        }
        if(ok&&all==sum)
        {
            rep(i,1,n)
            {
                if(i!=1)
                    printf(" ");
                printf("%d",a[i]);
            }
            break;
        }
    }
    while(next_permutation(a+1,a+1+n));
    return 0;
}

其实这题用dfs回溯法更见简单高效   上面那个剪枝其实很难想到

不要过度依赖STL  有时候效率非常低下

#include<bits/stdc++.h>
using namespace std;
//input
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);i--)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m);
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define inf 0x3f3f3f3f
#define REP(i,N)  for(int i=0;i<(N);i++)
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
#define N 105

int a[16];
int yhsj[15][15];
int vis[16];
int sum,n;
int ok=0;
int ans[16];
void dfs(int now,int all)
{
    if(ok)return ;
    if(now==n+1&&all==sum)
    {
        ok=1;
        rep(i,1,n)
        {
            if(i!=1)
            printf(" ");
            printf("%d",ans[i]);
        }
        return ;
    }
    rep(i,1,n)
    {
        if(vis[i])continue;
        if(all+i*yhsj[n][now]>sum)continue;
        vis[i]=1;
        ans[now]=i;
        dfs(now+1,all+i*yhsj[n][now]);
        vis[i]=0;
    }
    return ;
}
int main()
{
    RII(n,sum);
    rep(i,1,n)
    a[i]=i;

    yhsj[1][1]=1;
    rep(i,2,n)
    rep(j,1,i)
    yhsj[i][j]=yhsj[i-1][j-1]+yhsj[i-1][j];

    dfs(1,0);

    return 0;
}

原文地址:https://www.cnblogs.com/bxd123/p/10664413.html

时间: 2024-08-28 21:10:11

P1118 [USACO06FEB]数字三角形`Backward Digit Su`… 回溯法的相关文章

做题记录: P1118 [USACO06FEB]数字三角形Backward Digit Su…

P1118 [USACO06FEB]数字三角形Backward Digit Su- /*思路:设一开始的n个数为a1.a2.a3...an, 一步一步合并就可以用a1..an表示出最后剩下来 的数,不难发现其中a1..an的系数恰好就是第n层 杨辉三角中的数.所以我们可以先处理出第n层杨 辉三角中的数,然后根据这一层中的数搜索即可.*/ #include<iostream> #include<cstdio> #include<fstream> #include<a

P1118 [USACO06FEB]数字三角形Backward Digit Su…

P1118 [USACO06FEB]数字三角形Backward Digit Su… 题目描述 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 rep

P1118 [USACO06FEB]数字三角形`Backward Digit Su`…

题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 to N(1 \le N \le 10)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 num

洛谷P1118 [USACO06FEB]数字三角形 搜索

洛谷P1118 [USACO06FEB]数字三角形Backward Digit Su-     搜索 这题我们发现每一个位置的加权就是 杨辉三角 yh[ n ][ i ] 然后我们就可以求 n! 暴力 ,但是会 TLE 额 好像是会T 因为12! 已经 4亿了然后我们加一个强力剪枝 如果当前求出来的 s 已经大于 sum了,因为没有负的加权,也就是说这一路是没有用了的,在继续搜下去也不能更新答案了,那么就直接退出 . 1 #include <cstdio> 2 #include <cma

[luogu p1118] [USACO06FEB]数字三角形

题面 题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from \(1\) to$ N(1 \le N \le 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 numb

[USACO06FEB]数字三角形

题目描述 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 lef

洛谷P1118 数字三角形游戏

洛谷1118 数字三角形游戏 题目描述 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直到只剩下一个数字位置.下面是一个例子:     3   1   2   4       4   3   6         7   9          16 最后得到16这样一个数字. 现在想要倒着玩这样一个游戏,如果知道N,知道最后得到的数字的大小sum,请你求出最初序列a[i],为1-N的一个

数字三角形

数字三角形必须经过某一个点,使之走的路程和最大 输入格式: 第1行n,表示n行 (n<=25), 第2到n+1行为每个的权值,第n+2行为两个数x,y表示必须经过的点 输出格式: 输出最大值 样例1 输入: 2 1 1 1 1 1 输出: 2 //11 月 23 日 2015 #include <stdio.h> int num[26][26];//存储数字三角形的权值 int route[26][2];//记录临时最优路径 int n; int s1,s2;//以特殊点分为上半段和下半

BZOJ1653: [Usaco2006 Feb]Backward Digit Sums

1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 207  Solved: 161[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 cer