Codeforces Round #350 (Div. 2) A B C D E STL (list map stack)



链接:戳这里


A. Holidays
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
On the planet Mars a year lasts exactly n days (there are no leap years on Mars). But Martians have the same weeks as earthlings — 5 work days and then 2 days off. Your task is to determine the minimum possible and the maximum possible number of days off per year on Mars.

Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 1 000 000) — the number of days in a year on Mars.

Output
Print two integers — the minimum possible and the maximum possible number of days off per year on Mars.

Examples
input
14
output
4 4
input
2
output
0 2
Note
In the first sample there are 14 days in a year on Mars, and therefore independently of the day a year starts with there will be exactly 4 days off .

In the second sample there are only 2 days in a year on Mars, and they can both be either work days or days off.


思路:

休息日取min时  则从星期一开始加  休息日取max时  从星期六开始加


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n;
int main(){
    scanf("%d",&n);
    int m=n;
    int ans1=0,ans2=0;
    while(n>0){
        n-=5;
        if(n>0) ans1+=min(2,n);
        n-=min(2,n);
    }
    while(m>0){
        ans2+=min(2,m);
        m-=7;
    }
    cout<<ans1<<" "<<ans2<<endl;
    return 0;
}


B. Game of Robots
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
In late autumn evening n robots gathered in the cheerful company of friends. Each robot has a unique identifier — an integer from 1 to 109.

At some moment, robots decided to play the game "Snowball". Below there are the rules of this game. First, all robots stand in a row. Then the first robot says his identifier. After that the second robot says the identifier of the first robot and then says his own identifier. Then the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own. This process continues from left to right until the n-th robot says his identifier.

Your task is to determine the k-th identifier to be pronounced.

Input
The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ min(2·109, n·(n + 1) / 2).

The second line contains the sequence id1, id2, ..., idn (1 ≤ idi ≤ 109) — identifiers of roborts. It is guaranteed that all identifiers are different.

Output
Print the k-th pronounced identifier (assume that the numeration starts from 1).

Examples
input
2 2
1 2
output
1
input
4 5
10 4 18 3
output
4
Note
In the first sample identifiers of robots will be pronounced in the following order: 1, 1, 2. As k = 2, the answer equals to 1.

In the second test case identifiers of robots will be pronounced in the following order: 10, 10, 4, 10, 4, 18, 10, 4, 18, 3. As k = 5, the answer equals to 4.


思路:

先存下每一个人能报的个数   取出前缀和  然后lower_bound找出当前k能在第x个人被数完


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
ll a[100100];
ll b[100100];
ll n,k;
int main(){
    scanf("%I64d%I64d",&n,&k);
    a[0]=0;
    for(int i=1;i<=n;i++) a[i]=a[i-1]+i;
    for(int i=1;i<=n;i++) scanf("%I64d",&b[i]);
    int x=lower_bound(a+1,a+n+1,k)-a;
    ll t=k-a[x-1];
    printf("%I64d\n",b[t]);
    return 0;
}


C. Cinema
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Moscow is hosting a major international conference, which is attended by n scientists from different countries. Each of the scientists knows exactly one language. For convenience, we enumerate all languages of the world with integers from 1 to 109.

In the evening after the conference, all n scientists decided to go to the cinema. There are m movies in the cinema they came to. Each of the movies is characterized by two distinct numbers — the index of audio language and the index of subtitles language. The scientist, who came to the movie, will be very pleased if he knows the audio language of the movie, will be almost satisfied if he knows the language of subtitles and will be not satisfied if he does not know neither one nor the other (note that the audio language and the subtitles language for each movie are always different).

Scientists decided to go together to the same movie. You have to help them choose the movie, such that the number of very pleased scientists is maximum possible. If there are several such movies, select among them one that will maximize the number of almost satisfied scientists.

Input
The first line of the input contains a positive integer n (1 ≤ n ≤ 200 000) — the number of scientists.

The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the index of a language, which the i-th scientist knows.

The third line contains a positive integer m (1 ≤ m ≤ 200 000) — the number of movies in the cinema.

