http://codeforces.com/contest/828

哇这是我打的第一场cf,第一题都wa了无数次,然后第二题差几分钟交 ,第二天一交就AC了内心是崩溃的。果然我还是太菜l....

A. Restaurant Tables
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

The first line contains three integers na 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.

Output

Print the total number of people the restaurant denies service to.

Examples
input
4 1 2
1 2 1 1
output
0
input
4 1 1
1 1 2 1
output
2
Note

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 ;
}
B. Black Square
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input
5 4
WWWW
WWWB
WWWB
WWBB
WWWW
output
5
input
1 2
BB
output
-1
input
3 3
WWW
WWW
WWW
output
1
Note

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); }
C. String Reconstruction
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print lexicographically minimal string that fits all the information Ivan remembers.

Examples
input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
output
abacaba
input
1
a 1 3
output
aaa
input
3
ab 1 1
aba 1 3
ab 2 3 5
output
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);
}
D. High Load
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input
3 2
output
2
1 2
2 3
input
5 3
output
3
1 2
2 3
3 4
3 5
Note

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 ;
}
E. DNA Evolution
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

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!

Input

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 lr 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.
Output

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.

Examples
input
ATGCATGC
4
2 1 8 ATGC
2 2 6 TTT
1 4 T
2 2 6 TA
output
8
2
4
input
GAGTTGTTAA
6
2 3 4 TATGGTG
1 1 T
1 6 G
2 5 9 AGTAATA
1 10 G
2 2 6 TTGT
output
0
3
1
Note

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 ; }
上一篇:HTML学习笔记Day15


下一篇:如何解决jQuery Validation针对动态添加的表单无法工作的问题?