AtCoder Beginner Contest 159

传送门

A - The Number of Even Pairs

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
    //freopen("in.txt","r",stdin);
    int n,m;
    scanf("%d%d",&n,&m);
    printf("%d\n",n*(n-1)/2+m*(m-1)/2);
    return 0;
}

A.cpp

B - String Palindrome

#include <bits/stdc++.h>
#define ll long long
using namespace std;
char s[105];
bool cal(int l,int r) {
    for(int i=l;i<=r;i++) {
        if(s[i]!=s[r+l-i]) return false;
    }
    return true;
}
int main() {
    //freopen("in.txt","r",stdin);
    scanf("%s",s+1);
    int n=strlen(s+1);
    bool f=true;
    if(!cal(1,n)) f=false;
    if(!cal(1,(n-1)/2)) f=false;
    if(!cal((n+3)/2,n)) f=false;
    printf("%s\n",f?"Yes":"No");
    return 0;
}

B.cpp

C - Maximum Volume

题意:已知长方体的长+宽+高等于L,求长方体的最大体积。

数据范围:$1 \leq L \leq 1000$

题解:长方体最大体积时,长=宽=高=L/3。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
    //freopen("in.txt","r",stdin);
    double L;
    scanf("%lf",&L),L/=3;
    printf("%.12f\n",L*L*L);
    return 0;
}

C.cpp

D - Banned K

题意:给N个球,每个球上有数字Ai,求去掉第i个球以后,选两个相同数字的球的方案数。

数据范围:$3 \leq N \leq 2 \times 10^{5},1 \leq A_{i} \leq N$

题解:先算出不去掉任何球的方案数,即为每个数字个数中取两个的组合数总和。

去掉第i个球,先减去该球上sum[Ai]对答案的贡献,然后加上C(sum[Ai]-1,2)。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+5;
int a[N],num[N];
int main() {
    //freopen("in.txt","r",stdin);
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
        num[a[i]]++;
    }
    ll ans=0;
    for(int i=1;i<=n;i++) {
        ans+=1LL*num[i]*(num[i]-1)/2;
    }
    for(int i=1;i<=n;i++) {
        ans-=1LL*num[a[i]]*(num[a[i]]-1)/2;
        num[a[i]]--;
        printf("%lld\n",ans+1LL*num[a[i]]*(num[a[i]]-1)/2);
        num[a[i]]++;
        ans+=1LL*num[a[i]]*(num[a[i]]-1)/2;
    }
    return 0;
}

D.cpp

E - Dividing Chocolate

题意:给一个H*W的矩阵,其中’0‘代表该点为黑,’1‘代表该点为白,可以进行横着或竖着切,求至少切几次满足每块的白块不超过K块。

数据范围:$1 \leq H \leq 10,1\leq W \leq 1000,1 \leq K \leq H \times W$

题解:由于H很小,可以枚举横着切的情况,然后就是枚举每列求最小次数。

#include <bits/stdc++.h>
using namespace std;
const int N=1e3+5;
char s[15][N];
int a[15][N];
int cal(int x1,int x2,int y) {
    int ans=0;
    for(int i=x1;i<=x2;i++) {
        ans+=(s[i][y]==‘1‘);
    }
    return ans;
}
int main() {
    //freopen("in.txt","r",stdin);
    int n,m,k;
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0;i<n;i++) {
        scanf("%s",s[i]);
    }
    int ans=1e9;
    for(int s=0;s<(1<<(n-1));s++) {
        vector<int> vec;
        vec.push_back(-1);
        for(int i=0;i<n-1;i++) {
            if((1<<i)&s) vec.push_back(i);
        }
        vec.push_back(n-1);
        int sz=vec.size();
        bool f=true;
        for(int j=0;j<m;j++) {
            for(int i=1;i<sz;i++) {
                a[i][j]=cal(vec[i-1]+1,vec[i],j);
                if(a[i][j]>k) f=false;
            }
        }
        if(!f) continue;
        int num=0,b[15]={0};
        for(int j=0;j<m;j++) {
            bool f=true;
            for(int i=1;i<sz;i++) {
                b[i]+=a[i][j];
                if(b[i]>k) f=false;
            }
            if(!f) {
                num++;
                for(int i=1;i<sz;i++) {
                    b[i]=a[i][j];
                }
            }
        }
        ans=min(ans,sz-2+num);
    }
    printf("%d\n",ans);
    return 0;
}

E.cpp

F - Knapsack for All Segments

题意:给一个长度为N的序列A,定义f(L,R)为AL~AR中子序列总和为S的方案数,求$\sum_{L=1}^{N} \sum_{R=L}^{N} f(L,R) $(对998244353取模)。

