补题日记1
2022.1.24牛客寒假算法基础集训营1
呜呜呜我是傻逼只做出来最基础的L签到题,简直是最简单的有手就行的,其他的我有尝试过EFH,我是撒比,都写了,都过不了,于是本人下定决心决定要补题!!上传csdn以此督促自己呜呜呜。
补了EFHAJI, 写了L,还有BCDKG,不是我现在能够的着的,加油小小白!!
H题
1
//来自我的撒比想法,因为n=1e6,n^2超时,时间复杂度O(N^2)
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
res+=abs(a[i]+a[j]-1000);
}
}
cout<<res<<endl;
2
//后生可畏cdf的hs记录次数写法,时间复杂度O(值域^2)=O(10^3^2),如果值域过大就不能这样
long long ans=0;
vector<long long> hs(1010);
for(int i=0;i<n;i++){
if(cin>>x)hs[x]++;
}
for(int i=0;i<=1010;i++){
for(int j=i+1;j<=1000;j++){
ans+=abs(i+j-1000)*hs[i]*hs[j];
}
ans+=abs(i+i-1000)*(hs[i]-1)*hs[i]/2;
ans+=abs(i+i-1000)*hs[i];
}
cout<<ans;
3
//来自康康子的二分加前缀和写法 时间复杂度O(N*logN) ;lowerbound查询复杂度O(logN)
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
ios::sync_with_stdio(0);
cin>>n;
long long ans=0;
vector<long long> a(n),pre(n+1);
for(int i=0;i<n;i++)cin>>a[i];
sort(a.begin(),a.end());
for(int i=0;i<n;i++)pre[i+1]=pre[i]+a[i];
for(int i=0;i<n;i++){
int j=lower_bound(a.begin()+i,a.end(),1000-a[i])-a.begin();
ans+=pre[n]-pre[j]+(n-j)*a[i]-(n-j)*1000;
ans+=1000*(j-i)-(pre[j]-pre[i]+(j-i)*a[i]);
}
cout<<ans;
return 0;
}
//网友的简短写法
const int N=1e6+5;
int n;
ll a[N],sum[N];
int main()
{
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
sum[a[i]]++;
}
ll ans=0;
for(int i=0;i<=1000;i++){
ans+=sum[i]*abs(i+i-1000);
for(int j=0;j<=1000;j++)
ans+=sum[i]*sum[j]*abs(i+j-1000);
}
cout<<ans/2<<endl;
}
5
//纯纯二分和前缀和
//#define IOS ios::sync_with_stdio(false),cin.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<char, pair<int, int> > PCII;
typedef pair<int, pair<int, int> > PIII;
const int maxn=1e6+7;
const int N = 1e5+7;
const int mod = 998244353;
const ll inf=1e18;
const double pi = 3.14159265359;
int sum[maxn];
int n;
vector<int> v,a;
int main()
{
a.push_back(0);
cin>>n;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
a.push_back(x);
}
sort(a.begin(),a.end());
for(int i=1;i<=n;i++) sum[i] = sum[i-1] + a[i];
ll ans = 0;
for(int i=1;i<=n;i++)
{
int num = 1000 - a[i]; //临界值
int l = i,r = n;
while(l < r)
{
int mid = l + r + 1>> 1;
if(a[mid] > num) r = mid - 1;
else l = mid;
}
int idx = l;
int idx = lower_bound(i-1+a.begin(),a.end(),num) - a.begin();
cout<<a.begin()<<endl;
// cout<<l<<endl;
if(idx != a.size()) //找到了(后面全是>=num的,前面全是<=num的)
{
ans += ((n-idx+1)*a[i] + (sum[n] - sum[idx-1]) - (n-idx+1)*1000);
ans += (1000*(idx-i) - (idx-i)*a[i] - (sum[idx-1] - sum[i-1]));
}
else
ans += (1000*(n-i) - (n-i)*a[i] - (sum[idx-1] - sum[i-1]));
}
cout<<ans<<endl;
return 0;
}
//499 500 500 501
//499 999 1499 2000
E题
1
//来自我的撒比想法,为什么为什么!样例都过了!!但是就是过不了!!
#include<bits/stdc++.h>
using namespace std;
long long t,res=0,x=0;
long long n,m;
int main(){
cin>>t;
while(t--){
cin>>n>>m;
if(n==m){cout<<1<<endl;continue;}
else if(n>1&&m==1){cout<<-1<<endl;continue;}
else if(n>m){
x++;res+=2;
while(n-x*m+x>m){res+=2;x++;}
if(n-x*m+x<=m){res++;}
cout<<res<<endl;
continue;
}
else if(n<=m) cout<<1<<endl;
}
return 0;
}
2
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;cin>>t;
while(t--)
{
int n,m,t=1;
cin>>n>>m;
if(m==1){
if(n==1)printf("1\n");
else printf("-1\n");
}
————————————————————————————————————————————————————————————————
1. else{
t=ceil(1.0*(n-m)/(m-1));//人麻了
printf("%d\n",t*2+1);//咱就是说咱推了一下午花生都没搞出来这个式子
}
2. else{
time=(n-2)/(m-1)*2+1;
}
cout << time << endl;
3. else
{
LL ans=1;
ans+=(n-2)/(m-1)*2;
printf("%lld\n",ans);
}
4. else
{
n -= m;
count = (n + m - 2) / (m - 1);
printf("%lld\n", count * 2 + 1);
}
5. else{
int num1=n-1;
int num2=m-1;
if(num1%num2==0)cout<<((num1/num2-1)*2)+1<<endl;
else cout<<((num1/num2)*2)+1<<endl;
}
————————————————————————————————————————————————————————————————
}
}
3
//康康的解法,感觉很迷惑55
int solve(){
cin>>n>>m;
if(n<=m)return 1;
if(n>1&&m==1)return -1;
int x=n-m,y=m+(x%(m-1)),z=(x/(m-1))*2;
while(y>m){
z+=2;
y-=m-1;
}
return z+1;
}
int main(){
cin>>t;
while(t--)
{cout<<solve()<<endl;}
return 0;
}
F题
//一整个没想明白,来自我的撒比代码
#include<bits/stdc++.h>
using namespace std;
int t;
long long n,m;
const int N=1e5+10;
int a[N];
int main(){
cin>>t;
while(t--){
cin>>n>>m;
for(int i=0;i<n;i++)cin>>a[i];
sort(a,a+n);
if(n%2==1){
if(a[n/2]<m){cout<<-1<<endl;continue;}
else if(a[n/2]==m){cout<<1<<endl;continue;}
else {cout<<n<<endl;continue;}
}
else{
if(a[n/2-1]<m){cout<<-1<<endl;continue;}
else if(a[n/2-1]==m){cout<<1<<endl;continue;}
else if(a[n/2-1]>m){
if(a[0]>=m)cout<<n<<endl;
else cout<<1+n/2<<endl;
continue;
}
}
}
return 0;
}
1
//永远能找到贼短的代码,但是看起来很有道理
#include<bits/stdc++.h>
using namespace std;
int main () {
int n;
cin >> n;
while(n--) {
int a,b,cot1 = 0,cot2 = 0;
cin >> a >> b;
int k[a];
for(int i = 0; i < a; i++) {
cin >> k[i];
if(k[i] >= b)cot1++;
else cot2++;
}
if(cot1 - cot2 <= 0)cout <<"-1"<<endl;
else cout << cot1 - cot2 << endl;
}
}
2
int n;cin>>n;
while(n--){
ll a,b,cnt=0,t;
cin>>a>>b;
rep(i, 1, a){
cin>>t;
if(t<b)cnt++;
}
if(a-cnt*2>0)cout<<a-cnt*2<<endl;
else cout<<-1<<endl;
3
void solve() {
int n, m;
cin >> n >> m;
int l, r;
l = r = 0;
for(int i = 0; i < n; i ++) {
int a ;
cin >> a;
if(a >= m) r ++;
else l ++;
}
if(r <= l) cout << -1 << '\n';
else cout << r - l << '\n';
}
4
int T;
long ans,n,i;
long long m,a[100001];
cin>>T;
while(T--)
{
scanf("%ld%lld",&n,&m);
ans=0;
for(i=0;i<n;i++)
{
scanf("%lld",&a[i]);
if(a[i]>=m)
ans++;
else
ans--;
}
if(ans<1)
puts("-1");
else
printf("%ld\n",ans);
I题
1
//康子的代码
const int N = 200010;
const int mod = 1e9 + 7;
int tt = 1;
int pqow(ll b, ll n){//快速幂求逆元
ll ret = 1;
for(int i = n; i; i >>= 1, b = (b * b) % mod)
if(i & 1) ret *= b, ret %= mod;
return ret;
}
int solve(){
ll n, m; cin >> n >> m;
if(n == 1) return 0;
ll fz = (pqow(2, n - 1) - 1) * m % mod;//数学期望求出来的式子求导得到
ll fm = pqow(pqow(2, n - 1), mod - 2) % mod;
return (fz * fm) % mod;
}
int main(){
ios::sync_with_stdio(0);
cin >> tt;
while(tt--)cout << solve() << endl;
return 0;
}
康康的教诲
2
ll quickpow(ll a, ll b) {
ll ans = 1;
ll res = a%mod;
while (b) {
if (b & 1)
ans = ans * res%mod;
b >>= 1;
res = res * res%mod;
}
return ans%mod;
}
void solve() {
int n,m; cin>>n>>m;
ll a = (quickpow(2,n-1)-1+mod)%mod;
a=a*m%mod;
ll b = quickpow(2,n-1);
ll ans = a*quickpow(b,mod-2)%mod;
cout<<ans<<endl;
}
3
int qmi(int a,int b)
{
int res=1;
while(b)
{
if(b&1)res=res*a%mod;
b>>=1;
a=a*a%mod;
}
return res;
}
void solve()
{
int n,m;
int ans=1;
cin>>n>>m;
int k = qmi(2,n-1);
int ni = qmi(k,mod-2);
cout<<m*(1-ni+mod)%mod<<'\n';
}
A题
1
#include<bits/stdc++.h>
using namespace std;
int n, f[100010][10], mod = 998244353;
int get(int x)
{
if(x % 9 == 0)
return 9;
return x % 9;
}
int main()
{
scanf("%d", &n);
vector<int> a(n + 1);
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
a[i] = get(a[i]);
}
f[0][0] = 1;
for(int i = 1; i <= n; i++)
for(int j = 0; j <= 9; j++)
{
f[i][j] = (f[i][j] + f[i - 1][j]) % mod;
int t = get(j + a[i]);
f[i][t] = (f[i][t] + f[i - 1][j]) % mod;
}
for(int i = 1; i <= 9; i++)
printf("%d ", f[n][i]);
puts("");
return 0;
}
2
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
const double eps = 1e-6;
const double PI = acos(-1.0);
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int mod = 998244353;
int dx1[4] = {-1, 0, 1, 0}, dy1[4] = {0, 1, 0, -1}; //上右下左
int dx2[8] = {-1, -1, -1, 0, 1, 1, 1, 0}, dy2[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; //从左上顺时针旋转
int dx3[8] = {-2, -1, 1, 2, 2, 1, -1, -2}, dy3[8] = {1, 2, 2, 1, -1, -2, -2, -1}; //日字,从右上顺时针
int T, n, m, k, ans;
int qmi(int a, int b, int p){int res = 1 % p;a %= p;while (b > 0){if(b & 1) res = (LL)res * a % p;a = (LL)a * a % p;b >>= 1;}return res;}
int doMod(int x){return (x % mod + mod) % mod;}
int lowbit(int x){return x & -x;}
LL gcd(LL a,LL b) {return b?gcd(b,a%b):a;}
void debug1(int x){cout << x << "====" << endl;}
void debug2(int x, int y){cout << x << "====" << y << endl;}
void debug3(int x, int y, int z){cout << x << "====" << y << "====" << z << endl;}
//void print1(){for (int i = 1; i <= n; i ++ ){printf("%d ", );}cout << endl;}
int w[N];
int f[N][10];
void print2(){for (int i = 1; i <= n; i ++ ){for (int j = 1; j <= 9; j ++ ) printf("%d ", f[i][j]);cout<<endl;}cout<<endl;}
int ff(int x)
{
while (x >= 10)
{
int t = 0;
while (x)
{
t += x % 10;
x /= 10;
}
x = t;
}
return x;
}
int main()
{
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
// exit(0);
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> w[i];
f[0][0] = 1;
for (int i = 1; i <= n; i ++ )
{
for (int j = 0; j <= 9; j ++ )
{
f[i][ff(j + w[i])] = (f[i][ff(j + w[i])] + f[i - 1][j]) % mod;
f[i][j] = (f[i][j] + f[i - 1][j]) % mod;
}
}
for (int i = 1; i < 10; i ++ )
{
cout << f[n][i] << ' ';
}
return 0;
}
3
using namespace std;
const int mod = 998244353;
const int maxn = 1e5 + 5;
int a[maxn];
int dp[maxn][10];
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] %= 9;
}
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 9; j++) {
dp[i][j] =( dp[i][j]+dp[i - 1][j])%mod;
dp[i][(j+a[i])%9]= (dp[i][(j + a[i]) % 9] + dp[i - 1][j]) % mod;
}
}
for (int i = 1; i <= 8; i++) cout << dp[n][i] << " ";
cout << dp[n][0] - 1 << endl;
return 0;
}
4
#include <cstdio>
#include <iostream>
using namespace std;
const int mod = 998244353;
const int maxn = 1e5 + 5;
int a[maxn];
int dp[maxn][10];
int main()
{
int n;cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] %= 9;
}
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= 9; j++) {
dp[i][j] =( dp[i][j]+dp[i - 1][j])%mod;
dp[i][(j+a[i])%9]= (dp[i][(j + a[i]) % 9] + dp[i - 1][j]) % mod;
}
}
for (int i = 1; i <= 8; i++) cout << dp[n][i] << " ";
cout << dp[n][0] - 1 << endl;
return 0;
}
J题
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define pb push_back
#define SZ(v) ((int)v.size())
#define fs first
#define sc second
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
int T,A,B,n;
int va[10010],vb[10010];
bool cmp(int xx,int yy){
return xx>yy;
}
int suma[10010],sumb[10010];
int main(){
cin>>T;
while(T--){
cin>>A>>B>>n;
rep(i,1,A) scanf("%d",&va[i]);
rep(i,1,B) scanf("%d",&vb[i]);
int mxb=min(n/2,B);
if(A+mxb<n){
puts("-1");
continue;
}
sort(va+1,va+1+A,cmp);
sort(vb+1,vb+1+B,cmp);
rep(i,1,A) suma[i]=suma[i-1]+va[i];
rep(i,1,B) sumb[i]=sumb[i-1]+vb[i];
int ans=-1;
rep(ia,0,A){
int ib=n-ia;
if(ib>mxb||ib<0) continue;
ans=max(ans,suma[ia]+sumb[ib]);
}
assert(ans!=-1);
printf("%d\n",ans);
}
return 0;
}
2
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int gg[10001],hh[10001];
int t,i,a,b,sum = 0,n,j;
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
scanf("%d",&t);
while(t--)
{
cin>>a>>b>>n;
for(i=1;i<=a;i++)
{
scanf("%d",&gg[i]);
}
for(i=1;i<=b;i++)
{
scanf("%d",&hh[i]);
}
if(2*a<n||a+b<n)//把所有乖小孩都选了还是不够
{
printf("-1\n");
continue;
}
int sum1[10001] = {0},sum2[10001] = {0};
sort(gg+1,gg+1+a,cmp);//将安静小孩从大到小排序
sort(hh+1,hh+1+b,cmp);//将闹腾小孩幸福指数从大到小排序
sum = 0;
i = 1;
j = 1;
for(i=1;i<=a;i++)//正着将每多一个小孩幸福指数增加多少给找出来存入数组
{
sum1[i]+=sum1[i-1]+gg[i];
}
for(i=1;i<=b;i++)
{
sum2[i]+=sum2[i-1]+hh[i];
}
sum = -1;
for(i=0;i<=a;i++)//从0开始,因为有可能一个不选
{
if((n-i)>b||n<i||(n-i)>n/2)
{
continue;
}
sum = max(sum,sum1[i]+sum2[n-i]);
}
printf("%d\n",sum);
}
return 0;
}
此处仅作个人学习记录,如涉及侵权什么的联系本人就好。
呜呜呜,记录代码不是关键,关键是思考过没,自己会不会写,下次能不能自己写出来。
但是不想这钱白花,所以在此记录我的补题日记,至少曾经水上有涟漪。
供以后看看监察和自省。