CodeCraft-21 and Codeforces Round #711 (Div. 2)

@[toc](CodeCraft-21 and Codeforces Round #711 (Div. 2))

A [GCD Sum]

模拟吧。

/*
 *@author SunLakeWalk
 */
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <limits.h>
#include <sstream>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
//#pragma GCC optimize(2)
//#pragma GCC optimize(3, "Ofast", "inlin")

using namespace std;

#define ios ios::sync_with_stdio(false) , cin.tie(0)
#define x first
#define y second

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;

const int N = 100010, INF = 0x3f3f3f3f, mod = 1e9 + 7, base = 131;
const double eps = 1e-6, PI = acos(-1);

LL n;

void work()
{
    cin >> n;
    for (LL i = n; ; i ++ )
    {
        string s = to_string(i);
        
        int sum = 0;
        for (int j = 0; j < s.size(); j ++ )
          sum += s[j] - '0';

        if (__gcd(i, (LL)sum) > 1)
        {
            cout << i << endl;
            return;
        }
    }
}

int main()
{
  //ios;
  int T = 1;
  cin >> T;
  while (T -- )
  {
    work();
  }

  return 0;
}

B [Box Fitting]

贪心思想
法一:
凑2,从大到小来吧,反正都是2的质数级别,大的至少是小的2被,不用担心用一个大的占了两个小的空间

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 110;

int n, w;
int a[N];

void work()
{
      memset(a, 0, sizeof a);
      cin >> n >> w;

      int x;
      for (int i = 0; i < n; i ++ )
      {
          cin >> x;
          int t = __builtin_ctz(x);//返回二进制下末尾0的个数
          a[t] ++;
      }

      int cnt = 0;

      while (n)
      {
          int m = w;
          for (int i = 20; i >= 0; i -- )
          {
              while (m >= (1 << i) && a[i])
              {
                  a[i] --;
                  m -= (1 << i);
                  n --;
              }
          }
          cnt ++;
      }

      cout << cnt << endl;
}

int main()
{
    int T; cin >> T;
    while (T -- )
    {
        work();
    }

    return 0;
}

法二
直接单调队列,贪心来做嘛。

#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

int n, w;
int a[N];

void work()
{
    cin >> n >> w;
    for (int i = 0; i < n; i ++ ) cin >> a[i];

    sort(a, a + n, greater<int>());

    priority_queue<int> q;

    q.push(w - a[0]);
    for (int i = 1; i < n; i ++ )
    {
        int t = q.top();
        if (t >= a[i])
        {
            q.pop();
            q.push(t - a[i]);
        }
        else
        {
            q.push(w - a[i]);
        }
    }
    cout << q.size() << endl;

}

int main()
{
    int T; cin >> T;
    while (T -- )
    {
        work();
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值