CCPC女生专场 2017

http://acm.hdu.edu.cn/showproblem.php?pid=6029

hdu 6029 

1.有两个选择

(1) Add edges between this vertex and all the previous vertices (i.e. from vertex 1 to i1

).(2) Not add any edge between this vertex and any of the previous vertices.


注意是每个之前的点都要连上,不仅仅是1到i一条边(刚开始没看清以为是1到i这条边,覆盖中间所有点)

In the mathematical discipline of graph theory, a matching in a graph is a set of edges without common vertices. A perfect matching is a matching that each vertice is covered by an edge in the set.

选择一组边使得.---. .----. 没有覆盖到同一个点 这就是完美匹配(注意这里是选择,其他的边覆盖了这个点,但是不满足完美匹配的,可不用考虑)

要这张整幅图都有完美匹配,就是每一个点都要考虑到,即每个点都有与它搭配的点 而且这个搭配点是只属于它一个的。

转自https://blog.csdn.net/baidu_35643793/article/details/71438678

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100005;
typedef long long ll;
const ll tomod=1e9+7;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,x;
        int flag2=1;//记录没有被匹配的点的个数
        scanf("%d",&n);

        for(int i=2;i<=n;i++)
        {
            scanf("%d",&x);
            if(x==1)
            {
                if(flag2>0)
                    flag2--;//匹配掉一个
                else
                    flag2++;//如果之前的点都匹配了,则他自己作为一个未被匹配的点
            }
            else if(x==2)
                flag2++;
        }

        if(flag2!=0||n%2!=0)
            printf("No\n");
        else
            printf("Yes\n");
    }
    return 0;
}


HDU 6025 Coprime Sequence(前缀后缀GCD问题)

预处理:对于第i个数 计算 在 i 之前的数的gcd 和 i 之后的数的gcd

hdu  6024 Building Shops (dp)

转自https://blog.csdn.net/STILLxjy/article/details/72514980

题意: 
有n个教室,现在想在这n个教室中建一些超市,问你最少费用为多少? 
费用分为两种: 
1:在第i个教室建超市,费用因为ci 
2:没有建超市的教室的费用为它和它左边最接近的超市的坐标之间的距离

分析: 
根据题目所给的时间,和题目的数据的大小,我们可以知道题目可以承受住时间复杂度为O(n^2)的算法。


并且每个教室只有两种方案,要么建超市,要么不建。这就很像是背包问题了,所以我们就想到了dp.


我们设dp[i][0]表示在教室i不建超市时前i个教室的费用和的最小值;dp[i][1]表示在教室i建超市时前i个教室的费用和的最小值


那么我们很快可以得出: dp[i][1] = min(dp[i-1][0],dp[i-1][1]) + ci


关于dp[i][0],由于可以承受住时间复杂度为O(n^2)的算法,那么我们就可以想到枚举离教室i最近的超市j的位置,然后取所有情况的最小值就可以了。


假设左边最近超市为j,那么教室j+1~教室i都不能建超市,所以教室j+1~教室i的费用分别为他们的位置到教室j之间的距离了。当前dp[i][0] = dp[j][1] +( 教室j+1~教室i的费用)


如果我们暴力求解,那么时间复杂度会变成O(n^3),会超时。但是我们会发现由于j是从大到小变化的,所以就可以用:t += (i - j) * (nodes[j+1].x - nodes[j].x);来记录教室j+1~教室i的费用和了。


关于 t += (i - j) * (nodes[j+1].x - nodes[j].x); 的解释: 
比如我们要算x3 - x1 , x2 - x1的sum,那么由于保证了x是升序排列的,所以sum = (x3 - x2) + 2 * (x2 - x1).


猜你喜欢

转载自blog.csdn.net/lirui7610/article/details/80228001