zoukankan      html  css  js  c++  java
  • [每日一题]:CodeForces

    题目:

    题目大意:

    给一个序列,然后给你一个数 s ,从头开始进行累加,不能超过 s
    在累加的过程中,你可以 跳过 一个数 进行累加,最后保证累加的
    数量是最多的,不是累加的和哦,输出你跳过的那个数在序列中的位置。
    (根据样例推断,似乎只能从前开始累加,否则第一个样例就有误了,嘻嘻)
    

    考察点:

    签到题,模拟即可。
    

    Code:

    #include <cstdio>
    #include <string>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn = 1e5 + 10;
    
    typedef long long LL;
    
    int a[maxn]; 
    
    int t,n,s;
    
    int main(void) {
    	scanf("%d",&t);
    	while(t --) {
    		LL res = 0,ans = 0;
    		scanf("%d%d",&n,&s);
    		for(int i = 1; i <= n; i ++) {
    			scanf("%d",&a[i]);
    			ans += a[i];
    		}
    		// 总和 <= s 时直接跳过 
    		if(ans <= s) {
    			puts("0");
    			continue;
    		}
    		// 存储跳过的位置,时刻更新最大值 
    		int pos = 0,maxValue = -1;
    		bool flag = false;
    		for(int i = 1; i <= n; i ++) {
    			res += a[i];
    			if(res > s && flag) {
    				res -= a[i];
    				break;
    			}
    			if(res > s) {
    				res -= a[pos];
    				flag = true;
    			}
    			if(maxValue < a[i]) maxValue = a[i],pos = i;
    		}
    		printf("%d
    ",pos);
    	}	
    	return 0;
    } 
    
    

    后记:

    今天的题目有点水了,抱歉,各位!
  • 相关阅读:
    访问者模式
    中介者模式
    策略模式
    迭代器模式
    责任链模式
    contentProvider模板
    android studio常用快捷键(不断补充)
    jqgrid表格列动态加载的实现
    Android View.onMeasure方法的理解(转载)
    activity的生命周期
  • 原文地址:https://www.cnblogs.com/prjruckyone/p/12781869.html
Copyright © 2011-2022 走看看