Codeforces Round #673 (Div. 2) ABC 题解
A. Copy-paste
题意:问在保持每个数都小于等于k的情况下,最多能执行多少步a[j] += a[i] ,其中(i,j)为任意不同下标。
思路:水题,排个序,用a[1]去加到别的值上,看每个数能加多少个a[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 main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read(), k = read();
rep(i,1,n) a[i] = read();
ll cnt = 0;
sort(a+1,a+1+n);
rep(i,2,n)
{
ll d = k - a[i];
if(d>=0) cnt += d/a[1];
}
cout<<cnt<<endl;
}
return 0;
}
B. Two Arrays
题意:让你将a序列分成两部分,每部分的贡献是a[i]+a[j]==T的二元对的个数,问你怎么分配才能使得两部分的和最小。
思路:贪心。
首先先对等于T一半的进行特判,尽量平均分在两堆里。
其次若\(a[i]+a[j] == T\), 那就直接01分配即可。
最后注意此时的a[j]可能有多个(如T的6时的2 4 4 4, 4要丢到一起),要一视同仁。
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];
ll p[maxn];
map<ll,ll> sta;
map<ll,ll> Map;
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read(), t = read();
Map.clear();
sta.clear();
rep(i,1,n) a[i] = read(), p[i] = 0, Map[a[i]]=1;
rep(i,1,n)
{
if(Map[a[i]]==0) continue;
if(Map[t-a[i]]) Map[t-a[i]]--, sta[a[i]]=0, sta[t-a[i]] = 1;
else sta[a[i]] = 0;
}
int flag = 0;
rep(i,1,n)
{
if(a[i]*2==t) p[i] = flag, flag = !flag;
else
p[i] = sta[a[i]];
}
rep(i,1,n) cout<<p[i]<<' '; cout<<endl;
}
return 0;
}
C. k-Amazing Numbers
题意:定义K_NUM为长度为K的所有子串中都出现过的且最小的那个数,现在问你这个序列的每个K_NUM,K从1到n。
思路:动态规划dp。
考虑到要每个子串都出现,那一定要“连得起来”,即当前a[i], 在[i+1,i+k-1]这个区间里也要有a[i]。
所以对每个a[i],先把它下一次出现的地方用nxt[a[i]]]数组记录下来,这样每两个a[i]之间的间隔就是nxt[a[i]] - i 。
然后我们要联通的话,就需要这些间隔的最大值要小于等于k,表明至少要这么多才能保证这些a[i]联通起来。
所以再用一个cur数组记录当前往后的最大间距,那么就可以写状态转移了,dp[i]记录的即是答案:
若\(cur[i] <= k\),说明k够大,足以联通,就
\(dp[i] = min(a[i], min(dp[i-1], dp[i]))\)
否则,说明还不够大,那就先更新间距为cur[i]时的dp[cur[i]]
\(dp[cur[i]] = min(dp[cur[i]], a[i])\)
最后注意无论情况如何dp[i]都要和dp[i-1]做一次比较,因为满足前i-1的最小值肯定也满足前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>
#include <unordered_map>
#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 = 3e5+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];
ll Map[maxn];
ll nxt[maxn];
ll res[maxn];
ll cur[maxn];
ll dp[maxn];
int main()
{
int kase;
cin>>kase;
while(kase--)
{
ll n = read();
rep(i,1,n) a[i] = read(), Map[a[i]] = 0, nxt[a[i]] = n+1, dp[i] = inf;
res[n] = 0;
cur[n] = 0;
per(i,n,1) //res数组记录间距,cur记录当前往后的最大间距
{
res[i] = nxt[a[i]] - i, cur[i] = max(res[i], cur[nxt[a[i]]]);
nxt[a[i]] = i;
}
dp[0] = inf;
rep(i,1,n)
{
dp[i] = min(dp[i], dp[i-1]);
ll k = i;
if(cur[i]<=k) dp[i] = min(a[i], min(dp[i-1], dp[i]));
else dp[cur[i]] = min(dp[cur[i]], a[i]);
}
rep(i,1,n) cout<<(dp[i]==inf?-1:dp[i])<<' '; cout<<endl;
}
return 0;
}
Codeforces Round #673 (Div. 2) ABC 题解的更多相关文章
- Codeforces Round #312 (Div. 2) ABC题解
[比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地. 拉拉土地是一个很漂亮的国家,位于坐标线.拉拉土地是与著名的苹果树越来 ...
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- Codeforces Round #673 (Div. 2)
[Codeforces Round #673 (Div. 2) ] 题目链接# A. Copy-paste 思路: 贪心的策略.每次只加上最小的就可以了 #include<bits/stdc++ ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- Codeforces Round #538 (Div. 2) (A-E题解)
Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...
- Codeforces Round #531 (Div. 3) ABCDEF题解
Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...
- Codeforces Round #527 (Div. 3) ABCDEF题解
Codeforces Round #527 (Div. 3) 题解 题目总链接:https://codeforces.com/contest/1092 A. Uniform String 题意: 输入 ...
- Codeforces Round #499 (Div. 1)部分题解(B,C,D)
Codeforces Round #499 (Div. 1) 这场本来想和同学一起打\(\rm virtual\ contest\)的,结果有事耽搁了,之后又陆陆续续写了些,就综合起来发一篇题解. B ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
随机推荐
- java中堆污染(heap pollution)以及@SafeVarargs注解使用
什么是堆污染 heap pollution 堆污染发生在使用可变参数(varargs)或泛型时,将不兼容的类型插入到一个泛型对象中.这会导致在运行时尝试访问这些对象时发生 ClassCastExcep ...
- Linux 的那些操作都出自哪里?
Linux 的那些操作都出自哪里? 可以说 Linux 是一种 Unix.Unix 有一个 man 手册,手册包含了安装的软件的使用帮助,遇到问题的解决办法.总之几乎所有的操作都是手册里面有迹可循的, ...
- Nacos源码—5.Nacos配置中心实现分析
大纲 1.关于Nacos配置中心的几个问题 2.Nacos如何整合SpringBoot读取远程配置 3.Nacos加载读取远程配置数据的源码分析 4.客户端如何感知远程配置数据的变更 5.集群架构下节 ...
- vue3 基础-自定义指令 directive
上篇内容是关于 mixin 混入的一些操作, 也是关于代码复用层面的, 本篇讲自定义指令 directive 也是为了实现复用而设计的一些小功能啦. 先来看看, 如果不用 directive 的场景下 ...
- vue3 基础-表单元素双向绑定
通常是在 form 表单相关的场景中会用到双向绑定相关, 核心是 v-model 的应用. input 输入框 <!DOCTYPE html> <html lang="en ...
- pytorch中的剪枝操作
深度学习技术依赖于过参数化模型,这是不利于部署的,相反,生物神经网络是使用高效的稀疏连接的. 通过减少模型中的参数数量来压缩模型的技术非常重要,为减少内存.电池和硬件的消耗,而牺牲准确性,实现在设备上 ...
- C# 之静态构造器与静态字段初始化器
public class Test { /// <summary> /// 静态字段初始化器会在调用静态构造器前运行. /// 如果类型没有静态构造器,字段会在类型被使用前或运行时中更早的 ...
- 一个大对象引起的血案,GC的踩坑实录
背景: 问题: 有个渠道支付服务,负责与所有支付相关服务进行交互,包括 渠道下单支付,渠道成功通知,渠道的对账等 服务4台机,平时跑的都很稳定,通过thrift进行对外提供服务,且平时并未发现访问 ...
- React Native开发鸿蒙Next---灰度模式
React Native开发鸿蒙Next---灰度模式 政企相关的App在开发过程中,往往需要制作一个灰度模式,用于应对注入国家公祭日等特殊日期情况.Harmony开发中,由于基于ArkTs,处理相对 ...
- 如何实现本地大模型与MCP集成
1.概述 本文将围绕构建兼具本地运行大型语言模型(LLM)与MCP 集成能力的 AI 驱动工具展开,为读者提供从原理到实践的全流程指南.通过深度整合本地大模型的隐私性.可控性优势与 MCP 工具的自动 ...