题目地址: http://codeforces.com/contest/332

第一题:题目又臭又长,读了好长时间才读懂。

n个人,你是0号,从0开始到n-1循环做动作,只要你前面三个人动作一样,你就喝一杯橙汁,问你能喝多少杯,

模拟

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std; typedef __int64 LL;
const int N=20005;
const LL II=1000000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0); char s[N]; int main()
{
int i,j,n;
while(cin>>n)
{
scanf("%s",s);
int len=strlen(s),sum=0;
for(i=0;i<len;)
{
if((i+n)<len)
{
i=i+n;
if(s[i-1]==s[i-2]&&s[i-2]==s[i-3])
{
sum++;
}
}
else
break;
}
cout<<sum<<endl;
}
return 0;
}

第二题:给你n个数,要你求一个k使得[aa + k - 1] 与[bb + k - 1]的和最大,保证这两个集合不想交。

1、相当于两个dp吧

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std; typedef __int64 LL;
const int N=200005;
const LL II=1000000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0); LL x[N];
LL dp[N]; int main()
{
LL i,j,n,k;
while(scanf("%I64d%I64d",&n,&k)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%I64d",&x[i]);
memset(dp,0,sizeof(dp));
for(i=1;i<=k;i++)
dp[1]+=x[i];
for(i=2;i<=n-k+1;i++)
dp[i]=dp[i-1]-x[i-1]+x[i+k-1];
LL max1=dp[1],Max=0;
int t1,t2,te=1;
for(i=1+k;i<=n-k+1;i++)
{
if((dp[i]+max1)>Max)
{
Max=dp[i]+max1;
t1=te; t2=i;
}
if(max1<dp[i-k+1])
{
max1=dp[i-k+1];
te=i-k+1;
}
}
printf("%d %d\n",t1,t2);
}
return 0;
}

2、RMQ算法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std; typedef __int64 LL;
const int N=200005;
const LL II=1000000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0); LL x[N];
LL dp[N];
LL d[N][20]; LL RMQ(int l,int r)
{
int k=0;
while((1<<(k+1))<=(r-l+1))
k++;
return max(d[l][k],d[r-(1<<k)+1][k]);
} int main()
{
int i,j,n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(i=0;i<n;i++)
scanf("%I64d",&x[i]);
memset(dp,0,sizeof(dp));
for(i=0;i<k;i++)
dp[0]+=x[i];
int p=n-k+1;
for(i=1;i<p;i++)
dp[i]=dp[i-1]-x[i-1]+x[i+k-1];
for(i=0;i<p;i++)
d[i][0]=dp[i];
for(j=1;(1<<j)<=p;j++)
for(i=0;i+(1<<j)-1<p;i++)
d[i][j]=max(d[i][j-1],d[i+(1<<(j-1))][j-1]);
LL Max=0,tmp,kk;
int t1,t2;
for(i=0;i<p;i++)
{
if((i+k)<p)
{
LL s=dp[i]+(tmp=RMQ(i+k,p-1));
if(Max<s)
{
Max=s; kk=tmp;
t1=i; t2=i+k;
} }
}
for(j=t2;j<p;j++)
if(dp[j]==kk)
{
t2=j;break;
}
printf("%d %d\n",t1+1,t2+1);
}
return 0;
}

最新文章

  1. jshint配置(js检查)
  2. mongo 日记
  3. 提高jQuery执行效率需要注意几点
  4. Wildfly8 更改response header中的Server参数
  5. GCD 多线程
  6. A9.linux驱动
  7. HDU 5534 Partial Tree
  8. scrollTop的兼容性
  9. 微信小程序页面跳转的问题(app.json中设置tarBar后wx.redirectTo和wx.navigateTo均不能实现跳转到指定的页面)
  10. java特征
  11. Mysql 相关操作
  12. [HDU4864]Task (贪心)
  13. css模拟时钟
  14. Linux模拟网络延迟、丢包等
  15. topcoder srm 701 div1 -3
  16. Dubbo的原理以及详细原理、配置
  17. wpf 千位符 格式化字符串
  18. nyoj 单调递增子序列(二)
  19. [network]交换机中用户权限
  20. clean code 第一章笔记

热门文章

  1. UITableView刷新局部
  2. The Swift Programming Language-官方教程精译Swift(1)小试牛刀
  3. Java替换字符或十进制数的字符串
  4. 让低版本的IE浏览器 强制渲染为IE8 或者 以上 浏览器模式
  5. 打印man手册为pdf文件
  6. 定制openwrt的根文件
  7. shell awk统计重复个数
  8. 【DateTime格式大全 】
  9. jquery 超简单的点赞效果
  10. 01.由浅入深学习.NET CLR 基础系列之CLR 的执行模型