AcWing 第 1 场周赛补题记录(A~C)

比赛链接:Here

AcWing 3577. 选择数字

排序,然后选取两个数组的最大值

void solve() {
    int n; cin >> n;
    vector<int>a(n);
    for (int &x : a)cin >> x;
    int m; cin >> m;
    vector<int>b(m);
    for (int &x : b)cin >> x;
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    cout << a[n - 1]  << " " << b[m - 1] << "\n";
}

AcWing 3578. 最大中位数

因为我们要让中位数最大,所以对前面 n>>1 的数可以不用管,然后我们每次让最小的数+到第二小看看可不可以加到如果不可以就把剩余的k平均分给最小数,如果可以加就加到第二小然后以此类推

using ll = long long;
const int N = 2e5 + 10, mod = 1e9 + 7;
ll a[N], sum[N];
int main() {
    int n;
    ll k;
    cin >> n >> k;
    for (int i = 1; i <= n; i ++ ) {
        cin >> a[i];
    }
    sort(a + 1, a + 1 + n);
    int pos = n / 2 + 1;//中位数所在位置
    for (int i = 1; i <= n; i ++ ) {
        sum[i] = sum[i - 1] + a[i];
    }
    ll res = a[pos];
    for (int i = pos; i <= n; i ++ ) {
        ll more = k + (sum[i] - sum[pos - 1]);
        ll num = more / (i - pos + 1);
        if (num < a[i]) break;
        if (i < n) num = min(num, a[i + 1]);
        res = max(res, num);
    }
    cout << res;
}

AcWing 3579. 数字移动

C 题 眨眼一看,我去!好难!这怎么写!其实仔细读题之后,会发现,此题就是找闭环过程,剩最后10分钟,糊出来了。
解题思路:因为在闭环上的点操作数都是一样的,1->2;2->3;3->4;4->1 他们的操作数都是一样的都是4,所以直接模拟就好啦。

const int N = 2e5 + 10, mod = 1e9 + 7; // 注意数据范围是 2e5
int a[N], q[N], res[N], n, t;
bool f[N];
void solve() {
    cin >> n;
    for (int i = 1; i <= n; ++i )cin >> a[i];;
    for (int i = 1; i <= n; ++i )f[i] = false;
    for (int i = 1; i <= n; ++i) {
        if (f[i]) continue;
        int j = i, top = 1, cnt = 0;
        do {f[j] = true, j = a[j]; q[top++] = j, cnt++;} while (!f[j]);
        for (int k = 1; k < top; ++k)res[q[k]] = cnt;
    }
    for (int i = 1; i <= n; ++i)cout << res[i] << " ";
    cout << "\n";
}
posted @ 2021-07-04 19:57  RioTian  阅读(59)  评论(0编辑  收藏  举报