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;
}