A Sereja and Algorithm

题意:给定有x,y,z组成的字符串,每次询问某一段s[l, r]能否变成变成zyxzyx的循环体。

分析:

分析每一段x,y,z数目是否满足构成循环体,当然长度<3的要特判。

代码:

 #include <bits/stdc++.h>
#define in freopen("solve_in.txt", "r", stdin);
#define pb push_back using namespace std;
typedef long long LL; const int maxn = (int)1e5 + ;
char s[maxn];
int cnt[maxn][];
int main() { int m;
scanf("%s", s+);
scanf("%d", &m);
int len = strlen(s+);
for(int i = ; i <= len; i++) {
for(int k = ; k < ; k++)
cnt[i][k] = cnt[i-][k];
cnt[i][s[i]-'x']++;
}
for(int i = ; i < m; i++) {
int l, r;
scanf("%d%d", &l, &r);
int a = cnt[r][]-cnt[l-][];
int b = cnt[r][]-cnt[l-][];
int c = cnt[r][]-cnt[l-][];
int tmp = r-l+;
if(tmp < )
puts("YES");
else { if(tmp% == ) {
if(a == b && b == c)
puts("YES");
else puts("NO");
} else if(tmp% == ) {
if((a == b && c - b == ) || (b == c && a-b == ) || (a == c &&b -a == ))
puts("YES");
else puts("NO");
} else {
if((a == b && a-c == ) || (b == c && b-a==) || (a == c && a-b == ))
puts("YES");
else puts("NO");
}
}
}
return ;
}

B Sereja ans Anagrams

题意:给定2个数组,a[1...n],以及b[1...m]问能否从a中等间隔的选取m个数,使得这m个数与b中各个整数数目对应相等。

分析:

容易知道由于是等间隔p的出现,所以可以将a[1..n]的数按间隔分成p组,每次添加一个数时,看相应的组m个数形成的序列是否和b中数的数目对应相等,每次添加,减少一个数,最多改变2个数的数目,看是不是所有的数的数目都满足与在b中的相等。感觉和单调队列类似。

代码:

 #include <bits/stdc++.h>
#define in freopen("solve_in.txt", "r", stdin);
#define pb push_back using namespace std;
typedef long long LL;
typedef map<int, int> MPII; const int maxn = *(int)1e5;
int n, m, p;
MPII mps, mx[maxn];
vector<int> ans;
int a[maxn], b[maxn];
int sat[maxn];
queue<int> q[maxn]; int main() { scanf("%d%d%d", &n, &m, &p);
for(int i = ; i < n; i++) {
scanf("%d", a+i);
}
int dif = ;
for(int i = ; i < m; i++) {
int t;
scanf("%d", &t);
if(mps[t] == )
dif++;
mps[t]++;
}
for(int i = ; i < n; i++) {
int di = i%p;
if(q[di].size() >= m) {
int u = q[di].front();
q[di].pop();
if(mx[di][u] == mps[u])
sat[di]--;
else if(mx[di][u] == mps[u]+)
sat[di]++;
mx[di][u]--;
}
int u = a[i];
q[di].push(a[i]);
if(mx[di][u] == mps[u])
sat[di]--;
else if(mx[di][u] == mps[u]-)
sat[di]++;
mx[di][u]++;
if(sat[di] == dif)
ans.pb(i-(m-)*p);
}
cout<<ans.size()<<endl;
for(int i = ; i < ans.size(); i++) {
printf("%d%c", ans[i]+, i == ans.size()-?'\n':' ');
}
return ;
}

C Sereja and the Arrangement of Numbers

题意:n个位置,从m不同的数中选一些数去填充,构成一个数组,数组中出现的任何两个数一定要在数组中至少连续出现一次,每个数都有一个代价,要求选出的数总代价最大!

分析:首先肯定的是选出代价尽量大的数,那么就要确定n个位置最多放多少个不同的数,做的时候其实是想到了完全图的,但是没继续想下去。由于任何两个数都要相邻出现,所以肯定是满足完全图关系的,但是如果将相邻关系看做一条边的话,n个位置n-1条边,而且这n-1条边是能够从左走到右,也就是构成一个半欧拉图。

半欧拉图:最多有两个点度数为奇数。对于偶数个节点完全图,每个点度数为奇数,添加一条边能够使得两个点度数变为偶数,所以最少需要添加(点数/2-1)条边构成半欧拉图,因为最终还是允许两个点度数为奇数。奇数个结点完全图,度数为偶数,所以不需要添加边了。

这样根据,图中n-1条边二分最多能放置的结点数,得到数组中最多出现多少个不同的数,贪心选取最大就行了。

