A. Meximization

第i位优先放等于i-1的,没有的话就后面随便填了。

view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} }; ll a[maxn];
int vis[maxn]; int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
rep(i,1,n) a[i] = read(), vis[i] = 0;
sort(a+1,a+1+n);
int idx = 0;
rep(i,1,n)
{
int flag = 0;
rep(j,1,n) if(!vis[j]&&a[j]==i-1)
{
flag = 1;
cout<<a[j]<<' ';
vis[j] = 1;
break;
} if(!flag) break;
}
rep(i,1,n) if(!vis[i]) cout<<a[i]<<' ';
cout<<endl;
}
return 0;
}

B. M-arrays

将a[i]存成模m后余数的形式,那么以每个a[i]与m-a[i]匹配的方式最优。特判一下a[i]为0和a[i]==m-a[i]的情况即可。

view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} }; ll a[maxn]; int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read(), m = read();
rep(i,1,n) a[i] = read(), a[i] %= m;
map<ll,ll> Map;
map<ll,ll> vis;
rep(i,1,n) Map[a[i]]++;
ll ans = 0;
rep(i,1,n)
{
if(vis[a[i]]) continue;
if(a[i]==0) ans += 1;
else
{
if(a[i]==m-a[i])
{
ans += 1;
}
else
{
ll mi = min(Map[a[i]], Map[m-a[i]]);
ll ma = max(Map[a[i]], Map[m-a[i]]);
ans += 1;
ans += max(ma-mi-1,0);
} }
vis[a[i]] = 1;
vis[m-a[i]] = 1;
} cout<<ans<<endl;
}
return 0;
}

k-LCM (hard version)

两个版本解法大同小异。先看C1,k=3时策略如下:

如果n为奇数,那么用a = (n-1)/2, b = a, c = 1即可。

若n为偶数,

且n/2是奇数,那么a = n/2, b = a, c = 2

否则直接 a = n/2, b = n/4, c = b即可。

那么C2的话,就先输出k-3个1,剩下的3个k来处理n-(k-3)又可以直接套C1方法了。

view code
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define endl '\n'
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} }; int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read(), k = read();
rep(i,4,k) cout<<1<<' ';
n -= (k-3);
ll a = 0, b = 0, c = 0;
if(n&1)
{
a = n/2;
b = a;
c = 1;
}
else
{
if((n/2)&1)
{
a = n/2 - 1;
b = a;
c = 2;
}
else
{
a = n/2;
c = a/2;
b = c;
}
}
cout<<a<<' '<<b<<' '<<c<<endl;
}
return 0;
}

Codeforces Round #708 (Div. 2) ABC1C2题解的更多相关文章

  1. # Codeforces Round #529(Div.3)个人题解

    Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...

  2. Codeforces Round #557 (Div. 1) 简要题解

    Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...

  3. Codeforces Round #540 (Div. 3) 部分题解

    Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...

  4. Codeforces Round #538 (Div. 2) (A-E题解)

    Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...

  5. Codeforces Round #531 (Div. 3) ABCDEF题解

    Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...

  6. Codeforces Round #527 (Div. 3) ABCDEF题解

    Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...

  7. Codeforces Round #499 (Div. 1)部分题解(B,C,D)

    Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...

  8. Codeforces Round #545 (Div. 1) 简要题解

    这里没有翻译 Codeforces Round #545 (Div. 1) T1 对于每行每列分别离散化,求出大于这个位置的数字的个数即可. # include <bits/stdc++.h&g ...

  9. Codeforces Round #624 (Div. 3)(题解)

    Codeforces Round #624 (Div.3) 题目地址:https://codeforces.ml/contest/1311 B题:WeirdSort 题意:给出含有n个元素的数组a,和 ...

  10. Codeforces Round #821(Div.2) (A-C) 题解

    Codeforces Round #821(Div.2) (A-C) A.Consecutive Sum 大致题意 给定一组共 n 个数据 ,如果俩个数的下标在 mod k 意义下同余,则可以交换a[ ...

随机推荐

  1. should contain指令的用法,校验结果

    上图案例 商品添加后,匹配"商品名称","现价","库存" 首先获取"商品名称","现价",&quo ...

  2. js录制系统/麦克风声音(基于electron)

    最近想用electron写一个和音视频相关的软件,尽管在写之前都想好了要用哪些技术,但依然写得很艰难,今天对相关知识的个人理解做个记录和整理. 时隔半年,最近发现网上有大神造好的轮子,而且功能强大:h ...

  3. Linux各种服务配置开机自启

    一.Linux配置redis开机自启 (1)到redis配置文件中找到conf文件:vi redis.conf (2)daemonize no 修改为:daemonize yes (3)cd /etc ...

  4. redis没有启动会报什么错???

    以下是报错信息,应该是根据项目不同,具体的不太一样,但我们能清晰的看到一句 加红,加大,加粗 Caused by: redis.clients.jedis.exceptions.JedisConnec ...

  5. 【安装】Ubuntu20.04下安装ROS的完整过程(内含已装好ROS的虚拟机、虚拟机创建过程、ROS安装过程及全过程录屏)

    现成的虚拟机 为方便大家学习,如果安装ROS遇到的问题实在太多,也可以直接下载我提供给大家的.已经安装好ROS的Ubuntu虚拟机:下载链接, 提取码:1030. 虚拟机的使用说明也在这个文件夹下,虚 ...

  6. 词库过大导致的Redis超时问题-RedisCommandTimeoutException

    问题 Redis缓存超时问题 报错内容 redis io.lettuce.core.RedisCommandTimeoutException: Command timed out after 10 s ...

  7. Flutter跨平台发送系统通知和状态栏通知技术浅析

    @charset "UTF-8"; .markdown-body { line-height: 1.75; font-weight: 400; font-size: 15px; o ...

  8. TVM Pass优化 -- InferType 类型推导

    定义(What) InferType,类型推断,顾名思义,给表达式进行类型的推断 直接上代码 import tvm from tvm import relay import numpy as np d ...

  9. 快速修改kafka的broker配置或topic配置

    下载开源的kafka界面客户端KafkaKing:https://github.com/Bronya0/Kafka-King 连接后,双击broker配置,或者双击topic配置: 修改好后回车保存就 ...

  10. 实现一个前端动态模块组件(Vite+原生JS)

    1. 引言 在前面的文章<使用Vite创建一个动态网页的前端项目>中我们实现了一个动态网页.不过这个动态网页的实用价值并不高,在真正实际的项目中我们希望的是能实现一个动态的模块组件.具体来 ...