好多cf未总结,所以今天总结掉。
还有kmp算法的使用
D. Colored Boots
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There are nn left boots and nn right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings ll and rr, both of length nn. The character lili stands for the color of the ii-th left boot and the character riri stands for the color of the ii-th right boot.
A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.
For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are notcompatible: ('f', 'g') and ('a', 'z').
Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.
Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair.
Input
The first line contains nn (1≤n≤1500001≤n≤150000), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots).
The second line contains the string ll of length nn. It contains only lowercase Latin letters or question marks. The ii-th character stands for the color of the ii-th left boot.
The third line contains the string rr of length nn. It contains only lowercase Latin letters or question marks. The ii-th character stands for the color of the ii-th right boot.
Output
Print kk — the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors.
The following kk lines should contain pairs aj,bjaj,bj (1≤aj,bj≤n1≤aj,bj≤n). The jj-th of these lines should contain the index ajaj of the left boot in the jj-th pair and index bjbj of the right boot in the jj-th pair. All the numbers ajaj should be distinct (unique), all the numbers bjbj should be distinct (unique).
If there are many optimal answers, print any of them.
Examples
input
Copy
10
codeforces
dodivthree
output
Copy
5
7 8
4 9
2 2
9 10
3 1
input
Copy
7
abaca?b
zabbbcc
output
Copy
5
6 5
2 3
4 6
7 4
1 2
input
Copy
9
bambarbia
hellocode
output
Copy
0
input
Copy
10
code??????
??????test
output
Copy
10
6 2
1 6
7 3
3 5
4 8
9 7
5 1
2 4
10 9
8 10
#include<bits/stdc++.h>
using namespace std;
stack<int> l[256],r[256],a,b;
#define c(x,y) for(;!x.empty()&&!y.empty();) a.push(x.top()) ,b.push(y.top()),x.pop(),y.pop();
string s,t;
int n;int i;
int main(){
for(cin>>n>>s>>t;i<n;i++){
l[s[i]].push(i);
r[t[i]].push(i);
}
for(int i='a';i<='z';i++){
c(l[i],r[i]);
c(l[i],r['?']);
c(l['?'],r[i]);
}
c(l['?'],r['?']);
cout<<a.size()<<endl;
for(;!a.empty()&&!b.empty();){
cout<<a.top()+1<<" "<<b.top()+1<<endl;
a.pop();
b.pop();
}
return 0;
}
E. Superhero Battle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A superhero fights with a monster. The battle consists of rounds, each of which lasts exactly nn minutes. After a round ends, the next round starts immediately. This is repeated over and over again.
Each round has the same scenario. It is described by a sequence of nn numbers: d1,d2,…,dnd1,d2,…,dn (−106≤di≤106−106≤di≤106). The ii-th element means that monster's hp (hit points) changes by the value didi during the ii-th minute of each round. Formally, if before the ii-th minute of a round the monster's hp is hh, then after the ii-th minute it changes to h:=h+dih:=h+di.
The monster's initial hp is HH. It means that before the battle the monster has HH hit points. Print the first minute after which the monster dies. The monster dies if its hp is less than or equal to 00. Print -1 if the battle continues infinitely.
Input
The first line contains two integers HH and nn (1≤H≤10121≤H≤1012, 1≤n≤2⋅1051≤n≤2⋅105). The second line contains the sequence of integers d1,d2,…,dnd1,d2,…,dn (−106≤di≤106−106≤di≤106), where didi is the value to change monster's hp in the ii-th minute of a round.
Output
Print -1 if the superhero can't kill the monster and the battle will last infinitely. Otherwise, print the positive integer kk such that kk is the first minute after which the monster is dead.
Examples
input
Copy
1000 6
-100 -200 -300 125 77 -4
output
Copy
9
input
Copy
1000000000000 5
-1 0 0 0 0
output
Copy
4999999999996
input
Copy
10 4
-3 -6 5 4
output
Copy
-1
#include<bits/stdc++.h>
using namespace std;
long long a[200003];//注意数据范围
int main(){
int n;
long long h;//
cin>>h>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
a[i]=a[i]+a[i-1];
//cout<<a[i]<<"fe"<<endl;
}
for(int i=1;i<=n;i++){
if(h+a[i]<=0){
cout<<i<<endl;
return 0;
}
}
if(a[n]>=0) {
return cout<<-1<<endl,0;//lingyizhongbiaoshi
}
long long ans=h*n;
for(int i=1;i<=n;i++){
ans=min(ans,(h+a[i]-a[n]-1)/(-a[n])*n+i);
}
cout<<ans<<endl;
return 0;
}
emplace_back替代push_back
https://www.cnblogs.com/carsonzhu/p/5113213.html
F1. Same Sum Blocks (Easy)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
This problem is given in two editions, which differ exclusively in the constraints on the number nn.
You are given an array of integers a[1],a[2],…,a[n].a[1],a[2],…,a[n]. A block is a sequence of contiguous (consecutive) elements a[l],a[l+1],…,a[r]a[l],a[l+1],…,a[r] (1≤l≤r≤n1≤l≤r≤n). Thus, a block is defined by a pair of indices (l,r)(l,r).
Find a set of blocks (l1,r1),(l2,r2),…,(lk,rk)(l1,r1),(l2,r2),…,(lk,rk) such that:
- They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (li,ri)(li,ri) and (lj,rj(lj,rj) where i≠ji≠j either ri<ljri<lj or rj<lirj<li.
- For each block the sum of its elements is the same. Formally,
a[l1]+a[l1+1]+⋯+a[r1]=a[l2]+a[l2+1]+⋯+a[r2]=a[l1]+a[l1+1]+⋯+a[r1]=a[l2]+a[l2+1]+⋯+a[r2]=
⋯=⋯=
a[lk]+a[lk+1]+⋯+a[rk].a[lk]+a[lk+1]+⋯+a[rk].
- The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l′1,r′1),(l′2,r′2),…,(l′k′,r′k′)(l1′,r1′),(l2′,r2′),…,(lk′′,rk′′)satisfying the above two requirements with k′>kk′>k.
The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer nn (1≤n≤501≤n≤50) — the length of the given array. The second line contains the sequence of elements a[1],a[2],…,a[n]a[1],a[2],…,a[n] (−105≤ai≤105−105≤ai≤105).
Output
In the first line print the integer kk (1≤k≤n1≤k≤n). The following kk lines should contain blocks, one per line. In each line print a pair of indices li,rili,ri (1≤li≤ri≤n1≤li≤ri≤n) — the bounds of the ii-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
input
Copy
7
4 1 2 2 1 5 3
output
Copy
3
7 7
2 3
4 5
input
Copy
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
output
Copy
2
3 4
1 1
input
Copy
4
1 1 1 1
output
Copy
4
4 4
1 1
2 2
3 3
#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<int ,int >pii;
map<ll , vector<pii> > ma;
vector<pii> ::iterator it;
const int maxx=1e5+5;//
int a[maxx];
ll sum;int pos;
int main(){
int n;cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
sum=0;
for(int j=i;j>=1;j--){
sum+=a[j];
if(ma[sum].size()==0||ma[sum].back().second<j){
ma[sum].push_back(make_pair(j,i));
}
if(i==1) pos=sum;//
if(ma[sum].size()>ma[pos].size()) pos=sum;
}
}
cout<<ma[pos].size()<<endl;
for(it=ma[pos].begin();it!=ma[pos].end();it++){
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}
F2. Same Sum Blocks (Hard)
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
This problem is given in two editions, which differ exclusively in the constraints on the number nn.
You are given an array of integers a[1],a[2],…,a[n].a[1],a[2],…,a[n]. A block is a sequence of contiguous (consecutive) elements a[l],a[l+1],…,a[r]a[l],a[l+1],…,a[r] (1≤l≤r≤n1≤l≤r≤n). Thus, a block is defined by a pair of indices (l,r)(l,r).
Find a set of blocks (l1,r1),(l2,r2),…,(lk,rk)(l1,r1),(l2,r2),…,(lk,rk) such that:
- They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks (li,ri)(li,ri) and (lj,rj(lj,rj) where i≠ji≠j either ri<ljri<lj or rj<lirj<li.
- For each block the sum of its elements is the same. Formally,
a[l1]+a[l1+1]+⋯+a[r1]=a[l2]+a[l2+1]+⋯+a[r2]=a[l1]+a[l1+1]+⋯+a[r1]=a[l2]+a[l2+1]+⋯+a[r2]=
⋯=⋯=
a[lk]+a[lk+1]+⋯+a[rk].a[lk]+a[lk+1]+⋯+a[rk].
- The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks (l′1,r′1),(l′2,r′2),…,(l′k′,r′k′)(l1′,r1′),(l2′,r2′),…,(lk′′,rk′′)satisfying the above two requirements with k′>kk′>k.
The picture corresponds to the first example. Blue boxes illustrate blocks.
Write a program to find such a set of blocks.
Input
The first line contains integer nn (1≤n≤15001≤n≤1500) — the length of the given array. The second line contains the sequence of elements a[1],a[2],…,a[n]a[1],a[2],…,a[n] (−105≤ai≤105−105≤ai≤105).
Output
In the first line print the integer kk (1≤k≤n1≤k≤n). The following kk lines should contain blocks, one per line. In each line print a pair of indices li,rili,ri (1≤li≤ri≤n1≤li≤ri≤n) — the bounds of the ii-th block. You can print blocks in any order. If there are multiple answers, print any of them.
Examples
input
Copy
7
4 1 2 2 1 5 3
output
Copy
3
7 7
2 3
4 5
input
Copy
11
-5 -4 -3 -2 -1 0 1 2 3 4 5
output
Copy
2
3 4
1 1
input
Copy
4
1 1 1 1
output
Copy
4
4 4
1 1
2 2
3 3
n变大了
一样的答案
G. Privatization of Roads in Treeland
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Treeland consists of nn cities and n−1n−1 roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right — the country's topology is an undirected tree.
There are some private road companies in Treeland. The government decided to sell roads to the companies. Each road will belong to one company and a company can own multiple roads.
The government is afraid to look unfair. They think that people in a city can consider them unfair if there is one company which owns two or more roads entering the city. The government wants to make such privatization that the number of such cities doesn't exceed kk and the number of companies taking part in the privatization is minimal.
Choose the number of companies rr such that it is possible to assign each road to one company in such a way that the number of cities that have two or more roads of one company is at most kk. In other words, if for a city all the roads belong to the different companies then the city is good. Your task is to find the minimal rr that there is such assignment to companies from 11 to rr that the number of cities which are not good doesn't exceed kk.
The picture illustrates the first example (n=6,k=2n=6,k=2). The answer contains r=2r=2 companies. Numbers on the edges denote edge indices. Edge colors mean companies: red corresponds to the first company, blue corresponds to the second company. The gray vertex (number 33) is not good. The number of such vertices (just one) doesn't exceed k=2k=2. It is impossible to have at most k=2k=2 not good cities in case of one company.
Input
The first line contains two integers nn and kk (2≤n≤200000,0≤k≤n−12≤n≤200000,0≤k≤n−1) — the number of cities and the maximal number of cities which can have two or more roads belonging to one company.
The following n−1n−1 lines contain roads, one road per line. Each line contains a pair of integers xixi, yiyi (1≤xi,yi≤n1≤xi,yi≤n), where xixi, yiyi are cities connected with the ii-th road.
Output
In the first line print the required rr (1≤r≤n−11≤r≤n−1). In the second line print n−1n−1 numbers c1,c2,…,cn−1c1,c2,…,cn−1 (1≤ci≤r1≤ci≤r), where cici is the company to own the ii-th road. If there are multiple answers, print any of them.
Examples
input
Copy
6 2
1 4
4 3
3 5
3 6
5 2
output
Copy
2
1 2 1 1 2
input
Copy
4 2
3 1
1 4
1 2
output
Copy
1
1 1 1
input
Copy
10 2
10 3
1 2
1 3
1 4
2 5
2 6
2 7
3 8
3 9
output
Copy
3
1 1 2 3 2 3 1 3 1
G. Privatization of Roads in Treeland
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Treeland consists of nn cities and n−1n−1 roads. Each road is bidirectional and connects two distinct cities. From any city you can get to any other city by roads. Yes, you are right — the country's topology is an undirected tree.
There are some private road companies in Treeland. The government decided to sell roads to the companies. Each road will belong to one company and a company can own multiple roads.
The government is afraid to look unfair. They think that people in a city can consider them unfair if there is one company which owns two or more roads entering the city. The government wants to make such privatization that the number of such cities doesn't exceed kk and the number of companies taking part in the privatization is minimal.
Choose the number of companies rr such that it is possible to assign each road to one company in such a way that the number of cities that have two or more roads of one company is at most kk. In other words, if for a city all the roads belong to the different companies then the city is good. Your task is to find the minimal rr that there is such assignment to companies from 11 to rr that the number of cities which are not good doesn't exceed kk.
The picture illustrates the first example (n=6,k=2n=6,k=2). The answer contains r=2r=2 companies. Numbers on the edges denote edge indices. Edge colors mean companies: red corresponds to the first company, blue corresponds to the second company. The gray vertex (number 33) is not good. The number of such vertices (just one) doesn't exceed k=2k=2. It is impossible to have at most k=2k=2 not good cities in case of one company.
Input
The first line contains two integers nn and kk (2≤n≤200000,0≤k≤n−12≤n≤200000,0≤k≤n−1) — the number of cities and the maximal number of cities which can have two or more roads belonging to one company.
The following n−1n−1 lines contain roads, one road per line. Each line contains a pair of integers xixi, yiyi (1≤xi,yi≤n1≤xi,yi≤n), where xixi, yiyi are cities connected with the ii-th road.
Output
In the first line print the required rr (1≤r≤n−11≤r≤n−1). In the second line print n−1n−1 numbers c1,c2,…,cn−1c1,c2,…,cn−1 (1≤ci≤r1≤ci≤r), where cici is the company to own the ii-th road. If there are multiple answers, print any of them.
Examples
input
Copy
6 2
1 4
4 3
3 5
3 6
5 2
output
Copy
2
1 2 1 1 2
input
Copy
4 2
3 1
1 4
1 2
output
Copy
1
1 1 1
input
Copy
10 2
10 3
1 2
1 3
1 4
2 5
2 6
2 7
3 8
3 9
output
Copy
3
1 1 2 3 2 3 1 3 1
#include<bits/stdc++.h>
#define N 200005
#define pb push_back
//typedef pair<node,int> pii;
using namespace std;
int n,k,u,v,in[N],c[N],m,i;
struct node{
int e,v;
node(int e,int v){
e=e; v=v;
}
};
vector<node>g[N];
bool cmp(int x,int y){
return x>y;
}
void dfs(int u,int fa,int cl){
for(int i=0;i<g[u].size();i++){
int v=g[u][i].v,id=g[u][i].e;if(v==fa)continue;
if(cl>m)cl-=m;
c[id]=cl++;
dfs(v,u,cl);
}
}
int main(){
cin>>n>>k;
for(i=1;i<=n-1;i++){
scanf("%d%d",&u,&v);
in[u]++;in[v]++;
g[u].pb(node(i,v));
g[v].pb(node(i,u));
}
sort(in+1,in+n+1,cmp);
m=in[k+1];
dfs(1,0,1);
printf("%d\n",m);
for(i=1;i<=n-1;i++){
printf("%d ",c[i]);
}
return 0;
}
C. Polycarp Restores Permutation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
An array of integers p1,p2,…,pnp1,p2,…,pn is called a permutation if it contains each number from 11 to nn exactly once. For example, the following arrays are permutations: [3,1,2][3,1,2], [1][1], [1,2,3,4,5][1,2,3,4,5] and [4,3,1,2][4,3,1,2]. The following arrays are not permutations: [2][2], [1,1][1,1], [2,3,4][2,3,4].
Polycarp invented a really cool permutation p1,p2,…,pnp1,p2,…,pn of length nn. It is very disappointing, but he forgot this permutation. He only remembers the array q1,q2,…,qn−1q1,q2,…,qn−1 of length n−1n−1, where qi=pi+1−piqi=pi+1−pi.
Given nn and q=q1,q2,…,qn−1q=q1,q2,…,qn−1, help Polycarp restore the invented permutation.
Input
The first line contains the integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the length of the permutation to restore. The second line contains n−1n−1 integers q1,q2,…,qn−1q1,q2,…,qn−1 (−n<qi<n−n<qi<n).
Output
Print the integer -1 if there is no such permutation of length nn which corresponds to the given array qq. Otherwise, if it exists, print p1,p2,…,pnp1,p2,…,pn. Print any such permutation if there are many of them.
Examples
input
Copy
3
-2 1
output
Copy
3 1 2
input
Copy
5
1 1 1 1
output
Copy
1 2 3 4 5
input
Copy
4
-1 2 2
output
Copy
-1
#include <bits/stdc++.h>
using namespace std;
int64_t n,m,mx,i,x,p[222000];
set<int64_t> s;
int main(){
for(cin>>n,s.insert(0),i=1;i<n;i++)
cin>>x,p[i]=p[i-1]+x,m=min(m,p[i]),mx=max(mx,p[i]),s.insert(p[i]);
if(mx-m!=n-1||s.size()<n)cout<<-1;
else for(i=0;i<n;i++)cout<<p[i]+1-m<<" ";
}//很短的代码啊
要尽量精简,下一次写题号吧,题干太长了,没必要粘贴过来。
首先读题目要有着重点,再以示例检验理解的正确性,或者为了追求速度相反去做,除此之外,就是注意数据范围。
其次是做题目尽量考虑所有情况,不要有侥幸心理
再是做的时候要尽量化简,学会用一些经典的STL库
主要涉及到贪心和数据结构,
还有周期的计算之类的,水题
再是数据结构的map的使用
还有最后一道题要看懂,根据不成圈的特征,不会返回,同时反正特征很重要了
kmp算法
https://blog.csdn.net/u013071074/article/details/38294597
用了一个晚课时间看的吧。
讲的很详细了,只不过两个拓展算法,云里雾里的
让我自己写都可以写出来了。
后面是奇怪的东西,略。