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 题解的更多相关文章

  1. Codeforces Round #312 (Div. 2) ABC题解

    [比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地. 拉拉土地是一个很漂亮的国家,位于坐标线.拉拉土地是与著名的苹果树越来 ...

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

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

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

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

  4. Codeforces Round #673 (Div. 2)

    [Codeforces Round #673 (Div. 2) ] 题目链接# A. Copy-paste 思路: 贪心的策略.每次只加上最小的就可以了 #include<bits/stdc++ ...

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

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

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

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

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

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

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

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

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

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

  10. 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 ...

随机推荐

  1. Cookie,Session与Token

    参考资料 水平有限,欢迎交流!仅做学习交流使用 一定要看,非常透彻![Cookie.Session.Token究竟区别在哪?如何进行身份认证,保持用户登录状态?] 黑马jwt详解 Javaweb程序设 ...

  2. 🎀截图工具推荐-Snipaste

    简介 Snipaste 是一款非常强大且免费的截图和屏幕标记工具,由一位来自中国的开发者开发.它以其简洁的界面和丰富的功能而受到广泛好评. 官网 https://zh.snipaste.com/ Sn ...

  3. kali安装charles

    00X01 kali安装charles wget -q -O - http://www.charlesproxy.com/packages/apt/PublicKey | sudo apt-key a ...

  4. eclipse中那些难以分辨的符号、Java中的Long和mysql中的bigint

    这是一个字符串 从左到右依次是数字"0", 大写字母"O",小写字母"o",数字"1",小写字母"l(艾欧)& ...

  5. Java5新特性--可变参数

    可变参数 public class Test01 { public static void main(String[] args) { System.out.println(add(123)); Sy ...

  6. 自定义工具类之”判断两个集合中是否有一个相同的值-》CollectionUtils.containsAny(集合1,集合2)“

    判断两个集合中是否有一个相同的值 CollectionUtils.containsAny(集合1,集合2)就可以满足以下条件 两个集合中,只要有一个值相同就直接返回true 如:集合1:"1 ...

  7. Linux安装Libevent

    环境 Ubuntu 20.04.2 64位 软件包安装 通过apt-get 命令可以直接安装Libevent,这种方式方便快捷,省时省力. 安装命令如下: sudo apt-get install l ...

  8. RPC实战与核心原理之路由策略

    路由策略:怎么让请求按照设定的规则发到不同的节点上 回顾 健康检测在 RPC 中的作用,简单来讲就是帮助调用方应用来管理所有服务提供方的连接,并动态维护每个连接的状态,方便服务调用方在每次发起请求的时 ...

  9. WPF与WinForm的对比

    WPF与WinForm的对比 本文同时为b站WPF课程的笔记,相关示例代码 创建新项目 在vs2022中,这两者分别叫做WPF应用和Windows窗体应用. 渲染引擎和设计 WPF使用DirectX作 ...

  10. git基础及gitee配置

    安装git 网址:https://git-scm.com/book/zh/v2/起步-安装-Git 使用git 基本指令 # 初始化指令 git init # 管理目录下的文件状态 注:新增文件和修改 ...