分治

数列分治

POJ 1854: Evil Straw Warts Live
题解链接 http://www.hankcs.com/program/algorithm/poj-1854-evil-straw-warts-live.html
代码如下

/*

*/
#define method_1
#ifdef method_1
/*
题解链接 http://www.hankcs.com/program/algorithm/poj-1854-evil-straw-warts-live.html 
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<ctime>
#include<string>
#define D(x) cout<<#x<<" = "<<x<<"  "
#define E cout<<endl
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const int maxn=8000+5;
const int maxnum=26+5;
const int INF=0x3f3f3f3f;
char s[maxn];
int T,n,ans,cnt[maxnum];
void init(){
    ans=0;
    memset(cnt,0,sizeof(cnt));
}
bool check(){
    for(int i=0;i<=n-1;i++) cnt[s[i]-'a']++;
    int res=0;
    for(int i=0;i<=maxnum-5;i++) if(cnt[i]&1) res++;
    return res<=1; 
}
void solve(){
    int l=0,r=n-1;
    while(l<r){
        if(s[l]==s[r]) l++,r--;
        else{
            int posl=INF,posr=-INF;
            for(int i=l+1;i<r;i++) if(s[i]==s[r]){
                posl=i;break;
            }
            for(int i=r-1;i>l;i--) if(s[i]==s[l]){
                posr=i;break;
            }
            if(posl-l<r-posr){
                ans+=posl-l;
                for(int i=posl;i>=l+1;i--) swap(s[i],s[i-1]);
            }
            else{
                ans+=r-posr;
                for(int i=posr;i<r;i++) swap(s[i],s[i+1]);
            }
        }
    }
}
int main() {
//  ios::sync_with_stdio(false);
    //freopen("Evil Straw Warts Live.in","r",stdin);
    scanf("%d",&T);
    while(T--){
        init();
        scanf("%s",s);
        n=strlen(s);
        if(!check()){
            printf("Impossible\n");
            continue;
        }
        solve();
        printf("%d\n",ans);
    }
    return 0;
}
#endif
#ifdef method_2
/*

*/

#endif
#ifdef method_3
/*

*/

#endif

平面分治

树分治

POJ 2114: Boatherds
简单的点分治,维护一下dis时顺便记录一下所处的子树编号即可,可以避开繁复的容斥过程。
头疼的是,我在求树的重心时,把max_part=max(max_part,s[y]);写成了max_part=max(max_part,s[x]);结果WA了半天。
代码如下

/*

*/
#define method_1
#ifdef method_1
/*
简单的点分治,维护一下dis时顺便记录一下所处的子树编号即可,可以避开繁复的容斥过程。
头疼的是,我在求树的重心时,把max_part=max(max_part,s[y]);写成了max_part=max(max_part,s[x]);结果WA了半天。 
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<ctime>
#include<string>
#define D(x) cout<<#x<<" = "<<x<<"  "
#define E cout<<endl
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const int maxn=10000+5;
const int maxm=100+5;
const int INF=0x3f3f3f3f;
int n,X; //判断是否存在两点距离为X 
bool flag;
int head[maxn],tot;
struct node{
    int from,to,c;
}edge[maxn<<1];
void add(int from,int to,int c){
    edge[++tot].from=head[from],head[from]=tot,edge[tot].to=to,edge[tot].c=c;
}
void init(){
    memset(head,0,sizeof(head));
    tot=1;
}
struct node1{
    int d,b;//distance到目前分治中心的距离 belong所在的子树 
    bool operator<(const node1& h)const{return d<h.d;}
}dis[maxn];
int vis[maxn],cnt;
void getdis(int x,int len,int b){
    vis[x]=1;
    dis[++cnt].d=len,dis[cnt].b=b;
    for(int i=head[x];i;i=edge[i].from){
        int y=edge[i].to,c=edge[i].c;
        if(vis[y]) continue;
        getdis(y,len+c,b);
    }
    vis[x]=0;
}
int s[maxn],mins,root;
void getroot(int S,int x){
    s[x]=1,vis[x]=1;
    int max_part=0;
    for(int i=head[x];i;i=edge[i].from){
        int y=edge[i].to;
        if(vis[y]) continue;
        getroot(S,y);
        s[x]+=s[y];
        max_part=max(max_part,s[y]);
    }
    max_part=max(max_part,S-s[x]);
    if(mins>max_part){
        mins=max_part;
        root=x;
    }
    vis[x]=0;
}
void solve(int S,int x){
    if(flag) return;
    mins=S;
    getroot(S,x);
    vis[root]=1;
    cnt=0;
    dis[++cnt].d=0,dis[cnt].b=root;
    for(int i=head[root];i;i=edge[i].from){
        int y=edge[i].to,c=edge[i].c;
        if(vis[y]) continue;
        getdis(y,c,y);
    }
    sort(dis+1,dis+cnt+1);
    /*
    D(root);E;
    for(int i=1;i<=cnt;i++){
        D(dis[i].b);D(dis[i].d);E;
    }
    */
    int l=1,r=cnt;
    while(l<=r){
        while(l<r&&dis[l].d+dis[r].d>X) r--;
        while(l<r&&dis[l].d+dis[r].d==X){
            if(dis[l].b!=dis[r].b) flag=true;
            r--;
        }
        l++;
    }
    int now=root;
    for(int i=head[now];i;i=edge[i].from){
        int y=edge[i].to,c=edge[i].c;
        if(vis[y]) continue;
        solve(s[y],y);
    }
}
int main() {
//  ios::sync_with_stdio(false);
    //freopen("Boatherds.in","r",stdin);
    while(scanf("%d",&n)&&n){
        init();
        for(int i=1;i<=n;i++){
            int to,c;
            while(scanf("%d",&to)&&to) scanf("%d",&c),add(i,to,c),add(to,i,c);
        }
        while(scanf("%d",&X)&&X){
            flag=false;
            memset(vis,0,sizeof(vis));
            solve(n,1);
            if(flag) printf("AYE\n");
            else printf("NAY\n");
        }
        printf(".\n");
    }
    return 0;
}
#endif
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,907评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,987评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,298评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,586评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,633评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,488评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,275评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,176评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,619评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,819评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,932评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,655评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,265评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,871评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,994评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,095评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,884评论 2 354

推荐阅读更多精彩内容

  • 接下来我将用三种不同的方法求解“平面最近点对”问题。问题描述:在一个平面上随机分布着 n 个点,现给定 n 个点的...
    小石头呢阅读 3,161评论 0 0
  • 参考两篇其他bolg总结的二叉树:https://github.com/xy7313/lintcode/blob/...
    暗黑破坏球嘿哈阅读 2,369评论 0 1
  • 分治策略 本文包括分治的基本概念二分查找快速排序归并排序找出伪币棋盘覆盖最大子数组 源码链接:https://gi...
    廖少少阅读 1,850评论 0 7
  • 贪心算法 贪心算法总是作出在当前看来最好的选择。也就是说贪心算法并不从整体最优考虑,它所作出的选择只是在某种意义上...
    fredal阅读 9,231评论 3 52
  • 多线程(一) 一、多线程的作用 1、线程的定义 · 线程是进程的基本执行单元,一个进程的所有任务都在线程中执行 ·...
    漆黑烈焰武士G阅读 280评论 0 0