The fourth line contains m positive integers b1, b2, ..., bm (1 ≤ bj ≤ 109), where bj is the index of the audio language of the j-th movie.

The fifth line contains m positive integers c1, c2, ..., cm (1 ≤ cj ≤ 109), where cj is the index of subtitles language of the j-th movie.

It is guaranteed that audio languages and subtitles language are different for each movie, that is bj ≠ cj.

Output
Print the single integer — the index of a movie to which scientists should go. After viewing this movie the number of very pleased scientists should be maximum possible. If in the cinema there are several such movies, you need to choose among them one, after viewing which there will be the maximum possible number of almost satisfied scientists.

If there are several possible answers print any of them.

Examples
input
3
2 3 2
2
3 2
2 3
output
2
input
6
6 3 1 1 3 7
5
1 2 3 4 5
2 3 4 5 1
output
1
Note
In the first sample, scientists must go to the movie with the index 2, as in such case the 1-th and the 3-rd scientists will be very pleased and the 2-nd scientist will be almost satisfied.

In the second test case scientists can go either to the movie with the index 1 or the index 3. After viewing any of these movies exactly two scientists will be very pleased and all the others will be not satisfied.


思路:

map统计每种语言出现的次数,判断b数组中那种语言最多,最多语言仅为1的话就是anw.

有多种最多语言则判断这最多的语言中在c数组中出现的次数最多的  任意输出最多的一种所在的电影编号


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m;
int a[200100],b[200100],c[200100];
map<int,int> M1;
struct node{
    int num,id;
}s[200100],p[200100];
bool cmp(node a,node b){
    if(a.num==b.num) return a.id<b.id;
    return a.num>b.num;
}
int vis[200100];
int main(){
    scanf("%d",&n);
    mst(vis,0);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    scanf("%d",&m);
    for(int i=1;i<=m;i++) scanf("%d",&b[i]);
    for(int i=1;i<=m;i++) scanf("%d",&c[i]);
    for(int i=1;i<=n;i++) M1[a[i]]++;
    int ans=-1;
    for(int i=1;i<=m;i++) {
        if(M1[b[i]]>ans) ans=M1[b[i]];
    }
    for(int i=1;i<=m;i++){
        if(M1[b[i]]==ans) vis[i]=1;
    }
    ans=-1;
    int x;
    for(int i=1;i<=m;i++){
        if(vis[i]){
            if(M1[c[i]]>ans){
                ans=M1[c[i]];
                x=i;
            }
        }
    }
    cout<<x<<endl;
    return 0;
}


D1. Magic Powder - 1
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.

Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.

Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the n ingredients and can be used for baking cookies.

Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.

Input
The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.

The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.

Output
Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.

Examples
input
3 1
2 1 4
11 3 16
output
4
input
4 3
4 3 5 6
11 12 14 20
output
3
Note
In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.

In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.



思路:

二分这个最大的anw

然后去拿mid去判断是否可以用k填满这个anw


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
ll n,k;
ll a[100100],b[100100];
bool solve(ll x){
    ll sum=k;
    for(int i=1;i<=n;i++){
        if((b[i]-a[i]*x)<0){
            ll t=a[i]*x-b[i];
            sum-=t;
            if(sum<0) return false;
        }
    }
    return true;
}
int main(){
    scanf("%I64d%I64d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
    for(int i=1;i<=n;i++) scanf("%I64d",&b[i]);
    ll l=0,r=1e10,mid,ans=0;
    while(l<r){
        mid=(l+r)/2;
        if(solve(mid)) {
            l=mid+1;
            ans=mid;
        } else r=mid;
    }
    printf("%I64d\n",ans);
    return 0;
}


E. Correct Bracket Sequence Editor
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS).

Note that a bracket sequence is correct if it is possible to get a correct mathematical expression by adding "+"-s and "1"-s to it. For example, sequences "(())()", "()" and "(()(()))" are correct, while ")(", "(()" and "(()))(" are not. Each bracket in CBS has a pair. For example, in "(()(()))":

