Manthan, Codefest 18 (rated, Div. 1 + Div. 2)(A,B,C, D)

比赛链接:http://codeforces.com/contest/1037
A:
题意:把n用尽可能少的划分,使得1-n的所有数都能表示出来(选的时候要么选一次,要么不选)比如6=1+2+3,这样就可以表示出1-6的所有数。
题解:这题有很多解法,但思想都是二进制。其实看n的最高位是第几位就行了,比如6(110)最高位是第3位。答案就是3。还有一种解法,本质一样,只不过可以给出具体划分方案,就是当n为偶数,减去一半,n为奇数,减去(n/2+1)。
代码:
第一种:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <bitset>
#include <cmath>

using namespace std;

int main(){
    int n, ans = 0;
    scanf("%d", &n);
    int tmp = 1;
    while(tmp <= n){
        tmp *= 2;
        ans++;
    }
    printf("%d\n", ans);
return 0;
} 

第二种:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <bitset>
#include <cmath>

using namespace std;

int main(){
    int n, ans = 0;
    scanf("%d", &n);
    while()
    while(n){
        int tmp = n/2;
        if(n%2 == 0)
            n -= tmp;
        else
            n -= (tmp+1);
        if(n>=0)
            ans++;
    }
    printf("%d\n", ans);
return 0;
} 

B:
题意: 给出一个序列a,和一个数s,每次操作可以把序列中的某个数数加1或减1。现在要让序列的中位数和s相等,问最少操作次数?
题解:显然,如果s小于中位数,那么让小于s的数改变是没有意义的,大于中位数同理。
所以只需要考虑中位数和s之间的数,让从s的位置到中间位置的所有数都变成s(变多了没意义)就是最小花费。
代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 200005;

int a[maxn];
int n, s, mid;
long long ans = 0;


int main(){
    scanf("%d %d", &n, &s);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    sort(a+1, a+n+1);
    int mid = a[n/2+1];
    if(mid == s){
        printf("0\n");
        return 0;
    }
    int x = lower_bound(a+1, a+n+1, s) - a;
    int y = upper_bound(a+1, a+n+1, s) - a;
    if(s > mid){
        for(int i = n/2+1; i < x; i++){
            ans += (s - a[i]);
        }
    }
    else if(s < mid){
        for(int i = y; i <= n/2+1; i++){
            ans += (a[i] - s);
        }
    }
    printf("%I64d\n", ans);
return 0;
}

C:
题意:
给出两个01串a, b,把a变成b。操作是每次替换(0->1,1->0)花费1,每次交换(把i处和j处交换)花费 |ij| | i − j | .问最小花费。
题解:
显然,如果两个与b不同的地方的下标相差小于2(相邻)并且一个1一个0,直接交换比替换两次花费更小,其他情况就直接替换就行了。
代码:

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int n;
char a[1000005], b[1000005];
long long ans = 0;

int main(){
    scanf("%d", &n);
    scanf("%s\n%s", a, b);
    //printf("%s\n%s\n", a, b);
    for(int i = 0; i < n; i++){
        if(a[i] != b[i]){
            if(i+1 < n && a[i+1] != a[i] && a[i+1] != b[i+1]){
                ans++;
                a[i] = b[i];
                a[i+1] = b[i+1];
            }
            else{ 
                ans++;
                a[i] = b[i];
            }
        }
    }
    printf("%I64d\n", ans);
return 0;
}

D:
题意:
给出一棵树,和一个序列,判断这个序列是不是合法的bfs,bfs以1号结点为入口。
题解:
dfs处理出父亲儿子关系,然后按照给出的序列编号,在判断前面结点的号如果小于后面结点,直接不行。
代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

const int maxn = 400005;

int n;
int head[maxn], tot = 0;
int deep[maxn], a[maxn];
int vis[maxn];
vector<int> vec[maxn];
int ans[maxn];

struct edge{
    int v;
    int next;
}e[maxn];

void addedge(int x, int y){
    e[++tot].v = y;
    e[tot].next = head[x];
    head[x] = tot;
}

void dfs(int x, int fa){
    for(int i = head[x]; i != -1; i = e[i].next){
        int y = e[i].v;
        if(y == fa) continue;
        vec[x].push_back(y);
        dfs(y, x);
    }
}

bool check(){
    ans[a[1]] = 1;
    for(int i = 1; i <= n; i++){
        if(ans[a[i]] == -1) return false;
        for(int j = 0; j < vec[a[i]].size(); j++){
            ans[vec[a[i]][j]] = i+1;
        }
    }
    for(int i = 2; i <= n; i++){
        if(ans[a[i]] < ans[a[i-1]]) return false;
    }
    return true;
}

int main(){
    int flag = 1;
    memset(vis, 0, sizeof(vis));
    memset(head, -1, sizeof(head));
    memset(ans, -1, sizeof(ans));
    scanf("%d", &n);
    for(int i = 1; i <= n-1; i++){
        int x, y;
        scanf("%d %d", &x, &y);
        addedge(x, y);
        addedge(y, x);
    }
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    if(a[1] != 1){
        printf("No\n");
        return 0;
    }
    dfs(a[1], -1);
    if(check()) printf("Yes\n");
    else printf("No\n");
return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值