http://codeforces.com/contest/828
哇这是我打的第一场cf,第一题都wa了无数次,然后第二题差几分钟交 ,第二天一交就AC了内心是崩溃的。果然我还是太菜l....
1 second
256 megabytes
standard input
standard output
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Print the total number of people the restaurant denies service to.
4 1 2
1 2 1 1
0
4 1 1
1 1 2 1
2
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
题解:直接模拟,当时傻了b--就直接a++,wa了一年,废话太多我。。。上代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=2e5+;
int da[maxn],n,a,b;
int main()
{
int ans=;int c=;
scanf("%d %d %d",&n,&a,&b);
for(int i=;i<n;i++)
{
scanf("%d",&da[i]);
}
for(int i=;i<n;i++)
{
bool flag=false;
if(da[i]==)
{
if(a>)
{
a--;
}
else if(b>)
{
b--;c++;
}
else if(c>)
{
c--;
}
else
{
ans++;
}
}
else
{
if(b>)
{
b--;
}
else
{
ans+=;
} }
}
cout<<ans<<endl;
return ;
}
1 second
256 megabytes
standard input
standard output
Polycarp has a checkered sheet of paper of size n × m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.
You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length.
The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the sizes of the sheet.
The next n lines contain m letters 'B' or 'W' each — the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white.
Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1.
5 4
WWWW
WWWB
WWWB
WWBB
WWWW
5
1 2
BB
-1
3 3
WWW
WWW
WWW
1
In the first example it is needed to paint 5 cells — (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2).
In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square.
In the third example all cells are colored white, so it's sufficient to color any cell black.
题意:给一个n*m的B,W的图,求画一黑正方形需要的最少墨水。如果不能画输出-1.
题解:找到最下,左,右,上,的点。从而取长的作为边长。以及还有几种特殊情况。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=+;
char map[][];
bool vis[][];
int di[][]={-,,,-,,,,};
int n,m,lx=,rx=-,uy=,dy=-;
struct node
{
int x,y;
};
node a1,a2;
int main()
{
scanf("%d %d",&n,&m);
getchar();
for(int i=;i<=n;i++)
{
gets(map[i]+);
}
memset(vis,false,sizeof(vis));
int tmp1=,tmp2=;bool flag=false;
a1.x=a1.y=;
a2.x=a2.y=-;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(map[i][j]=='B')
{
// cout<<i<<" "<<j<<endl;
flag=true;
lx=min(lx,j); rx=max(rx,j);
uy=min(uy,i);//cout<<uy<<endl;
dy=max(dy,i);
/* if(i<a1.x||j<a1.y)
{
a1.x=j;a1.y=i;
}
if(i>a2.x||j>a2.y)
{
a2.x=j;a2.y=i;
} */
tmp2++;
} }
}
//cout<<lx<<" "<<rx<<" "<<uy<<" "<<dy<<" "<<a1.x<<" "<<a1.y<<" "<<a2.x<<" "<<a2.y<<endl;
//cout<<tmp2<<endl;
if(!flag)
{
printf("1\n");return ;
}
int l=max(rx-lx+,dy-uy+);
if(l>n||l>m)
{
printf("-1\n");return ;
}
//cout<<l<<endl;
printf("%d\n",l*l-tmp2); }
2 seconds
256 megabytes
standard input
standard output
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string sconsist of small English letters only.
The first line contains single integer n (1 ≤ n ≤ 105) — the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order — positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed106, 1 ≤ xi, j ≤ 106, 1 ≤ ki ≤ 106, and the sum of all ki doesn't exceed 106. The strings ti can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
Print lexicographically minimal string that fits all the information Ivan remembers.
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
abacaba
1
a 1 3
aaa
3
ab 1 1
aba 1 3
ab 2 3 5
ababab
题意:给n个字符串和位置求最小字典序的的字符串。
题解:直接模拟的话肯定会超时,用结构体储存字符串的位置然后排序一能免去重复的赋值。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
using namespace std;
const int maxn=2e6+;
struct p
{
int k,l,r;
}tmp;
bool cmp(const p &a,const p &b)
{
if(a.l==b.l)
return a.r>b.r;
else
return a.l<b.l;
}
/*bool cmp(p a,p b) {
if (a.l == b.l) return a.r > b.r;
return a.l < b.l;
}*/
vector<p>a;
string b;
vector<string>base;
int t,k,maxl,n;
int main()
{
std::ios::sync_with_stdio(false);
// scanf("%d",&n);
cin>>n;
// getchar();
while(n--)
{
cin>>b>>t;
base.push_back(b);
//scanf("%d",&t);
for(int i=;i<=t;i++)
{
//scanf("%d",&k);
cin>>k;
tmp=(p){(int) base.size()-,k,k+(int)b.size()-};
a.push_back(tmp);
}
}
sort(a.begin(),a.end(),cmp);
b="";
k=;
for(int i=;i<(int)a.size();i++)
{
if(a[i].r<k)continue;
while(k<a[i].l) {b+='a';k++;}
for(int j=a[i].l;k<=a[i].r;k++)
{
b+=base[a[i].k][k-a[i].l];// cout<<"1"<<endl;
}
}
cout<<b<<endl;
//printf("%s\n",a+1);
}
2 seconds
512 megabytes
standard input
standard output
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of n nodes connected with minimum possible number of wires into one network (a wire directly connects two nodes). Exactly k of the nodes should be exit-nodes, that means that each of them should be connected to exactly one other node of the network, while all other nodes should be connected to at least two nodes in order to increase the system stability.
Arkady wants to make the system as fast as possible, so he wants to minimize the maximum distance between two exit-nodes. The distance between two nodes is the number of wires a package needs to go through between those two nodes.
Help Arkady to find such a way to build the network that the distance between the two most distant exit-nodes is as small as possible.
The first line contains two integers n and k (3 ≤ n ≤ 2·105, 2 ≤ k ≤ n - 1) — the total number of nodes and the number of exit-nodes.
Note that it is always possible to build at least one network with n nodes and k exit-nodes within the given constraints.
In the first line print the minimum possible distance between the two most distant exit-nodes. In each of the next n - 1 lines print two integers: the ids of the nodes connected by a wire. The description of each wire should be printed exactly once. You can print wires and wires' ends in arbitrary order. The nodes should be numbered from 1 to n. Exit-nodes can have any ids.
If there are multiple answers, print any of them.
3 2
2
1 2
2 3
5 3
3
1 2
2 3
3 4
3 5
In the first example the only network is shown on the left picture.
In the second example one of optimal networks is shown on the right picture.
Exit-nodes are highlighted.
题意:求n个点的链接方式有k个端点,要使得最长的的路最短。
题解:从一个点往k个方向平均发散,这样能使最长的路最短。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
using namespace std;
int n,k;
int main()
{
scanf("%d %d",&n,&k);
int tmp=(n-)%k;
int d=(n-)/k;
if(tmp==)
{
printf("%d\n",(n-)/k*+-);
}
else if(tmp>=)
{
printf("%d\n",(n-)/k*+-);
}
else if(tmp==)
{
printf("%d\n",(n-)/k*+-);
}
int px=;
for(int i=;i<=k;i++)
{
if(tmp>)
{
for(int j=;j<=d+;j++)
{
if(j==)
printf("1 %d\n",px);
else
printf("%d %d\n",px-,px);
px++;
}
tmp--;
}
else
{
for(int j=;j<=d;j++)
{
if(j==)
printf("1 %d\n",px);
else
printf("%d %d\n",px-,px);
px++;
}
}
}
return ;
}
2 seconds
512 megabytes
standard input
standard output
Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A", "T", "G", "C". A DNA strand is a sequence of nucleotides. Scientists decided to track evolution of a rare species, which DNA strand was string s initially.
Evolution of the species is described as a sequence of changes in the DNA. Every change is a change of some nucleotide, for example, the following change can happen in DNA strand "AAGC": the second nucleotide can change to "T" so that the resulting DNA strand is "ATGC".
Scientists know that some segments of the DNA strand can be affected by some unknown infections. They can represent an infection as a sequence of nucleotides. Scientists are interested if there are any changes caused by some infections. Thus they sometimes want to know the value of impact of some infection to some segment of the DNA. This value is computed as follows:
- Let the infection be represented as a string e, and let scientists be interested in DNA strand segment starting from position l to position r, inclusive.
- Prefix of the string eee... (i.e. the string that consists of infinitely many repeats of string e) is written under the string s from position lto position r, inclusive.
- The value of impact is the number of positions where letter of string s coincided with the letter written under it.
Being a developer, Innokenty is interested in bioinformatics also, so the scientists asked him for help. Innokenty is busy preparing VK Cup, so he decided to delegate the problem to the competitors. Help the scientists!
The first line contains the string s (1 ≤ |s| ≤ 105) that describes the initial DNA strand. It consists only of capital English letters "A", "T", "G" and "C".
The next line contains single integer q (1 ≤ q ≤ 105) — the number of events.
After that, q lines follow, each describes one event. Each of the lines has one of two formats:
- 1 x c, where x is an integer (1 ≤ x ≤ |s|), and c is a letter "A", "T", "G" or "C", which means that there is a change in the DNA: the nucleotide at position x is now c.
- 2 l r e, where l, r are integers (1 ≤ l ≤ r ≤ |s|), and e is a string of letters "A", "T", "G" and "C" (1 ≤ |e| ≤ 10), which means that scientists are interested in the value of impact of infection e to the segment of DNA strand from position l to position r, inclusive.
For each scientists' query (second type query) print a single integer in a new line — the value of impact of the infection on the DNA.
ATGCATGC
4
2 1 8 ATGC
2 2 6 TTT
1 4 T
2 2 6 TA
8
2
4
GAGTTGTTAA
6
2 3 4 TATGGTG
1 1 T
1 6 G
2 5 9 AGTAATA
1 10 G
2 2 6 TTGT
0
3
1
Consider the first example. In the first query of second type all characters coincide, so the answer is 8. In the second query we compare string "TTTTT..." and the substring "TGCAT". There are two matches. In the third query, after the DNA change, we compare string "TATAT..."' with substring "TGTAT". There are 4 matches.
题意:给一个字符串,然后可以对字符串可修改,问l~r字符匹配的次数。直接模拟的话会超时。这题用到树状数组,本菜鸡还不会,在网上找半天看树状数组,还是不太懂。只好找大佬问了。
题解:
因为|e|不大于10,所以字符串每一个字符在树状数组上,对于长度(1~10),长度取模后(0~9),字符类型(1~4),位置(1~n)动态维护贡献,然后利用树状数组前缀和性质,求出[l,r]关于e在指定的树状数组中找的每一个字符的贡献总和
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1e5+;
int f[][][][maxn];
int n;
char s[maxn];
char c[maxn];
int lowbit(int x)
{
return x&(-x);
}
int ch(char s)
{
if(s=='A')return ;
else if(s=='T')return ;
else if(s=='G')return ;
else if(s=='C')return ;
return -;
}
void Add(int *tr,int x,int d)
{
for(;x<=n;x+=lowbit(x))
{
tr[x]+=d;
}
}
int Sum(int *tr,int x)
{
if(x==)return ;
int s=;
while(x)
{
s+=tr[x];
x-=lowbit(x);
}
return s;
}
int main()
{
scanf("%s",s+);
n=strlen(s+);
for(int i=;i<=n;i++)
for(int j=;j<=;j++)
Add(f[j][i%j][ch(s[i])] , i , ); int q,x,d,l,r;
scanf("%d",&q);
while(q--)
{
scanf("%d",&x);
if(x==)
{
scanf("%d%s",&d,c);
for(int i=;i<=;i++)
{
Add(f[i][d%i][ch(s[d])],d,-);
}
s[d]=c[];
for(int i=;i<=;i++)
Add(f[i][d%i][ch(s[d])],d,);
}
else
{
scanf("%d %d %s",&l,&r,c);
int len=strlen(c);
int ans=;
for(int i=;i<len;i++)
{
ans+=Sum(f[len][(l+i)%len][ch(c[i])],r)-Sum(f[len][(l+i)%len][ch(c[i])],l-);
}
// cout<<ans<<endl;
printf("%d\n",ans);
}
}
return ; }
http://codeforces.com/contest/828的更多相关文章
- codeforces 725D . Contest Balloons(贪心+优先队列)
题目链接:codeforces 725D . Contest Balloons 先按气球数从大到小排序求出初始名次,并把名次排在第一队前面的队放入优先队列,按w-t-1值从小到大优先,然后依次给气球给 ...
- codeforces.com/contest/325/problem/B
http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...
- http://codeforces.com/contest/349
A. Cinema Line time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- CodeForces - 725D Contest Balloons 贪心
D. Contest Balloons time limit per test 3 seconds memory limit per test 2 ...
- http://codeforces.com/contest/555/problem/B
比赛时虽然贪了心,不过后面没想到怎么处理和set的排序方法忘了- -,其实是和优先队列的仿函数一样的... 比赛后用set pair过了... #include <bits/stdc++.h&g ...
- http://codeforces.com/contest/845
A. Chess Tourney time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- http://codeforces.com/contest/610/problem/D
D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- http://codeforces.com/contest/612/problem/D
D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- http://codeforces.com/contest/536/problem/B
B. Tavas and Malekas time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- Day3 - Linux系统安装_Centos6.9
第1章 虚拟机安装 1.1 镜像下载 1.1.1 新版本下载 http://mirrors.aliyun.com #阿里云官方镜像站点 1.1.2 旧版本下载 http://vault.cento ...
- [WPF]使用WindowChrome自定义Window Style
1. 前言 做了WPF开发多年,一直未曾自己实现一个自定义Window Style,无论是<WPF编程宝典>或是各种博客都建议使用WindowStyle="None" ...
- 栈的实现Java
package practice; import java.util.Iterator; //栈 public class MyStack<T> implements Iterable&l ...
- openssl命令
author:JevonWei 版权声明:原创作品 1.构建根证书 构建根证书前,需要构建随机数文件(.rand),完整命令如 openssl rand -out private/.rand 1000 ...
- oracle得到日期对应的星期
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp56 select to_char(sysdate,'ww') fro ...
- ospf剩余笔记
OSPF 流程图: 带宽 开销 10 100 100 19 1000 4 10000 2 区域的划分减少lsdb的大小 有利于网络管理员故障排除 网络故障不会影响到其他区域 邻接关系 ...
- 团队作业8——第二次项目冲刺(Beta阶段)博客汇总
一.冲刺计划安排 http://www.cnblogs.com/teamworkers/p/6875742.html 二.七天冲刺汇总 http://www.cnblogs.com/teamworke ...
- 201521123004《Java程序设计》第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 答:主要以泛型为主 //简单的泛型类的定义,T为类型参数 public ...
- ArrayList和LinkedList区别及性能测试
ArrayList和LinkedList是Java Lis接口的2个实现.它们的区别如下表所示: 底层结构 强项 弱项 ArrayList 数组 随机访问get和set 插入删除 LinkedList ...
- 使用 Python & Flask 实现 RESTful Web API
环境安装: sudo pip install flask Flask 是一个Python的微服务的框架,基于Werkzeug, 一个 WSGI 类库. Flask 优点: Written in Pyt ...