hdu 6025 card card card(双指针)

题目链接:hdu 6025 card card card

题意:

有n对数(a,b),现在你可以将前x对(a,b)移到尾部。

操作完后,现在定义sum=ai-bi (1,x),当sum<0时,当前的价值为Σa(1<=i<=x-1)。

问你移动前多少对(a,b),使得价值最大,如果有多个答案,输出最小的那个。

题解:

复制一份,双指针模拟一下就行了。

 1 #include<bits/stdc++.h>
 2 #define F(i,a,b) for(int i=a;i<=b;++i)
 3 using namespace std;
 4
 5 const int N=2e6+7;
 6 int n,a[N],b[N],ans,ansval;
 7
 8 int main(){
 9     while(~scanf("%d",&n))
10     {
11         F(i,1,n)scanf("%d",a+i),a[i+n]=a[i];
12         F(i,1,n)scanf("%d",b+i),b[i+n]=b[i];
13         ans=0,ansval=0,n*=2;
14         int l=1,sum=0,val=0;
15         F(r,1,n)
16         {
17             sum+=a[r]-b[r],val+=a[r];
18             if(val>ansval)ans=l-1,ansval=val;
19             while(sum<0||r-l+1>n)
20                 sum-=a[l]-b[l],val-=a[l],l++;
21         }
22         printf("%d\n",ans);
23     }
24     return 0;
25 }

时间: 2024-11-12 12:26:21

hdu 6025 card card card(双指针)的相关文章

hdu 6025 Coprime Sequence (前后缀GCD)

题目链接:hdu 6025 Coprime Sequence 题意: 给你n个数,让你删掉一个数,使得剩下的数的gcd最大 题解: 先将这一列数的前缀后缀gcd预处理一下. 然后挨着for一下就行了 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 5 const int N=1e5+7; 6 int t,pre[N],suf[N],n,a[N]; 7 8

2017ICPC沈阳网络赛 HDU 6025 -- card card card(最大子段和)

card card card Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1230    Accepted Submission(s): 549 Problem Description As a fan of Doudizhu, WYJ likes collecting playing cards very much. One day

POJ 2062 HDU 1528 ZOJ 2223 Card Game Cheater

水题,感觉和田忌赛马差不多 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; struct P1 { int Num; int Hua; } Play1[30]; struct P2 { int Num; int Hua; } Play2[30]; bool cmp1(const P1&a,const P1&b

HDU 6103 Kirinriki (思维 双指针)

Kirinriki Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2169    Accepted Submission(s): 879 Problem Description We define the distance of two strings A and B with same length n isdisA,B=∑i=0n−

hdu 6025

题意:t组,n个数,去掉任意一个数,使n-1个数的GCD最大 思路:求前缀gcd,后缀gcd,然后max(gcd(gcd(1,i),gcd(i+1,n)). 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int a[100004]; 5 int b[100004]; 6 int c[100004]; 7 8 int main(){ 9 int t; 10 scanf("%d&quo

HDU 6025 Coprime Sequence

Coprime Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 44    Accepted Submission(s): 34 Problem Description Do you know what is called ``Coprime Sequence''? That is a sequence consis

hdu 6025 前缀 后缀 gcd

大致题意: 去掉一个元素能使这个数列的GCD最大为多少 分析: 我们求一个数列的GCD,是先求前两个元素的GCD,然后将这个GCD值在与下一个元素进行GCD运算.由此可知进行GCD运算的顺序对最终的结果是没有影响的.我们再看看数列的长度范围,小于100000.那我们就枚举去掉的那个元素,那么去掉元素后的数列的GCD即这个元素前面一段数列的GCD再与这个元素后面一段数列的GCD进行GCD运算.所以我们需要两个数组分别记录前缀GCD和后缀GCD,这两个数组都可以通过O(n)算法算出来. #inclu

HDU - 6025 Coprime Sequence(gcd+前缀后缀)

Do you know what is called ``Coprime Sequence''? That is a sequence consists of nnpositive integers, and the GCD (Greatest Common Divisor) of them is equal to 1. ``Coprime Sequence'' is easy to find because of its restriction. But we can try to maxim

uestcoj 890 Card Trick(dp+逆推)

题目链接: 啊哈哈,点我点我 思路:从终点向前递推. 首先p[I]表示从第i个点到终点的概率.则分为两种情况进行考虑. [1]已经翻到的点则它必定会到终点,则概率为1. [2]不知道的点则要进行枚举.那么p[i]=sum(p[i+j])/13(2=<j<=13).那么这个问题就解决了.. 为什么要逆推,因为从前往后走,要用到后面的状态. 哎,自己的dp好弱啊,一个暑假好像都没怎么做..哎,加油啊!!! 题目: Card Trick Time Limit: 2999/999MS (Java/Ot