分治思想《算法竞赛入门经典》

三步法
1,划分问题
2,递归求解
3,合并问题

首相分治法可以用了优化最大连续和
优化后时间复杂度为o(nlogn)

第一步:利用m=x+(y-x)/2,设置划分点。注意划分格式为[x,m),[m,y),这样不仅符合左边去到右边取不到的规范,而且不用m-1,m+1这些容易出错的步骤。
第二步:划分后近似相当于T(n)=2(n/2),然后对两边递归求解,最小不可分元素就是当一段连续和中只有一个元素,所以当right-left=1时返回A[left],因为left能取到,right取不到。当不是最小元素时,就左右两边延长到底,和该段每个元素单个值比较,留下最大的,返回给上一层。

棋盘覆盖问题
递归至边长只有2的正方形,此时必有一块已经被填,只需要将剩下三块填完即可,大于两块将棋盘一分为四,有个象限含有初始的黑块,将中心四块中其它的三块涂黑,然后继续递归。

#include
#include
#include<string>
#include
#include<vector>
#include
#include
#include
#include
using namespace std;

const int INF = 0x3f3f3f3f;
int board[105][105];
int team = 1;
int pos(int size, int a, int b, int x, int y)
{
    if (x < a + size / 2 && y < b + size / 2)
        return 1;
    else if (x >= a + size / 2 && y < b + size / 2)
        return 2;
    else if (x >= a + size / 2 && y >= b + size / 2)
        return 3;
    else
        return 4;
}
void Divide(int size, int a, int b)
{
    int x, y;
    if (size == 2)
    {
        for (int i = a; i size; i++)
        {
            for (int j = b; j size; j++)
            {
                if (board[i][j] == -1)
                {
                    board[i][j] = team;
                }
            }
        }
        team++;
    }
    else
    {
        for (int i = a; i size; i++)
        {
            for (int j = b; j size; j++)
            {
                if (board[i][j] != -1)
                {
                    x = i;
                    y = j;
                }
            }
        }
        int loc = pos(size, a, b, x, y);
        for (int i = a + size / 2 - 1; i <= a + size / 2; i++)
        {
            for (int j = b + size / 2-1; j <= b + size / 2; j++)
            {
                if (loc != pos(size, a, b, i, j))
                    board[i][j] = team;
            }
        }
        team++;
        Divide(size / 2, a, b);
        Divide(size / 2, a + size / 2, b + size / 2);
        Divide(size / 2, a + size / 2, b);
        Divide(size / 2, a, b + size / 2);
    }
}

int main()
{
    int k;
    while (cin >> k, k)
    {
        int x, y;
        cin >> x >> y;
        memset(board, -1, sizeof(board));
        board[x][y] = 0;
        int size = 1;
        for (int i = 1; i <= k; i++)
        {
            size *= 2;
        }
        Divide(size, 1, 1);
        for (int i = 1; i <= size; i++)
        {
            for (int j = 1; j <= size; j++)
            {
                printf("%-4d", board[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

循环日程表问题
思路通过temp控制每次方格的大小,来实现分治,
有规律可循,通过规律先对左下赋值
然后将左下复制到右上,
再将左上复制到右下

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int INF = 0x3f3f3f3f;
int board[105][105];
void solve(int k)
{
    board[1][1] = 1;
    board[2][1] = 2;
    board[1][2] = 2;
    board[2][2] = 1;
    int  temp = 1;
    for (int s = 1; s <= k; s++)
    {
        temp *= 2;
        for (int i = 1; i <= temp;i++)
        {
            for (int j = 1; j <=temp;j++)
            {
                board[i + temp][j] = board[i][j] + temp;
            }
        }
        for (int i = 1; i <= temp; i++)
        {
            for (int j = 1; j <=temp; j++)
            {
                board[i][j + temp] =board[i + temp][j] ;
            }
        }
        for (int i = 1; i <= temp; i++)
        {
            for (int j = 1; j <=temp; j++)
            {
                board[i+temp][j + temp] =board[i][j] ;
            }
        }   
    }
}

int main()
{
    int k;
    while (cin >> k, k)
    {
        solve(k);
        int size;
        size = 1;
        for (int i = 1; i <= k; i++)
        {
            size *= 2;
        }
        for (int i = 1; i <= size; i++)
        {
            for (int j = 1; j <= size; j++)
            {
                printf("%-4d", board[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

巨人与鬼
题目大意是n个黑点,n个白点,一个黑点连一个白点(直线),如何使任意两条直线都没有相交。

#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int INF = 0x3f3f3f3f;
int n;
struct node
{
    int x, y;
    int type;
    int ID;
    double angle;
}points[1005];

bool cmp1(node a, node b)
{
    if (a.y == b.y)
        return a.x < b.y;
    return a.y < b.y;
}
bool cmp2(node a, node b)
{
    return a.angle < b.angle;
}
void swap(int temp1, int temp2)
{
    int temp;
    temp = temp1;
    temp1 = temp2;
    temp2 = temp;

}
int ans[10005];
void Swap(node a, node b)
{
    swap(a.x, b.x);
    swap(a.y, b.y);
    swap(a.ID, b.ID);
    swap(a.type, b.type);
    double temp;
    temp = a.angle;
    a.angle = b.angle;
    b.angle = temp;
}

void solve(int left, int right)
{
    if (right - left == 1)
    {
        ans[points[left].ID] = points[right].ID;
        return;
    }
    if (left >= right)
    {
        return;
    }
    sort(points + left, points + right + 1, cmp1);
    for (int i = left+1; i <= right; i++)
    {
        points[i].angle = atan2(points[i].y - points[left].y, points[i].x - points[left].x);
    }
    sort(points + left+1, points + right + 1, cmp2);
    int sum = 0;
    for (int i = left; i <= right; i++)
    {
        sum += points[i].type;
        if (sum == 0)
        {
            ans[points[left].ID] = points[right].ID;
            solve(left + 1, i - 1);
            solve(i + 1, right);
        }       
    }
}

int main()
{

    while (cin >> n, n)
    {
        for (int i = 1; i <= n*2; i++)
        {
            cin >> points[i].type;
            cin >> points[i].x;
            cin >> points[i].y;
            points[i].ID = i;
        }
        solve(1, 2*n);
        for (int i = 1; i <=n; i++)
            printf("%d\n", ans[i]);
    }
    return 0;
}

你可能感兴趣的:(算法入门竞赛经典)