洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

题目描述

Each of Farmer John‘s N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i <= 25,000). The cows are so proud of it that each one now wears her number in a gangsta manner engraved in large letters on a gold plate hung around her ample bovine neck.

Gangsta cows are rebellious and line up to be milked in an order called ‘Mixed Up‘. A cow order is ‘Mixed Up‘ if the sequence of serial numbers formed by their milking line is such that the serial numbers of every pair of consecutive cows in line differs by more than K (1 <= K <= 3400). For example, if N = 6 and K = 1 then 1, 3, 5, 2, 6, 4 is a ‘Mixed Up‘ lineup but 1, 3, 6, 5, 2, 4 is not (since the consecutive numbers 5 and 6 differ by 1).

How many different ways can N cows be Mixed Up?

For your first 10 submissions, you will be provided with the results of running your program on a part of the actual test data.

POINTS: 200

约翰家有N头奶牛,第i头奶牛的编号是Si,每头奶牛的编号都是唯一的。这些奶牛最近 在闹脾气,为表达不满的情绪,她们在挤奶的时候一定要排成混乱的队伍。在一只混乱的队 伍中,相邻奶牛的编号之差均超过K。比如当K = 1时,1, 3, 5, 2, 6, 4就是一支混乱的队伍, 而1, 3, 6, 5, 2, 4不是,因为6和5只差1。请数一数,有多少种队形是混乱的呢?

输入输出格式

输入格式:

* Line 1: Two space-separated integers: N and K

* Lines 2..N+1: Line i+1 contains a single integer that is the serial number of cow i: S_i

输出格式:

* Line 1: A single integer that is the number of ways that N cows can be ‘Mixed Up‘. The answer is guaranteed to fit in a 64 bit integer.

输入输出样例

输入样例#1: 复制

4 1
3
4
2
1

输出样例#1: 复制

2

说明

The 2 possible Mixed Up arrangements are:

3 1 4 2

2 4 1 3

思路:

40分暴力

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 17
using namespace std;
int N,K,ans;
int num[MAXN],vis[MAXN];
void dfs(int now,int pre){
    if(now==N){
        ans++;
        return ;
    }
    for(int i=1;i<=N;i++)
        if(!vis[i]&&abs(num[pre]-num[i])>K){
            vis[i]=1;
            dfs(now+1,i);
            vis[i]=0;
        }
}
int main(){
    scanf("%d%d",&N,&K);
    for(int i=1;i<=N;i++)
        scanf("%d",&num[i]);
    dfs(0,-1);
    cout<<ans;
}

70分暴力:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 17
using namespace std;
int N,K,ans;
int num[MAXN],vis[MAXN];
void dfs(int now,int pre){
    if(now==N){
        ans++;
        return ;
    }
    for(int i=1;i<=N;i++)
        if(!vis[i]&&abs(pre-num[i])>K){
            vis[i]=1;
            dfs(now+1,num[i]);
            vis[i]=0;
        }
}
int main(){
    scanf("%d%d",&N,&K);
    for(int i=1;i<=N;i++)
        scanf("%d",&num[i]);
    dfs(0,-3400);
    cout<<ans;
}

100分记忆化搜索

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 17
using namespace std;
int N,K;
int num[MAXN];
bool vis[MAXN];
long long f[1<<16][20];
long long dfs(int sta,int cnt,int pre){
    if(cnt==N) return f[sta][pre]=1;
    if(f[sta][pre]!=-1)    return f[sta][pre];
    long long res=0;
    for(int i=1;i<=N;i++)
        if(!vis[i]&&abs(num[i]-num[pre])>K){
            vis[i]=1;
            res+=dfs(sta+(1<<(i-1)),cnt+1,i);
            vis[i]=0;
        }
    f[sta][pre]=res;
    return res;
}
int main(){
    memset(f,-1,sizeof(f));
    scanf("%d%d",&N,&K);
    for(int i=1;i<=N;i++)
        scanf("%d",&num[i]);
    num[0]=-3400;
    dfs(0,0,0);
    printf("%lld",f[0][0]);
}

原文地址:https://www.cnblogs.com/cangT-Tlan/p/8641629.html

时间: 2024-09-30 18:44:43

洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows的相关文章

题解 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

题解 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题目链接 数据规模16-20的都是状压 如果要每一位都压序号的话空间肯定是不够的,所以每一位只能是0或1.1表示有这头牛,0表示没有这头牛.显然每个位置的选择和他两边的牛有关,所以我们就可以定义这样的状态: f[i][j]表示使用集合i的牛,其中最后一头牛的序号为j时的总方案数.答案从f[(1 << n) - 1][n]累加即可. 转移也比较好想,从i中枚举选出倒数第二头牛,作为子状态的最后一头牛,注意边界情况.

P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows

—————————————————————————————————————————————————————————— 我真睿智 重叠变量名查了十多分钟的错 很好的状压题,无论是隐形的表示还有计数 ———————————————————————————— #include<bits/stdc++.h> using namespace std; long long int n,kk,ans,a[17],f[17][1<<17],cows[17]; int main() { cin>

洛谷 P2915 【[USACO08NOV]奶牛混合起来Mixed Up Cows】

类似于n皇后的思想,只要把dfs表示放置情况的数字压缩成一个整数,就能实现记忆化搜索了. 一些有关集合的操作: {i}在集合S内:S&(1<<i)==1: 将{i}加入集合S:S=S|(1<<i): 集合S内包含了{0,1,2,...,n-2,n-1}:S==(1<<n)-1: 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 using namespa

[USACO08NOV]奶牛混合起来Mixed Up Cows

题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i <= 25,000). The cows are so proud of it that each one now wears her number in a gangsta manner engraved in large letters on a gold plate hung around her

[luoguP2915] [USACO08NOV]奶牛混合起来Mixed Up Cows(DP)

传送门 f[i][S] 表示当前集合为 S,最后一个数为 i 的最优解 f[i][S] += f[j][S - i] (j, i ∈ S && j != i && abs(a[i] - a[j]) > k) ——代码 1 #include <cstdio> 2 #include <iostream> 3 #define LL long long 4 5 int a[17]; 6 int n, m, k; 7 LL ans, f[17][1 &l

Luogu2917_ [USACO08NOV]奶牛混合起来Mixed Up Cows_KEY

题目传送门 看到数据范围就果断装压. 设f[i][j]表示i状态下最后一个数字为a[j]. code: #include <cstdio> using namespace std; int N,K,a[17]; long long f[1<<16][17]; inline int abs(int x){return x>0?x:-x;} int main() { scanf("%d%d",&N,&K); register int i,j,k

洛谷 P2858 [USACO06FEB]奶牛零食Treats for the Cows

题目描述 FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats are interesting for many re

洛谷 P2919 [USACO08NOV]守护农场Guarding the Farm

P2919 [USACO08NOV]守护农场Guarding the Farm 题目描述 The farm has many hills upon which Farmer John would like to place guards to ensure the safety of his valuable milk-cows. He wonders how many guards he will need if he wishes to put one on top of each hill

洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk

题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是不同的.此外,就像每头奶牛每天只能挤出固定数量的奶,每位奶农每天能提供的牛奶数量是一定的.每天Marry乳业可以从奶农手中采购到小于或者等于奶农最大产量的整数数量的牛奶. 给出Marry乳业每天对牛奶的需求量,还有每位奶农提供的牛奶单价和产量.计算采购足够数量的牛奶所需的最小花费. 注:每天所有奶农