zoukankan      html  css  js  c++  java
  • 每日算法

    每日算法

    those times when you get up early and you work hard; those times when you stay up late and you work hard; those times when don’t feel like working — you’re too tired, you don’t want to push yourself — but you do it anyway. That is actually the dream. That’s the dream. It’s not the destination, it’s the journey. And if you guys can understand that, what you’ll see happen is that you won’t accomplish your dreams, your dreams won’t come true, something greater will. mamba out


    那些你早出晚归付出的刻苦努力,你不想训练,当你觉的太累了但还是要咬牙坚持的时候,那就是在追逐梦想,不要在意终点有什么,要享受路途的过程,或许你不能成就梦想,但一定会有更伟大的事情随之而来。 mamba out~

    2020.3.24


    lqb -乘积最大

    如果 k < n , 那么就要思考怎样去选择了:
    1.k 如果是偶数的话,选出来的结果一定是非负数 , 原因如下:
    (1) # 负数的个数是偶数个的话,负负得正,那么一定是非负数
    (2) # 负数的个数如果是奇数个的话,那么我们就只选偶数个绝对值最大的负数
    2.k 如果是奇数个的话,
    (1)# 所有的数字如果都是负数,那么选出来的结果也一定都是负数
    (2)# 否则的话,则一定至少有 1个非负数, 那么我们将最大的数取出来, 此时要选的个数就是 k--

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <string>
    
    using namespace std;
    typedef long long ll;
    const int N = 100005;
    const int mod =  1000000009;
    int a[N];
    int n , k;
    
    ll f(ll a,ll b)
    {
    	return a % mod * b % mod;
    }
    int main()
    {
    	cin >> n >> k;
    	for(int i = 0;i < n ;i ++)
    	{
    		scanf("%d",&a[i]);
    	}
    	
    	sort(a , a + n);
    	int ans = 1; 
    	int l = 0,r = n - 1;
    	int sign = 1;
    	
    	if(k % 2)
    	{
    		ans = a[r--];
    		k--;
    		if(ans < 0)sign = -1;
    	}
    	while(k)
    	{
    		ll x = (ll)a[l] * a[l + 1];
    		ll y = (ll)a[r - 1] * a[r];
    		
    		if(x * sign > y * sign)
    		{
    			ans = x % mod * ans % mod;
    			l += 2;
    		}else{
    			ans = y % mod * ans % mod;
    			r -= 2;
    		}
    		k -= 2;
    	}
    	cout << ans << endl;
    	return 0;
    }
    

    luogu- P5057 [CQOI2006]简单题

    区间修改和单点查询的树状数组,最后在求每个点的值的时候,如果该点的前缀和为奇数差分前缀和实际上存储的是每个位置的修改次数如果是奇数次那么结果肯定为1,否则为0

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <string>
    #define lowbit(x) (x & -x)
    using namespace std;
    typedef long long ll;
    
    const int N = 500005;
    int tr[N], n , m;
    
    void add(int x,int v)
    {
    	for(int i = x;i <= n ;i += lowbit(i))tr[i] += v;
    }
    
    ll query(int x)
    {
    	ll ans = 0;
    	for(int i = x; i; i -= lowbit(i))
    		ans += tr[i];
    	return ans;
    }
    
    int main()
    {
    	cin >> n >> m;
    	
    	for(int i = 0;i < m;i ++)
    	{
    		int a , l , r;
    		cin >> a;
    		if(a == 1){
    			cin >> l >> r;
    			add(l,1),add(r + 1,-1);
    			
    		}else{
    			cin >> l;
    			cout << query(l) % 2 << endl;
    		}
    	}
    	return 0;
    }
    

    luogu -P3368 【模板】树状数组 2

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <string>
    #define lowbit(x) (x & -x)
    using namespace std;
    typedef long long ll;
    
    const int N = 500005;
    ll a[N] , tr[N], n , m;
    
    void add(int x,int v)
    {
    	for(int i = x;i <= n ;i += lowbit(i))tr[i] += v;
    }
    
    ll query(int x)
    {
    	ll ans = 0;
    	for(int i = x; i; i -= lowbit(i))
    		ans += tr[i];
    	return ans;
    }
    
    int main()
    {
    	cin >> n >> m;
    	int pre = 0 , now;
    	for(int i = 1;i <= n ;i ++)
    	{
    		scanf("%d",&now);
    		add(i,now-pre);
    		pre = now;
    	}
    	for(int i = 0;i < m;i ++)
    	{
    		int a , l , r , k;
    		cin >> a;
    		if(a == 1){
    			scanf("%d %d %d",&l,&r,&k);
    			add(l,k),add(r + 1,-k);
    		}else{
    			scanf("%d",&l);
    			printf("%lld
    ",query(l));
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    奖学金 题解
    大数加法
    删除倒数第 N 个节点
    css中行内元素默认间隙解决方案
    vuecli3项目中优化lodash/moment使用
    谷歌浏览器input输入框自动填充数据
    vuecli3首页白屏优化
    highcharts开发交易所的行情走势图
    react-native使用flatlist上拉加载下拉刷新
    放大预览图片不失真
  • 原文地址:https://www.cnblogs.com/wlw-x/p/12560877.html
Copyright © 2011-2022 走看看