1. 程式人生 > >Educational Codeforces Round 44 (Rated for Div. 2)

Educational Codeforces Round 44 (Rated for Div. 2)

out 嚴重 CA cstring chess ostream div while print

A. Chess Placing

題解:看著像優化問題,實際上因為奇數的位置或者偶數的位置一定會被填滿,所以暴力的對每個元素尋找它的適合位置。

感受:比賽的時候想多了,如果棋子的個數較少的話感覺會有點麻煩。。。

#pragma warning(disable:4996)
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long 
#define
lson root<<1 #define rson root<<1|1 #define mem(arr,in) memset(arr,in,sizeof(arr)) using namespace std; const int maxn = 105; int n; int a[maxn], use[maxn]; int ca_1() { mem(use, 0); for (int i = 1; i <= n; i++) if (a[i] % 2) use[a[i]] = 1; int cnt = 0; for (int i = 1
; i <= n; i++) if (a[i] % 2 == 0) { for (int j = 1; j <= 2*n; j += 2) if (!use[j]) { cnt += abs(j - a[i]); use[j] = 1; break; } } return cnt; }
int ca_2() { mem(use, 0); for (int i = 1; i <= n; i++) if (a[i] % 2 == 0) use[a[i]] = 1
; int cnt = 0; for (int i = 1; i <= n; i++) if (a[i] % 2) { for (int j = 2; j <= 2*n; j += 2) if (!use[j]) { cnt += abs(j - a[i]); use[j] = 1; break; } } return cnt; } int main() { while (cin >> n) { n = n / 2; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1); int ans = min(ca_1(), ca_2()); cout << ans << endl; } return 0; }

B. Switches and Lamps

題解:很直白的一道題,判斷刪除一個開關會影響那盞燈的狀態。

感受:這題出的很晚很無奈,,,,

#pragma warning(disable:4996)
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long 
#define lson root<<1
#define rson root<<1|1
#define mem(arr,in) memset(arr,in,sizeof(arr))
using namespace std;

const int maxn = 2005;

int n, m;
int mp[maxn][maxn], d[maxn];
char p[maxn][maxn];

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF){
        
        for (int i = 0; i < n; i++) scanf("%s", p[i]);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (p[i][j] == 1) mp[i][j] = 1;
                else mp[i][j] = 0;
            }
        }
        
            mem(d, 0);

            for (int j = 0; j < m; j++) {
                int cnt = 0;
                for (int i = 0; i < n; i++) {
                    if (mp[i][j] == 1) cnt++;
                }
                d[j] = cnt;
            }

            bool flag;
            for (int i = 0; i < n; i++) {
                flag = true;
                for (int j = 0; j < m; j++) {
                    if (mp[i][j] == 1) {
                        if (d[j] <= 1) flag = false;
                    }
                    else {
                        if (d[j] == 0) flag = false;
                    }
                }
                if (flag) break;
            }

            if (flag) printf("YES\n");
            else printf("NO\n");
        
    }
    return 0;
}

C. Liebig‘s Barrels

題解:我的想法是先排序得到最優的情況,判斷是否滿足,如果從位置pos就不滿足的話,就從pos往前找後面剩下的桶(need)的價值,同時在從前往後找每個桶的價值。具體看代碼。。。

感受:翻車最嚴重的一道題,,,同樣的貪心思想,代碼卻寫掛了,死活還找不到錯誤。。。註釋掉的是我的代碼,如果有朋友知道哪錯了,麻煩告知一聲,灰常感謝!

#pragma warning(disable:4996)
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long 
#define lson root<<1
#define rson root<<1|1
#define mem(arr,in) memset(arr,in,sizeof(arr))
using namespace std;

const int maxn = 100005;

int n, k, l, m;
int a[maxn];
bool use[maxn];

int main()
{
    while (scanf("%d %d %d", &n, &k, &l) != EOF) {
        m = n * k;
        for (int i = 1; i <= m; i++) scanf("%d", &a[i]);
        sort(a + 1, a + m + 1);

        if (a[n] - a[1] > l) printf("0\n");
        else {
            ll ans = 0;
            int pos = m;
            for (int i = n; i <= m; i++) if (a[i] - a[1] > l) { pos = i - 1; break; }

            mem(use, 0);
            int cnt = 0;
            for (int i = 1; i <= pos; i += k) {
                ans += a[i];
                cnt++;
                use[i] = 1;
            }
            if (cnt == n) printf("%I64d\n", ans);
            else {
                for (int i = pos; i >= 1; i--) if (!use[i]) {
                    ans += a[i];
                    cnt++;
                    if (cnt == n) break;
                }
                printf("%I64d\n", ans);
            }
        }
        /*
        int pos = -1;
        for (int i = 1; i <= m; i +=k) if (a[i] - a[1] > l) { pos = i; break; }

        ll ans = 0;

        if (pos == -1) {
            for (int i = 1; i <= m; i += k) ans += a[i];
            printf("%I64d\n", ans);
        }
        else {

            int need = n - (pos - 1) / k;
            int cnt = 0;
            int x = -1;

            for (int i = pos; i >= 1; i--) if (a[i] - a[1] <= l) {
                cnt++;
                ans += a[i];
                if (cnt == need) {
                    x = i;
                    break;
                }
            }
            for (int i = 1; i < x; i += k) { ans += a[i]; cnt++; }

            
            if (cnt != n) printf("0\n");
            else printf("%I64d\n", ans);
        }
        */
    }
    return 0;
}

Educational Codeforces Round 44 (Rated for Div. 2)