Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)
题意:
分析:
代码:
//尺取法
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std; char inp[];
int ch[]; int main()
{
int n,s,e,i,poke = ,curpoke = ,res = n;
scanf("%d",&n);getchar();scanf("%s",inp);
for(i = ; i < n; i++) ch[inp[i]]++; for(i = ; i < ; i++)if(ch[i])poke++;
for(i = ; i < ; i++)ch[i] = ;
s=e=;
while(s <= e && e <= n)
{
if(curpoke < poke)
{
if(ch[inp[e]] == ) curpoke++;
ch[inp[e]]++; e++;
}
else
{
if(ch[inp[s]] == ) curpoke--;
ch[inp[s]]--;
s++;
}
if(poke == curpoke) res = min(res, e - s);
}
printf("%d",res);
}
//二分求解
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
int n;
map<char,int> mp;
map<char,int> gg;
char a[];
int zong;
bool check(int xx)
{
int zha=;
gg.clear();
for(int i=;i<=xx;i++)
{
if(gg[a[i]]==)
zha++;
gg[a[i]]++;
}
if(zha>=zong)
return true;
for(int i=xx+;i<=n;i++)
{
if(gg[a[i]]==)
zha++;
gg[a[i]]++;
gg[a[i-xx]]--;
if(gg[a[i-xx]]==)
zha--;
if(zha>=zong)
return true;
}
return false;
}
int main()
{
zong=;
scanf("%d",&n);
getchar();
mp.clear();
for(int i=;i<=n;i++)
{
scanf("%c",&a[i]);
if(mp[a[i]]==)
{
mp[a[i]]=;
zong++;
}
}
int l=zong,r=n,mid=;
while(l<r)
{
mid=(l+r)/;
if(check(mid))
r=mid;
else
l=mid+;
}
printf("%d\n",l);
return ;
}
Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)的更多相关文章
- Codeforces Round #364 (Div. 2)C. They Are Everywhere(尺取法)
题目链接: C. They Are Everywhere time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Round #364 (Div. 2)
这场是午夜场,发现学长们都睡了,改主意不打了,第二天起来打的virtual contest. A题 http://codeforces.com/problemset/problem/701/A 巨水无 ...
- Codeforces Round #364 (Div.2) D:As Fast As Possible(模拟+推公式)
题目链接:http://codeforces.com/contest/701/problem/D 题意: 给出n个学生和能载k个学生的车,速度分别为v1,v2,需要走一段旅程长为l,每个学生只能搭一次 ...
- 树形dp Codeforces Round #364 (Div. 1)B
http://codeforces.com/problemset/problem/700/B 题目大意:给你一棵树,给你k个树上的点对.找到k/2个点对,使它在树上的距离最远.问,最大距离是多少? 思 ...
- Codeforces Round #364 (Div. 2) B. Cells Not Under Attack
B. Cells Not Under Attack time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Codeforces Round #364 (Div. 2) Cells Not Under Attack
Cells Not Under Attack 题意: 给出n*n的地图,有给你m个坐标,是棋子,一个棋子可以把一行一列都攻击到,在根据下面的图,就可以看出让你求阴影(即没有被攻击)的方块个数 题解: ...
- Codeforces Round #364 (Div. 2) Cards
Cards 题意: 给你n个牌,n是偶数,要你把这些牌分给n/2个人,并且让每个人的牌加起来相等. 题解: 这题我做的时候,最先想到的是模拟,之后码了一会,发现有些麻烦,就想别的方法.之后发现只要把它 ...
- Codeforces Round #364 (Div. 2)->A. Cards
A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- Codeforces Round #364 (Div. 2) E. Connecting Universities
E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes input s ...
随机推荐
- C++:构造函数的重载
构造函数的重载的判断条件是:参数的个数或者类型不同,其他要保持一样 举例说明: 方式一:在类中声明重载构造函数,在类外定义 //第一种方法: #include<iostream> usin ...
- 在Android里完美实现基站和WIFI定位
来自:http://www.cnblogs.com/coffeegg/archive/2011/10/01/2197129.html 众所周知的,在OPhone和大部分国产的Android定制机里不支 ...
- Codeforces Round #204 (Div. 2) C
写了一记忆化 TLE了 把double换成long long就过了 double 这么耗时间啊 #include <iostream> #include<cstdio> #i ...
- EF4.0和EF5.0增删改查的写法区别及执行Sql的方法
EF4.0和EF5.0增删改查的写法区别 public T AddEntity(T entity) { //EF4.0的写法 添加实体 //db.CreateObjectSet<T>(). ...
- UVA 575 Skew Binary (水)
题意:根据这种进制的算法,例如,给你一个左式,要求推出右式.(其实右式就是一个十进制数,根据这种进位的方法来转成特殊进制的数.) 思路:观察转换特点,有点类似于二进制,但是其在后面还减一了.比如25- ...
- Java [Leetcode 144]Binary Tree Preorder Traversal
题目描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given bin ...
- jQuery避免$符和其他JS库冲突的方法对比
1.如果jquery库在第三方库之后引用.这个时候jquery库会占用$. 解决办法:剔除$符号的使用权. <script type="text/javascript" sr ...
- 【ASP.NET】编程点滴 :ASP.NET身份验证
ASP.NET实际开发中身份验证 是一个不可回避的问题.在相当一段长的时间内,由于不求甚解,我对这个话题似懂非懂.今天就对它做个简单的小结. Authentication and Authorizat ...
- 【WEB小工具】jQuery函数
jQuery-API帮助文档:Click here jQuery简介 jQuery是JavaScript框架,jQuery也是JavaScript代码.使用jQuery要比直接使用JavaScript ...
- HDU 5429 Geometric Progression
题意:给出一个大数数列,问是不是等比数列. 解法:拿java大数搞,注意全是0的情况也是Yes.我把公比用分数表示了,灰常麻烦,题解说只要判a[i - 1] * a[i + 1] == a[i] * ...