Codeforces #321(div2)

A. Kefa and First Steps

题意: 求连续最长不降子序列

分析: 水题

代码:

//
//  Created by TaoSama on 2015-09-23
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n, a[N];

int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	ios_base::sync_with_stdio(0);

	while(scanf("%d", &n) == 1){
		for(int i = 1; i <= n; ++i) scanf("%d", a + i);
		a[n + 1] = -INF;
		int ans = 1, cnt = 1;
		for(int i = 2; i <= n + 1; ++i){
			if(a[i] >= a[i - 1]) ++cnt;
			else{
				ans = max(ans, cnt);
				cnt = 1;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

B. Kefa and Company
题意: 给很多对身价和满意度的对 要求选择的对中任意2个对 身价差距不超过d 求最大满意度
分析: 按照身价排序 然后维护最大身价-最小身价不超过d的滑动窗口 更新最大满意度就是答案
代码:
//
//  Created by TaoSama on 2015-09-23
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n, m;
typedef pair<int, int> P;
P a[N];
int deq[N];

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    while(scanf("%d%d", &n, &m) == 2) {
        for(int i = 1; i <= n; ++i) scanf("%d%d", &a[i].first, &a[i].second);
        sort(a + 1, a + 1 + n);
        int l = 1;
        long long sum = 0, ans = 0;
        for(int i = 1; i <= n; ++i) {
            sum += a[i].second;
            deq[i] = i;
            while(i > l
                    && a[deq[i]].first - a[deq[l]].first >= m) sum -= a[deq[l++]].second;
//          printf("%d\n", sum);
            ans = max(ans, sum);
        }
        printf("%I64d\n", ans);
    }
    return 0;
}
C. Kefa and Park
题意: 求树上到叶子节点的路径中 连续猫个数不超过m 的不同叶子节点
分析: 搜一下树就好了
代码:
//
//  Created by TaoSama on 2015-09-23
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n, k, ans;
bool cat[N];
vector<int> G[N];

void dfs(int u, int f, int c){
	if(c > k) return;
	int cnt = 0;
	for(auto v : G[u]){
		if(v == f) continue;
		++cnt;
		dfs(v, u, cat[v] ? c + 1 : 0);
	}
	if(cnt == 0) ++ans;
}

int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	ios_base::sync_with_stdio(0);

	while(scanf("%d%d", &n, &k) == 2){
		for(int i = 1; i <= n; ++i) scanf("%d", cat + i);
		for(int i = 1; i <= n; ++i) G[i].clear();
		for(int i = 1; i < n; ++i){
			int u, v; scanf("%d%d", &u, &v);
			G[u].push_back(v);
			G[v].push_back(u);
		}
		ans = 0;
		dfs(1, -1, cat[1]);
		printf("%d\n", ans);
	}
	return 0;
}
D. Kefa and Dishes
题意: n个饭菜 吃m个 如果按照k种前后排列的话有额外奖励 求最大满意度
分析: 状压dp 需要知道当前谁吃了 当前谁吃了 所以dp[i][j]:= 当前状态为i 且吃了j的最大满意度
          然后dp就好了
代码:
//
//  Created by TaoSama on 2015-09-23
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n, m, k;
long long dp[1 << 18][20];
int a[20], c[20][20];

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    while(scanf("%d%d%d", &n, &m, &k) == 3) {
        for(int i = 0; i < n; ++i) scanf("%d", a + i);
        memset(c, 0, sizeof c);
        for(int i = 1; i <= k; ++i) {
            int u, v, w; scanf("%d%d%d", &u, &v, &w);
            --u; --v;
            c[u][v] = w;
        }
        memset(dp, -1, sizeof dp);
        for(int i = 0; i < n; ++i) dp[1 << i][i] = a[i];
        long long ans = 0;
        for(int i = 1; i < 1 << n; ++i) {
            for(int j = 0; j < n; ++j) {
                if(dp[i][j] == -1) continue;
                for(int k = 0; k < n; ++k) {
                    if(i >> k & 1) continue;
                    dp[i | (1 << k)][k] = max(dp[i | (1 << k)][k], dp[i][j] + c[j][k] + a[k]);
                }
                if(__builtin_popcount(i) == m) ans = max(ans, dp[i][j]);
            }
        }
        printf("%I64d\n", ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值