A. Detective Book
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Ivan recently bought a detective book. The book is so interesting that each page of this book introduces some sort of a mystery, which will be explained later. The ii-th page contains some mystery that will be explained on page aiai (ai≥iai≥i).
Ivan wants to read the whole book. Each day, he reads the first page he didn't read earlier, and continues to read the following pages one by one, until all the mysteries he read about are explained and clear to him (Ivan stops if there does not exist any page ii such that Ivan already has read it, but hasn't read page aiai). After that, he closes the book and continues to read it on the following day from the next page.
How many days will it take to read the whole book?
Input
The first line contains single integer nn (1≤n≤1041≤n≤104) — the number of pages in the book.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (i≤ai≤ni≤ai≤n), where aiai is the number of page which contains the explanation of the mystery on page ii.
Output
Print one integer — the number of days it will take to read the whole book.
Example
input
Copy
9 1 3 3 6 7 6 8 8 9
output
Copy
4
Note
Explanation of the example test:
During the first day Ivan will read only the first page. During the second day Ivan will read pages number 22 and 33. During the third day — pages 44-88. During the fourth (and the last) day Ivan will read remaining page number 99.
刚开始这题看错题意了,我以为是3,3,6就要跑到下一个6就可结束。
cf的题真的很难懂,看半天
然后才发现一个当前i的谜团答案在ai页,必须要到ai页去
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
int a[N],n,vis[N];
int main()
{
cin>>n;
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
int mx=1;
int ans=0;
for(int i=1;i<=n;)
{
int d=a[i];
mx=max(mx,d);
// printf("i:%d\n",i);
while(i<=mx)
{
mx=max(mx,a[i]);
i++;
}
ans++;
}
cout<<ans;
}
/*
10
1 3 4 5 5 6 7 8 9 10
7
*/
B. Good String
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You have a string ss of length nn consisting of only characters > and <. You may do some operations with this string, for each operation you have to choose some character that still remains in the string. If you choose a character >, the character that comes right after it is deleted (if the character you chose was the last one, nothing happens). If you choose a character <, the character that comes right before it is deleted (if the character you chose was the first one, nothing happens).
For example, if we choose character > in string > > < >, the string will become to > > >. And if we choose character < in string > <, the string will become to <.
The string is good if there is a sequence of operations such that after performing it only one character will remain in the string. For example, the strings >, > > are good.
Before applying the operations, you may remove any number of characters from the given string (possibly none, possibly up to n−1n−1, but not the whole string). You need to calculate the minimum number of characters to be deleted from string ss so that it becomes good.
Input
The first line contains one integer tt (1≤t≤1001≤t≤100) – the number of test cases. Each test case is represented by two lines.
The first line of ii-th test case contains one integer nn (1≤n≤1001≤n≤100) – the length of string ss.
The second line of ii-th test case contains string ss, consisting of only characters > and <.
Output
For each test case print one line.
For ii-th test case print the minimum number of characters to be deleted from string ss so that it becomes good.
Example
input
Copy
3 2 <> 3 ><< 1 >
output
Copy
1 0 0
Note
In the first test case we can delete any character in string <>.
In the second test case we don't need to delete any characters. The string > < < is good, because we can perform the following sequence of operations: > < < →→ < < →→ <.
这个题也有点意思,wa了一次,幸好过了,看着题感觉挺难
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
char s[N];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n>>s+1;
int sum=0;
for(int i=1;i<=n;i++)
{
if(s[i]=='<')
{
sum=0;
continue;
}
else sum++;
}
int sum1=0;
for(int i=n;i;i--)
{
if(s[i]=='>')
{
sum1=0;
continue;
}
else sum1++;
}
int ans=min(sum,sum1);
cout<<ans<<endl;
}
}
C. Playlist
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You have a playlist consisting of nn songs. The ii-th song is characterized by two numbers titi and bibi — its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 33 songs having lengths [5,7,4][5,7,4] and beauty values [11,14,6][11,14,6] is equal to (5+7+4)⋅6=96(5+7+4)⋅6=96.
You need to choose at most kk songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
Input
The first line contains two integers nn and kk (1≤k≤n≤3⋅1051≤k≤n≤3⋅105) – the number of songs in the playlist and the maximum number of songs you can choose, respectively.
Each of the next nn lines contains two integers titi and bibi (1≤ti,bi≤1061≤ti,bi≤106) — the length and beauty of ii-th song.
Output
Print one integer — the maximum pleasure you can get.
Examples
input
Copy
4 3 4 7 15 1 3 6 6 8
output
Copy
78
input
Copy
5 3 12 31 112 4 100 100 13 55 55 50
output
Copy
10000
Note
In the first test case we can choose songs 1,3,41,3,4, so the total pleasure is (4+3+6)⋅6=78(4+3+6)⋅6=78.
In the second test case we can choose song 33. The total pleasure will be equal to 100⋅100=10000100⋅100=10000.
这题题意是看懂了, 想了半天不知道怎么做,然后比赛结束后看别人代码做的
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3e5+10;
struct node
{
ll x,y;
bool operator < ( const node a) const
{
return a.y<y;
}
}a[N];
ll n,k;
int main()
{
cin>>n>>k;
priority_queue<int,vector<int>,greater<int> >q;
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
sort(a+1,a+1+n);//幸福值小的在后面
ll sum=0,ans=0;
for(int i=1;i<=n;i++)
{
q.push(a[i].x);
sum+=a[i].x;
if(q.size()>k)
{
sum-=q.top();
q.pop();//去点堆内第一个值最小的,保证sum*幸福值最大
}
ans=max(ans,sum*a[i].y);
}
cout<<ans<<endl;
}
D. Minimum Triangulation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a regular polygon with nn vertices labeled from 11 to nn in counter-clockwise order. The triangulation of a given polygon is a set of triangles such that each vertex of each triangle is a vertex of the initial polygon, there is no pair of triangles such that their intersection has non-zero area, and the total area of all triangles is equal to the area of the given polygon. The weight of a triangulation is the sum of weigths of triangles it consists of, where the weight of a triagle is denoted as the product of labels of its vertices.
Calculate the minimum weight among all triangulations of the polygon.
Input
The first line contains single integer nn (3≤n≤5003≤n≤500) — the number of vertices in the regular polygon.
Output
Print one integer — the minimum weight among all triangulations of the given polygon.
Examples
input
Copy
3
output
Copy
6
input
Copy
4
output
Copy
18
Note
According to Wiki: polygon triangulation is the decomposition of a polygonal area (simple polygon) PP into a set of triangles, i. e., finding a set of triangles with pairwise non-intersecting interiors whose union is PP.
In the first example the polygon is a triangle, so we don't need to cut it further, so the answer is 1⋅2⋅3=61⋅2⋅3=6.
In the second example the polygon is a rectangle, so it should be divided into two triangles. It's optimal to cut it using diagonal 1−31−3 so answer is 1⋅2⋅3+1⋅3⋅4=6+12=181⋅2⋅3+1⋅3⋅4=6+12=18.
发现这题才是最简单的
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int main()
{
cin>>n;
ll now=3;
ll ans=0;
while(now<=n)
{
ans=(ans+(now-1)*now);
now++;
}
cout<<ans<<endl;
}