1. 程式人生 > >2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1)

2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1)

2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1)

思路:

Exam

思路:水題

程式碼:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int k;
    scanf("%d",&k);
    char s1[1010],s2[1010];
    scanf("%s%s",s1,s2);
    int same=0;
    int n=strlen(s1);
    
for(int i=0;i<n;i++){ same+=s1[i]==s2[i]; } cout<<min(same,k)+min(n-same,n-k)<<endl; return 0; }
View Code

 

Coprime Integers

思路:容斥

程式碼:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using
namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair<LL, LL> #define pli pair<LL, int> #define
pii pair<int, int> #define piii pair<pii, int> #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 1e7 + 5; LL g[N]; LL solve(int a, int b) { if(a > b) swap(a, b); if(a == 0) return 0; for (int i = a; i >= 1; i--) { g[i] = 1LL * (a/i) * (b/i); for (int j = i+i; j <= a; j += i) g[i] = g[i] - g[j]; } return g[1]; } int main() { int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d); printf("%lld\n", solve(b, d) - solve(b, c-1) - solve(a-1, d) + solve(a-1, c-1)); return 0; }
View Code

 

Contest Setting

思路:dp

dp[i][j]表示前i種選j個的方案數

程式碼:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head


const int MOD = 998244353;
const int N = 1e3 + 5;
LL dp[N][N];
int cnt[N], a[N], tot = 0;
map<int, int> mp;
int main() {
    int n, k;
    scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        if(mp.find(a[i]) == mp.end()) mp[a[i]] = ++tot, cnt[tot] = 1;
        else cnt[mp[a[i]]] ++;
    }
    dp[0][0] = 1;
    for (int i = 1; i <= tot; i++) {
        for (int j = 0; j <= k; j++) dp[i][j] = dp[i-1][j];
        for (int j = 1; j <= k; j++) dp[i][j] = (dp[i][j] + dp[i-1][j-1]*cnt[i]) % MOD;
    }
    printf("%lld\n", dp[tot][k]);
    return 0;
}
View Code

 

Count The Bits

思路:dp

dp[i][j][0]表示前i位構成的數中對k取模為j的數的個數

dp[i][j][1]表示前i位構成的數中對k取模為j的數中二進位制中1的個數

程式碼:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head

const int N = 150, M = 1e3 + 5;
const int MOD = 1e9 + 9;
LL dp[N][M][2];
int main() {
    int k, b;
    scanf("%d %d", &k, &b);
    dp[0][0][0] = 1;
    dp[0][0][1] = 0;
    for (int i = 1; i <= b; i++) {
        for (int j = 0; j < k; j++) {
            (dp[i][(j*2)%k][0] += dp[i-1][j][0]) %= MOD;
            (dp[i][(j*2+1)%k][0] += dp[i-1][j][0]) %= MOD;

            (dp[i][(j*2)%k][1] += dp[i-1][j][1]) %= MOD;
            (dp[i][(j*2+1)%k][1] += dp[i-1][j][1] + dp[i-1][j][0]) %= MOD;
        }
    }
    printf("%lld\n", dp[b][0][1]);
    return 0;
}
View Code

 

Cops And Roobers

Rectangles

Goat on a Rope

思路:求點到矩形的最近距離

程式碼:

#include<bits/stdc++.h>
using namespace std;
double cal(double x1,double y1,double x2,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main()
{
    double x,y,x1,x2,y1,y2,ans=1e9;
    scanf("%lf%lf%lf%lf%lf%lf",&x,&y,&x1,&y1,&x2,&y2);
    if(x>=min(x1,x2)&&x<=max(x2,x1)) ans=min(abs(y1-y),abs(y2-y));
    else if(y>=min(y1,y2)&&y<=max(y1,y2)) ans=min(abs(x-x1),abs(x-x2));
    else ans=min(cal(x,y,x1,y1),min(cal(x,y,x2,y2),min(cal(x,y,x1,y2),cal(x,y,x2,y1))));
    printf("%.3f\n",ans);
}
View Code

 

Repeating Goldbachs

思路:暴力

程式碼:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head

const int N = 1e6 + 5;
int p[N], tot = 0;
bool not_p[N];
void seive() {
    for (int i = 2; i < N; i++) {
        if(!not_p[i]) {
            p[++tot] = i;
        }
        for (int j = 1; j <= tot && p[j]*i < N; j++) {
            not_p[p[j]*i] = true;
            if(i % p[j] == 0) break;
        }
    }
}
int main() {
    int x;
    scanf("%d", &x);
    seive();
    int ans = 0;
    while(x >= 4) {
        for(int i = 1; i <= tot && p[i] <= x; i++) {
            if(!not_p[x-p[i]]) {
                x = x - p[i] - p[i];
                ans++;
                break;
            }
        }
    }
    printf("%d\n", ans);
    return 0;
}
View Code

 

Inversions

Time Limits

思路:水題

程式碼:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head

const int N = 105;
int t[N];
int n, s;
int main() {
    scanf("%d %d", &n, &s);
    for (int i = 1; i <= n; i++) scanf("%d", &t[i]);
    sort(t+1, t+1+n);
    printf("%d\n", (t[n]*s + 999) / 1000);
    return 0;
}
View Code

 

Knockout

Liars

思路:暴力

程式碼:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head

const int N = 1e3 + 5;
pii a[N];
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d %d", &a[i].fi, &a[i].se);
    int ans = -1;
    for (int i = 0; i <= n; i++) {
        int cnt = 0;
        for (int j = 1; j <= n; j++) {
            if(a[j].fi <= i && i <= a[j].se) cnt++;
        }
        if(cnt == i) ans = max(ans, i);
    }
    printf("%d\n", ans);
    return 0;
}
View Code

 

Mobilization