zoukankan      html  css  js  c++  java
  • G.Eating Plan

    Bob is hungry now and he needs to eat some food. Alice puts nn dishes of food in front of him, numbered from 11 to nn. Alice tells him that he can only eat continuous dishes of food, or he will be poisoned by food. For example, if there are 1010 dishes, he can eat the food in the 22-nd, 33-rd and 44-th dishes but he can not eat the food in the 22-nd, 33-rd and 55-th dishes because there is the 44-th dish between them so it’s not continuous. Furthermore, if he chooses to eat food in the ii-th dish, he has to eat all food in that dish.

    Bob’s stomach has a strange feature that if there is at least t~(=998857459)t (=998857459) kg food in it, the weight in it will reduce tt kg repeatedly until it is strictly lower than tt kg. Also, if the weight of food in his stomach is exactly tt kg, his stomach will be empty. Now Bob wants to eat the smallest number of dishes and remains no less than kk kg food in his stomach. Can you tell him how many dishes he needs to choose?

    Input
    The first line contains two integers nn and m~(1leq nleq 100000,1leq mleq 10000)m (1≤n≤100000,1≤m≤10000), indicates the number of dishes and the number of queries.

    The second line contains nn integers a_1,a_2,cdots,a_na
    1

    ,a
    2

    ,⋯,a
    n

    , which is a permutation of 1, 2, cdots, n1,2,⋯,n, indicates that there is (a_i!)(a
    i

    !) kg food in the ii-th dish, where s! = 1 imes 2 imes 3 imes cdots imes ss!=1×2×3×⋯×s.

    The third line contains mm integers, the ii-th integer k_i(1leq k_i<t)k
    i

    (1≤k
    i

    <t) indicates a query with the lower bound of weight.

    Output
    Each line of mm lines contains an integer indicates the number of dishes Bob needs to choose in that query. If there is no way to reach Bob’s target, output -1 instead.

    样例输入复制
    4 2
    1 2 3 4
    29 31
    样例输出复制
    2
    3

    #include <bits/stdc++.h>
    #pragma GCC optimize(3 , "Ofast" , "inline")
    //struct node1{ char s[N] ; char *operator [] (int x) {return s + (x) * m ;}} c;
    //struct node2{int a[N] ;int *operator [] (int x) {return a + x * m ;}} s , t;
    using namespace std;
    typedef long long ll ;
    const int INF = 0x3f3f3f3f , N = 1e5 + 10 ;
    int n , m ;
    const int mod = 998857459 ;
    void read(int &x)
    {
    	x = 0 ;
    	char c = getchar() ;
    	while(!isdigit(c)) c = getchar() ;
    	while(isdigit(c)) x = x * 10 + c - 48 , c = getchar() ;
    }
    ll a[N] ;
    struct node
    {
    	ll pos , val ;
    	node() {}
    	node(ll pos , ll val) :pos(pos) , val(val) {} 
    }b[N] ;
    ll res[N] , sum[N]; 
    int main()
    {
    
        int n , m ;
    	a[0] = 1; 
    	for(int i = 1; i <= 2810 ;i ++)
    	 a[i] = a[i - 1] * i % mod ;
    	read(n) , read(m) ;
    	int idx ;
    	for(int i = 1; i <= n ;i ++)
    	 {
    	 	int x ; 
    	 	scanf("%d" , &x) ;
    	 	if(x <= 2802)
    	 	 b[++ idx] = {i , a[x]} ;
    	 }
    	for(int i = 1; i <= idx ;i ++)
    	  	sum[i] = (sum[i - 1] + b[i].val) % mod ;
    	for(int i = 1; i <= idx ;i ++)
    	 for(int j = i; j <= idx ;j ++)
    	    res[b[j].pos - b[i].pos + 1] = max(res[b[j].pos - b[i].pos + 1] , ((sum[j] - sum[i - 1]) % mod + mod) % mod )  ;
    	while(m --)
    	{
    		int x ;
    		scanf("%d" , &x) ;
    		int f = 0 ;
    		for(int i = 1 ; i <= n ;i ++)
    		 {
    		 	if(res[i] >= x)
    		 	 {
    		 	 	cout << i << endl ;
    		 	 	f = 1 ;
    		 	 	break ;
    			 }
    		 }
    		 if(!f) cout << -1 << endl ;
    	} 
    	return 0 ;
    }
    
    
    
    每次做题提醒自己:题目到底有没有读懂,有没有分析彻底、算法够不够贪心、暴力够不够优雅。
  • 相关阅读:
    28. Implement strStr()(KMP字符串匹配算法)
    60. Permutation Sequence(求全排列的第k个排列)
    47. Permutations II (全排列有重复的元素)
    46. Permutations (全排列)
    3. Longest Substring Without Repeating Characters(最长子串,双指针+hash)
    Python 一行代码实现并行
    1. Two Sum
    236. Lowest Common Ancestor of a Binary Tree(最低公共祖先,难理解)
    ssm项目配置多个数据源
    SpringMVC架构实现原理
  • 原文地址:https://www.cnblogs.com/spnooyseed/p/12870863.html
Copyright © 2011-2022 走看看