zoukankan      html  css  js  c++  java
  • CodeForces 297C Splitting the Uniqueness (脑补构造题)

    题意

    Split a unique array into two almost unique arrays. unique arrays指数组各个数均不相同,almost unique arrays指可以删掉数后再判断。

    思路

    略神的数学构造题。。。 官方题解: An equivalent definition for almost unique, is arrays with at least ⌊ 2n / 3⌋ different elements. The idea is to split s into three parts. In the first part, we give uniqueness to a. In the second part, we give uniqueness to b. In the third part, we give uniqueness to both. Lets assume s is sorted. Since s is an unique array, si ≥ i for all i (0-based). The image below will give some intuition on how to split it. ais red, b is blue, the length of the bar represent the magnitude of the number. In the first and second part, we do not care about the array that we are not giving uniqueness to.  We will make an example with n = 30. i = 0... 9:  assign ai = i (do not care values of b) i = 10... 19:  assign bi = i (do not care values of a) i = 20... 29:  assign bi = 29 - ia takes the remains. From i = 20, a will have strictly increasing values starting from at least 11.  

    代码

      [cpp] #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <string> #include <cstring> #include <vector> #include <set> #include <stack> #include <queue> #define MID(x,y) ((x+y)/2) #define MEM(a,b) memset(a,b,sizeof(a)) #define REP(i, begin, end) for (int i = begin; i <= end; i ++) using namespace std; typedef long long LL; struct num{ int value; int id; num(){} num(int _id, int _value){id = _id; value = _value;} }; bool cmp(num n1, num n2){ return n1.value < n2.value; } typedef vector <num> VI; VI v; int a[100005], b[100005]; int main(){ //freopen("test.in", "r", stdin); //freopen("test.out", "w", stdout); int n; scanf("%d", &n); REP(i, 0, n-1){ int tmp; scanf("%d", &tmp); v.push_back(num(i, tmp)); } sort(v.begin(), v.end(), cmp); int d = (int)ceil((double)n/3); for (int i = 0; i < min(n, d); i ++){ a[v[i].id] = i; b[v[i].id] = v[i].value - a[v[i].id]; } for (int i = d; i < min(n, d*2); i ++){ b[v[i].id] = i; a[v[i].id] = v[i].value - b[v[i].id]; } for (int i = 2*d; i < n; i ++){ b[v[i].id] = n - 1 - i; a[v[i].id] = v[i].value - b[v[i].id]; } puts("YES"); for (int i = 0; i < n-1; i ++) printf("%d ", a[i]); printf("%d ", a[n-1]); for (int i = 0; i < n-1; i ++) printf("%d ", b[i]); printf("%d ", b[n-1]); return 0; } [/cpp]
  • 相关阅读:
    android 11中报new Handler()已过时
    My WelcomeApplet
    设计模式(五):抽象工厂方法(多个工厂方法的组合)
    [Redis知识体系] 一文全面总结Redis知识体系
    RocketMq的事务消息发送方法,消息零丢失的实现方式,代码流程讲解,干货分享
    充血枚举的用法
    Rocketmq的启动jvm配置解析-runbroker.sh
    springboot+mybatis整合,基本框架
    rabbitmq单机入门安装教程(ubuntu环境 亲测有效)
    常用表单验证插件
  • 原文地址:https://www.cnblogs.com/AbandonZHANG/p/4114108.html
Copyright © 2011-2022 走看看