【Java/补题/CF/ACM赛制】 The 2022 Hangzhou Normal U Summer Trials

题目链接

The 2022 Hangzhou Normal U Summer Trials

知识一览

01 - 二分
02 - 前缀和/子数组

题目列表

快输

    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        String tmp;

        public FastReader() {
            br=new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while(st==null||!st.hasMoreElements()) {
                try {
                    st=new StringTokenizer(br.readLine());
                }catch(IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong(){return Long.parseLong(next());}

        String nextLine() {
            String str="";
            try {
                str=br.readLine();
            }catch(IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        boolean hasNext(){
            if(st!=null&&st.hasMoreTokens())return true;
            try {
                tmp=br.readLine();
                st=new StringTokenizer(tmp);
            }catch(IOException e) {
                return false;
            }
            return true;
        }
    }

    static PrintWriter out=new PrintWriter(
            new BufferedWriter(new OutputStreamWriter(System.out)));
    static FastReader sc=new FastReader();

C - Check Problems(二分/模拟)

#pragma GCC optimize("Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;

const ll maxn=5e5+10;
ll n,k;
ll a[maxn];
ll sum[maxn];
ll s[maxn];
int main(){
	scanf("%lld",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		
	}
	for(int i=2;i<=n;i++){
		sum[i]=a[i]-a[i-1];
		s[i]=s[i-1]+sum[i];
	}
	sum[n+1]=1e18;
	scanf("%lld",&k);
	while(k--){
		ll x;
		scanf("%lld",&x);
		ll l=1,r=n+1;
		while(l<r){
			ll mid=(l+r)>>1;
			if(sum[mid]>=x){
				r=mid;
			}else{
				l=mid+1;
			}
		}
		l--;
		ll ans=0;
		ans=(n-l+1)*x;
		printf("%lld\n",ans+s[l]);
	}
	return 0;
}

F - Subarrays(前缀和)

题解:F - Subarrays

	static int N=(int)1e5+10;
    static Map<Long,Long> mp;
    static long n,k,a[]=new long[N],sum=0,ans=0;
    public static void main(String[] args) {
        //int T=sc.nextInt();
        int T=1;
        while(T-->0){
            solve();
        }
    }
    private static void solve(){
        n=sc.nextLong();
        k=sc.nextLong();
        mp=new HashMap<>();
        mp.put((long)0,(long)1);
        for(int i=1;i<=n;i++){
            a[i]=sc.nextLong();
            sum=sum+a[i];//算出前i个数的和
            sum=sum%k;//分别求出前i个数%k的余数,并用map存起来
            ans+=mp.getOrDefault(sum,(long)0);
            mp.put(sum,mp.getOrDefault(sum,(long)0)+1);
        }
        out.println(ans);
        out.flush();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值