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 ...
随机推荐
- Condition的await()方法底层源码
一.Condition的await()方法底层源码 以下是 ConditionObject 中 await 方法的源码及其详细分析: public final void await() throws ...
- Spring Security认证与授权
什么是Spring Security Spring Security是基于Spring框架,提供了一套Web应用安全性框架.专门为Java应用提供用户认证(Authentication)和用户授权(A ...
- 腾讯Java后端一面,被速通了!
分享一篇腾讯的后端Java一面凉经,被速通了, 大家感受一下难度如何. 这次面试的考察覆盖了从 项目经验的深度挖掘(面试官非常看重 STAR 法则的应用)到 扎实的计算机基础(经典的 TCP/UDP ...
- NOIP集训 P4137 Rmq Problem / mex 题解
前置指使:可持久化线段树 题解:P4137 Rmq Problem / mex 有一个长度为 \(n\) 的数组 \(\{ a_1,a_2,...,a_n \}\) . \(m\) 次询问,每次询问一 ...
- MySql的information_schema.processlist库学习之"如何检测出大数据sql查询"
1.如何通过MySql检测出大数据sql查询 一般数据库都会存在:information_schema数据库 检测出大数据sql查询[time时间越长说明,数据量越大,要根据公司的限度来衡量,我的思路 ...
- IDEA问题之“调整IDEA字体大小”
调整IDEA字体大小 1.正常版 2. 远程版
- 内网服务器离线安装部署 Ollama
一.安装 Ollama 1.官网下载地址:Releases · ollama/ollama 2.cd至下载目录 3.执行二进制文件安装 sudo tar -C /usr -xzf ollama-lin ...
- JuiceFS v1.3-Beta1:一亿文件备份分钟级完成,性能优化全解析
在最新发布的 JuiceFS v1.3 Beta1 版本中,我们引入了一种全新的二进制备份机制,旨在更高效地应对亿级文件规模下的备份与迁移场景.相比现有的 JSON 备份方式,该机制在导入导出元数据时 ...
- L2-3、Prompt结构化思维助力复杂任务:分步骤提示与多任务合并技巧
一.什么是 CoT(Chain of Thought)提示法? 结构化思维在人工智能交互中的重要性日益凸显,其中Chain of Thought(CoT,思维链)提示法是一种强大的技术,能够显著提升A ...
- 浅析Java8中default关键字
摘要:介绍Java8新增关键字default,它用于在接口中标记方法为默认方法和编写实现逻辑,方便通过新增方法重构接口,而无需修改所有实现类,目的在于兼容接口已有实现类. 综述 default关键 ...