Codeforces Round #277.5 (Div. 2) 解题报告

还是只会4道。。sad。。。

A:SwapSort

用一个数组存储排好序之后。然后从头开始依次将需要交换的与本来应该在这个位置的交换,最多交换n-1次。

代码如下;

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
int a[4000], b[4000], c[4000], d[4000];
int main()
{
    int i, j, n, pos, cnt;
    while(scanf("%d",&n)!=EOF)
    {
        cnt=0;
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            b[i]=a[i];
        }
        sort(b,b+n);
        for(i=0;i<n;i++)
        {
            if(a[i]!=b[i])
            {
                for(j=i+1;j<n;j++)
                {
                    if(a[j]==b[i])
                    {
                        pos=j;
                        break;
                    }
                }
                swap(a[i],a[pos]);
                c[cnt]=i;d[cnt++]=pos;
            }
        }
        printf("%d\n",cnt);
        for(i=0;i<cnt;i++)
        {
            printf("%d %d\n",c[i],d[i]);
        }
    }
    return 0;
}

B:BerSU Ball

二分图最大匹配裸题。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
int link[200], vis[200], n, m, a[200], b[200];
int head[200], cnt;
struct node
{
    int u, v, next;
}edge[100000];
void add(int u, int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int dfs(int u)
{
    int i;
    for(i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!vis[v])
        {
            vis[v]=1;
            if(link[v]==-1||dfs(link[v]))
            {
                link[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
void hungary()
{
    int i, ans=0;
    memset(link,-1,sizeof(link));
    for(i=0;i<n;i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i))
            ans++;
    }
    printf("%d\n",ans);
}
int main()
{
    int i, j;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        scanf("%d",&m);
        for(i=0;i<m;i++)
        {
            scanf("%d",&b[i]);
        }
        memset(head,-1,sizeof(head));
        cnt=0;
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                if(abs(a[i]-b[j])<=1)
                {
                    add(i,j);
                }
            }
        }
        hungary();
    }
    return 0;
}

C:Given Length and Sum of Digits...

贪心水题。

按照顺序依次填充。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
#define LL __int64
const int INF=0x3f3f3f3f;
int a[1000], b[1000];
int main()
{
    int m, s, i, j, x;
    while(scanf("%d%d",&m,&s)!=EOF)
    {
        if(m*9<s)
        {
            printf("-1 -1\n");
            continue ;
        }
        if(s==0)
        {
            if(m==1)
                printf("0 0\n");
            else
                printf("-1 -1\n");
            continue ;
        }
        x=s-1;
        for(i=m; i>=2; i--)
        {
            if(x>=9)
            {
                a[i]=9;
                x-=9;
            }
            else if(x>=0&&x<9)
            {
                a[i]=x;
                x=0;
            }
        }
        a[1]=x+1;
        x=s;
        for(i=1;i<=m;i++)
        {
            if(x>=9)
            {
                b[i]=9;
                x-=9;
            }
            else if(x>=0&&x<9)
            {
                b[i]=x;
                x=0;
            }
        }
        for(i=1;i<=m;i++)
        {
            printf("%d",a[i]);
        }
        printf(" ");
        for(i=1;i<=m;i++)
        {
            printf("%d",b[i]);
        }
        puts("");
    }
    return 0;
}

D:Unbearable Controversy of Being

枚举起点,分别进行BFS找到距离为2的点,然后记录到达这个点的次数,若次数大于2,说明存在,根据组合数来求解。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
#define LL __int64
LL vis[4000], ans;
int head[4000], cnt, n;
struct node
{
    int u, v, next;
}edge[50000];
void add(int u,int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void bfs(int x)
{
    int i;
    queue<int>q;
    for(i=head[x];i!=-1;i=edge[i].next)
    {
        q.push(edge[i].v);
    }
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(v!=x)
                vis[v]++;
        }
    }
    for(i=1;i<=n;i++)
    {
        if(vis[i]>=2)
            ans+=vis[i]*(vis[i]-1)/2;
    }
}
int main()
{
    int m, i, j, u, v;
    scanf("%d%d",&n,&m);
    ans=0;
    memset(head,-1,sizeof(head));
    cnt=0;
    while(m--)
    {
        scanf("%d%d",&u,&v);
        add(u,v);
    }
    for(i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        bfs(i);
    }
    printf("%I64d\n",ans);
    return 0;
}
时间: 2024-08-03 04:55:24

Codeforces Round #277.5 (Div. 2) 解题报告的相关文章

Codeforces Round #277.5 (Div. 2) JAVA版题解

Codeforces Round #277.5 (Div. 2) A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output In this problem your goal is to sort an array consisting of n integers in at most n swaps. For t

Codeforces Round #277.5 (Div. 2) b

/**  * @brief Codeforces Round #277.5 (Div. 2) b  * @file b.c  * @author 面码  * @created 2014/11/18 17:22  * @edited  2014/11/18 17:22  * @type greedy  *  */ #include <stdio.h> #define MAXN 110 #define max(a, b)  ((a) > (b) ? (a) : (b)) #define mi

Codeforces Round #277.5 (Div. 2)C. Given Length and Sum of Digits...(贪心)

传送门 Description You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the d

Codeforces Round #277.5 (Div. 2)D Unbearable Controversy of Being (暴力)

这道题我临场想到了枚举菱形的起点和终点,然后每次枚举起点指向的点,每个指向的点再枚举它指向的点看有没有能到终点的,有一条就把起点到终点的路径个数加1,最后ans+=C(路径总数,2).每两个点都这么弄.但是我考虑时间复杂度n2前面的系数过大会超时,再想别的方法也没想出来.. 其实思路就是这样的,只不过时间上可以优化一下,就是用常用的两种做法不变而优化时间复杂度的方法:1.以空间换时间2.分步降维 #include <cstdio> #include <vector> using n

CodeForce---Educational Codeforces Round 3 The best Gift 解题报告

对于这题笔者认为可以用数学排列来算,但是由于笔者很懒所以抄了一段大神的代码来交个大家了, 这位大神的基本想法就是通过记录各类书的数量,再暴力破解: 下面贴出这位大神的代码吧: 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 6 int h[15]; 7 int main() 8 { 9 int n,m; 10 scanf("%d%d&q

Codeforces Round #277.5 (Div. 2)——C贪心—— Given Length and Sum of Digits

You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base with

Codeforces Round #277.5 (Div. 2)

A 题意:给定n个数,最多交换n次获得一个不减的序列,求一个合法的交换序列. 题解:每次找从i开始的最小值换到第i个位置就可以了. #include <cstdio> #include <algorithm> #include <cstring> #include <iostream> #include <cmath> using namespace std; int a[5000],n,max1,maxx; int main() { scanf

Codeforces Round #277.5 (Div. 2)-D

题意:求该死的菱形数目.直接枚举两端的点.平均意义每一个点连接20条边,用邻接表暴力计算中间节点数目,那么中间节点任选两个与两端可组成的菱形数目有r*(r-1)/2. 代码: #include<iostream> #include<cstdio> #include<cmath> #include<map> #include<cstring> #include<algorithm> #define rep(i,a,b) for(int

Codeforces Round #277.5 (Div. 2) C Given Length and Sum of Digits...

大大的传送门:就是这里 这一道卡在了特判的  1   0 本来输出 0  0 输出  -1    -1了,就错了,, 这道题就是一个恨好的贪心,往大了贪 下面是代码 #include <cstdio> #include <cstring> #include <vector> using namespace std; int n,m; int ans[110]; int main(){ scanf("%d%d",&n,&m); mems