码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round 640 (Div. 4)

时间:2020-05-10 10:22:14      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:break   using   out   元素   题解   struct   不能   lap   amp   

题目传送门

div4。。基本上都是构造题,水一波题解

A. Sum of Round Numbers

就把每一位拆出来

技术图片
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (register int i = a; i <= b; i++)
 
int n, a[10];
void solve()
{
    cin >> n;
    int id = 0;
    for (int i = 1; n > 0; i *= 10, n /= 10)
        if (i * (n % 10) != 0)
            a[++id] = i * (n % 10);
    cout << id << endl;
    rep(i, 1, id) cout << a[i] << " ";
    cout << endl;
}
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}
View Code

 

B. Same Parity Summands

问能不能将n分为k个奇数或k个偶数的和,分情况讨论吧

技术图片
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (register int i = a; i <= b; i++)
 
int n, k;
void solve()
{
    cin >> n >> k;
    if (!((n & 1) ^ (k & 1)) && n >= k)
    {
        puts("YES");
        rep(i, 1, k - 1) cout << "1 ";
        cout << n - k + 1 << endl;
    }
    else if (!(n & 1) && n >= 2 * k)
    {
        puts("YES");
        rep(i, 1, k - 1) cout << "2 ";
        cout << n - k * 2 + 2 << endl;
    }
    else
        puts("NO");
}
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}
View Code

 

C. K-th Not Divisible by n

找到第k个不被n整除的数,规律也很明显

技术图片
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (register int i = a; i <= b; i++)
 
ll n, k;
void solve()
{
    cin >> n >> k;
    cout << (k - 1) / (n - 1) + k << endl;
}
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}
View Code

 

D. Alice, Bob and Candies

alice和bob从两端一次吃糖,每次要吃的比对方多,简单的模拟就好了

技术图片
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (register int i = a; i <= b; i++)
 
ll n, a[1010];
void solve()
{
    cin >> n;
    rep(i, 1, n) cin >> a[i];
    int l = 1, r = n, sum1 = 0, sum2 = 0, ans1 = 0, ans2 = 0;
    int cnt = 0;
    while (l <= r)
    {
        sum1 = 0;
        cnt++;
        while (l <= r && sum1 <= sum2)
        {
            sum1 += a[l];
            ans1 += a[l];
            l++;
        }
        if (l <= r)
            cnt++;
        sum2 = 0;
        while (l <= r && sum2 <= sum1)
        {
            sum2 += a[r];
            ans2 += a[r];
            r--;
        }
    }
    cout << cnt << " " << ans1 << " " << ans2 << endl;
}
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}
View Code

 

E. Special Elements

E题我想得太难了,实际上就n2也能过。

序列中找到能用序列中连续元素和表示的数的个数

技术图片
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (register int i = a; i <= b; i++)
 
int n, a[8010];
int sum[8010];
int vis[8010];
int ans;
void solve()
{
    cin >> n;
    ans = 0;
    rep(i, 1, n) vis[i] = 0;
    rep(i, 1, n)
    {
        cin >> a[i];
        sum[i] = sum[i - 1] + a[i];
        vis[a[i]]++;
    }
    rep(i, 2, n) for (int j = i - 1; j; j--)
    {
        if (sum[i] - sum[j - 1] > n)
            break;
        if (vis[sum[i] - sum[j - 1]])
        {
            ans += vis[sum[i] - sum[j - 1]];
            vis[sum[i] - sum[j - 1]] = 0;
        }
    }
    cout << ans << endl;
}
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}
View Code

 

F. Binary String Reconstruction

构造01字符串,使字符串每相邻两项拿出来,(00),(01,10),(11)的个数分别为n0,n1,n2

我是按n2,n0,n1的顺序构造的,细节看代码

技术图片
int n0, n1, n2;
void solve()
{
    cin >> n0 >> n1 >> n2;
    if (n0 == 0 && n1 == 0)
        rep(i, 0, n2) putchar(1);
    else if (n1 == 0 && n2 == 0)
        rep(i, 0, n0) putchar(0);
    else
    {
        rep(i, 0, n2) putchar(1);
        rep(j, 0, n0) putchar(0);
        rep(i, 1, n1 - 1) putchar(i & 1 ? 1 : 0);
    }
    cout << endl;
}
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}
View Code

 

G. Special Permutation

构造一个数组,使相邻两项差值在2到4之间

我先开始考虑的是从按差为2变化,比如8:8,6,4,2,1,3,5,7。多列举几项发现只需要改一下中间1-4的顺序就可以了

技术图片
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (register int i = a; i <= b; i++)
 
int n;
void solve()
{
    cin >> n;
    if (n == 2 || n == 3)
    {
        puts("-1");
        return;
    }
    int tmp = n;
    while (n >= 5)
    {
        cout << n << " ";
        n -= 2;
    }
    if (n == 3)
        n = 6;
    if (n == 4)
        n = 5;
    cout << "3 1 4 2 ";
    while (n <= tmp)
    {
        cout << n << " ";
        n += 2;
    }
    cout << endl;
}
 
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}
View Code

 

Codeforces Round 640 (Div. 4)

标签:break   using   out   元素   题解   struct   不能   lap   amp   

原文地址:https://www.cnblogs.com/likunhong/p/12861883.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!