数据范围:$1 \leq N,S,A_{i} \leq 3000$

题解:通过朴素的01背包可以求出1~[1,N]的所有数,然后需要求2~[2,N]、3~[3,N]的总和,可以在第i个位置加一个起点,也就是f[0]++,这样就能合并在一起。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3e3+5;
const int MD=998244353;
int a[N],f[N];
void add(int &x,int y) {
    x+=y;
    if(x>=MD) x-=MD;
}
int main() {
    //freopen("in.txt","r",stdin);
    int n,s;
    scanf("%d%d",&n,&s);
    for(int i=0;i<n;i++) {
        scanf("%d",&a[i]);
    }
    int ans=0;
    for(int i=0;i<n;i++) {
        add(f[0],1);
        for(int j=s;j>=a[i];j--) {
            add(f[j],f[j-a[i]]);
        }
        add(ans,f[s]);
    }
    printf("%d\n",ans);
    return 0;
}

F.cpp

原文地址:https://www.cnblogs.com/zdragon1104/p/12551440.html

时间: 2024-08-30 16:35:49

AtCoder Beginner Contest 159的相关文章

AtCoder Beginner Contest 159 E - Dividing Chocolate【枚举】

题目链接 数据范围: \(1≤H≤10\) \(1≤W≤1000\) \(1≤K≤H×W\) 分析: 先观察数据,发现行数特别小,那么我们就可以枚举行的分法,对于每一种分法,求出列的划分数,取最小. 先用二维前缀和,预处理整个图. 复杂度:\(O(2^H*H*W)\) 代码: #include <bits/stdc++.h> using namespace std; const int N=1010; char s[15][N]; int num[15][N]; int main() { in

【ATcoder】AtCoder Beginner Contest 159题解

官方题解 落谷链接 ATC链接 A - The Number of Even Pairs 题意 给你两个数$n, m$代表有$n$个偶数,$m$个奇数.让你输出$n$个偶数$m$个奇数从中任选两个数(没有顺序)相加结果为偶数的个数. 题解 相加为偶数只有偶加偶和奇加奇两种情况,其实就是在$n$个数中取两个($C\binom{2}{n}$),在$m$个数中取两个($C\binom{2}{m}$). 时间复杂度$O(1)$ 1 #include <iostream> 2 using namespa

AtCoder Beginner Contest 103 D(贪心)

AtCoder Beginner Contest 103 D 题目大意:n个点,除第n个点外第i与第i+1个点有一条边,给定m个a[i],b[i],求最少去掉几条边能使所有a[i],b[i]不相连. 按右端点从小到大排序,如果当前选的去掉的边在区间内,那么符合条件,否则ans++,并贪心地把去掉的边指向右端点,因为前面的区间都满足条件了,所以要去掉的边要尽量向右移使其满足更多的区间. 1 #include <iostream> 2 #include <cstdio> 3 #incl

AtCoder Beginner Contest 136

AtCoder Beginner Contest 136 Contest Duration : 2019-08-04(Sun) 20:00 ~ 2019-08-04(Sun) 21:40 Website: AtCoder BC-136 后面几题都挺考思考角度D. C - Build Stairs 题目描述: 有n座山从左到右排列,给定每一座山的高度\(Hi\),现在你可以对每座山进行如下操作至多一次:将这座山的高度降低1. 问是否有可能通过对一些山进行如上操作,使得最后从左至右,山的高度呈不下降

AtCoder Beginner Contest 154 题解

人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We have A balls with the string S written on each of them and B balls with the string T written on each of them. From these balls, Takahashi chooses one

AtCoder Beginner Contest 155 简要题解

AtCoder Beginner Contest 155 A:签到失败,WA一次. int main() { int a, b, c; cin >> a >> b >> c; if(a == b && b == c) cout << "No"; else if(a == b || a == c || b == c) cout << "Yes"; else cout << &quo

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质)

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质) We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aiai and Vertex bibi. Consider painting each of these edges white or black. There ar

【ATcoder】AtCoder Beginner Contest 161 题解

题目链接:AtCoder Beginner Contest 161 原版题解链接:传送门 A - ABC Swap 这题太水,直接模拟即可. 1 #include <iostream> 2 using namespace std; 3 int main() { 4 int a, b, c; 5 cin >> a >> b >> c; 6 swap(a, b); 7 swap(a, c); 8 cout << a << " &

AtCoder Beginner Contest 115 题解

题目链接:https://abc115.contest.atcoder.jp/ A Christmas Eve Eve Eve 题目: Time limit : 2sec / Memory limit : 1024MB Score : 100 points Problem Statement In some other world, today is December D-th. Write a program that prints Christmas if D=25, Christmas E