1st bracket is paired with 8th,
2d bracket is paired with 3d,
3d bracket is paired with 2d,
4th bracket is paired with 7th,
5th bracket is paired with 6th,
6th bracket is paired with 5th,
7th bracket is paired with 4th,
8th bracket is paired with 1st.
Polycarp's editor currently supports only three operations during the use of CBS. The cursor in the editor takes the whole position of one of the brackets (not the position between the brackets!). There are three operations being supported:

«L» — move the cursor one position to the left,
«R» — move the cursor one position to the right,
«D» — delete the bracket in which the cursor is located, delete the bracket it's paired to and all brackets between them (that is, delete a substring between the bracket in which the cursor is located and the one it's paired to).
After the operation "D" the cursor moves to the nearest bracket to the right (of course, among the non-deleted). If there is no such bracket (that is, the suffix of the CBS was deleted), then the cursor moves to the nearest bracket to the left (of course, among the non-deleted).

There are pictures illustrated several usages of operation "D" below.

All incorrect operations (shift cursor over the end of CBS, delete the whole CBS, etc.) are not supported by Polycarp's editor.

Polycarp is very proud of his development, can you implement the functionality of his editor?

Input
The first line contains three positive integers n, m and p (2 ≤ n ≤ 500 000, 1 ≤ m ≤ 500 000, 1 ≤ p ≤ n) — the number of brackets in the correct bracket sequence, the number of operations and the initial position of cursor. Positions in the sequence are numbered from left to right, starting from one. It is guaranteed that n is even.

It is followed by the string of n characters "(" and ")" forming the correct bracket sequence.

Then follow a string of m characters "L", "R" and "D" — a sequence of the operations. Operations are carried out one by one from the first to the last. It is guaranteed that the given operations never move the cursor outside the bracket sequence, as well as the fact that after all operations a bracket sequence will be non-empty.

Output
Print the correct bracket sequence, obtained as a result of applying all operations to the initial sequence.

Examples
input
8 4 5
(())()()
RDLD
output
()
input
12 5 3
((()())(()))
RRDLD
output
(()(()))
input
8 8 8
(())()()
LLLLLLDD
output
()()
Note
In the first sample the cursor is initially at position 5. Consider actions of the editor:

command "R" — the cursor moves to the position 6 on the right;
command "D" — the deletion of brackets from the position 5 to the position 6. After that CBS takes the form (())(), the cursor is at the position 5;
command "L" — the cursor moves to the position 4 on the left;
command "D" — the deletion of brackets from the position 1 to the position 4. After that CBS takes the form (), the cursor is at the position 1.
Thus, the answer is equal to ().


思路:

用栈存下每个'(' || ')'  对应的(左右)括号的位置

光标需要注意不能移到外面去,数组模拟链表执行操作就可以了

更新每个位置的l,r  这样就是链表的实现

然后输出链表存下来的部分


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m,now;
char s[500100],p[500100];
stack<int> sta;
int a[500100];
struct node{
    int l,r;
}L[500100];
int main(){
    scanf("%d%d%d%s%s",&n,&m,&now,s+1,p+1);
    for(int i=1;i<=n;i++){
        if(s[i]=='(') sta.push(i);
        else {
            a[i]=sta.top();
            a[sta.top()]=i;
            sta.pop();
        }
    }
    for(int i=1;i<=n;i++){
        L[i].l=i-1;
        L[i].r=i+1;
    }
    L[n].r=0;
    for(int i=1;i<=m;i++){
        if(p[i]=='R') now=L[now].r;
        else if(p[i]=='L') now=L[now].l;
        else {
            if(s[now]=='('){
                int x=now;
                int y=a[now];
                now=L[y].r;
                if(now==0) now=L[x].l;
                L[L[x].l].r=L[y].r;
                L[L[y].r].l=L[x].l;
            } else {
                int x=a[now];
                int y=now;
                now=L[y].r;
                if(now==0) now=L[x].l;
                L[L[x].l].r=L[y].r;
                L[L[y].r].l=L[x].l;
            }
        }
    }
    ///for(int i=1;i<=n;i++) cout<<L[i].l<<" "<<L[i].r<<endl;
    while(L[now].l!=0) now=L[now].l;
    for(int i=now;i!=0;i=L[i].r) printf("%c",s[i]);
    cout<<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值