URAL 2072 Kirill the Gardener 3

URAL 2072

思路:

1.状压dp+记忆化搜索

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))

const int INF=0x3f3f3f3f;
int dp[(1<<20)+5];
int a[25];
int n;
int dfs(int sum,int sta){
    if(dp[sta]!=INF)return dp[sta];
    for(int i=0;i<n;i++){
        if(sta&(1<<i)){
            int temp=sta-(1<<i);
            int tsum=sum-a[i];
            int l=(i-1+n)%n;
            int r=(i+1)%n;
            if(sta&(1<<l))temp-=1<<l,tsum-=a[l];
            if(sta&(1<<r))temp-=1<<r,tsum-=a[r];
            dp[sta]=min(dp[sta],dfs(tsum,temp)+tsum);
        }
    }
    return dp[sta];
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int sum=0;
    mem(dp,INF);
    dp[0]=0;
    cin>>n;
    for(int i=0;i<n;i++)cin>>a[i],sum+=a[i];
    cout<<dfs(sum,(1<<n)-1)<<endl;
    return 0;
} 

2.dfs+剪枝

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))

const int N=30;
const int INF=0x7f7f7f7f;
int a[N],sum[N];
bool vis[N];
int ans=INF,n;
void dfs(int res,int t){
    if(t>=ans)return ;//剪枝
    if(res==0){
        ans=min(ans,t);
    }
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            int l=i-1;
            int r=i+1;
            if(l==0)l=n;
            if(r==n+1)r=1;
            if(vis[l])l=25;
            if(vis[r])r=25;
            vis[l]=vis[i]=vis[r]=true;
            dfs(res-a[l]-a[i]-a[r],t+res-a[l]-a[i]-a[r]);
            vis[l]=vis[i]=vis[r]=false;
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int sum=0;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i],sum+=a[i];
    dfs(sum,0);
    cout<<ans<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/widsom/p/8370706.html

时间: 2024-10-30 20:32:11

URAL 2072 Kirill the Gardener 3的相关文章

URAL 2002. Test Task(登陆模拟 map )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2002 2002. Test Task Time limit: 0.5 second Memory limit: 64 MB It was an ordinary grim October morning. The sky was covered by heavy gray clouds. It was a little rainy. The rain drops fell on the win

URAL 2005. Taxi for Programmers (最短路 数学啊)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2005 2005. Taxi for Programmers Time limit: 0.5 second Memory limit: 64 MB The clock shows 11:30 PM. The sports programmers of the institute of maths and computer science have just finished their trai

ural 2030

Awesome Backup System Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice URAL 2030 Description It is known that all people can be divided into two groups: those who have never lost important data and t

URAL 2003. Simple Magic(数学啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2003 2003. Simple Magic Time limit: 1.0 second Memory limit: 64 MB Do you think that magic is simple? That some hand-waving and muttering incomprehensible blubber is enough to conjure wonderful garden

URAL 2000. Grand Theft Array V(贪心啊)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2000 2000. Grand Theft Array V Time limit: 0.5 second Memory limit: 64 MB A long anticipated game called Grand Theft Array V is about to appear in shops! What, haven't you heard of it? Then we must te

URAL 2002. Test Task (阅读理解)

2002. Test Task Time limit: 0.5 second Memory limit: 64 MB It was an ordinary grim October morning. The sky was covered by heavy gray clouds. It was a little rainy. The rain drops fell on the windows with quiet bangs. Ilya was sitting at the computer

URAL 2040 Palindromes and Super Abilities 2(回文树)

Palindromes and Super Abilities 2 Time Limit: 1MS   Memory Limit: 102400KB   64bit IO Format: %I64d & %I64u Status Description Dima adds letters s1, -, sn one by one to the end of a word. After each letter, he asks Misha to tell him how many new pali

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