代码:

 #include <bits/stdc++.h>
#define in freopen("solve_in.txt", "r", stdin);
#define pb push_back using namespace std;
typedef long long LL;
typedef map<int, int> MPII;
const int maxn = (int)1e5 + ; int w[maxn];
int getAns(int lim, int r){
int l = ;
while(l < r){
int mid((l+r+)>>);
int tmp;
if(mid&){
tmp = ((LL)mid*(mid-)>>);
if(tmp <= lim)
l = mid;
else r = mid-;
}
else {
tmp = ((LL)mid*(mid-)/+mid/-);
if(tmp <= lim)
l = mid;
else r = mid-;
}
}
return l;
}
int main(){ int n, m;
scanf("%d%d", &n, &m);
for(int i(); i != m; i++){
scanf("%*d%d", w+i);
}
sort(w, w+m, greater<int> ());
n = getAns(n-, m);
LL ans = ;
for(int i = ; i != n; i++)
ans += w[i];
cout<<ans<<endl;
return ;
}

Codeforces Round #215 (Div. 1)的更多相关文章

  1. Codeforces Round #215 (Div. 2) B. Sereja and Suffixes map

    B. Sereja and Suffixes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  2. Codeforces Round #215 (Div. 1) B. Sereja ans Anagrams 匹配

    B. Sereja ans Anagrams Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  3. Codeforces Round #215 (Div. 2) D. Sereja ans Anagrams

    http://codeforces.com/contest/368/problem/D 题意:有a.b两个数组,a数组有n个数,b数组有m个数,现在给出一个p,要你找出所有的位置q,使得位置q  q+ ...

  4. Codeforces Round #215 (Div. 2) C. Sereja and Algorithm

    #include <iostream> #include <vector> #include <algorithm> #include <string> ...

  5. Codeforces Round #215 (Div. 2) B. Sereja and Suffixes

    #include <iostream> #include <vector> #include <algorithm> #include <set> us ...

  6. Codeforces Round #215 (Div. 2) A. Sereja and Coat Rack

    #include <iostream> #include <vector> #include <algorithm> using namespace std; in ...

  7. Codeforces Round #215 (Div. 1) B

    出来冒个泡 由于数比较大  开了map计数  然后边走边删边加 勉强可过 #include <iostream> #include<cstdio> #include<cs ...

  8. Codeforces Round #215 (Div. 2) D题(离散化+hash)

    D. Sereja ans Anagrams time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

随机推荐

  1. 【算法】A*改进算法

    目的:我这里希望实现一个java A* 游戏里的战斗寻径 定义部分: 这个定义引用自 http://www.cnblogs.com/kanego/archive/2011/08/30/2159070. ...

  2. C#学习笔记3:提示“截断字符串或二进制数据”错误解决方法

    1.调试程序如出现“截断字符串或二进制数据”的关于数据库的错误,可以先试一试修改数据库中字符定义的长度. 2.使用ManualResetEvent前需导入 命名空间System.Threading; ...

  3. linux process 相关命令

    1.显示指定用户信息:ps -u root 2.显示所有进程信息,连同命令行:ps -ef 3. ps 与grep 常用组合用法,查找特定进程:ps -ef|grep ssh 4. 把所有进程显示出来 ...

  4. javascript异步执行函数导致的变量变化问题解决思路

    for(var i=0;i<3;i++) { setTimeout(function(){ console.log(i) },0); }控制台输出:333 这是因为执行方法的时候for循环已经执 ...

  5. 06_init()和destroy()方法

    [工程截图] [HelloWorld.java] package com.HigginCui; public class HelloWorld { public HelloWorld(){ Syste ...

  6. 为UITextView添加与UITextField一样的边框——UITextField默认边框颜色、宽度、圆角

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3789052.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  7. 九度OJ 城际公路网 -- 图论

    题目地址:http://ac.jobdu.com/problem.php?pid=1343 题目描述: 为了加快城市之间的通行和物资流动速度,A国政府决定在其境内的N个大中型城市之间,增加修建K条公路 ...

  8. 策略模式(Strategey Pattern)

    策略模式:定义了算法族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户. 下面举个例子: 有两个具体策略,分别执行两个整型加法和减法. interface Strateg ...

  9. JQuery中的动画

    一.show()方法和hide()方法 这两种方法是jQuery动画的最基本方法.当为元素调用show方法时相当于将该元素的display样式改为block或者inline,同理,如果当元素调用hid ...

  10. Winform窗口弹出位置控制

    窗体的弹出位置可以由属性StartPosition来指定,默认值有: Manural 自定义,由属性Location指定: CenterScreen 屏幕中央: WindowsDefaultBound ...