CF 101 102 103

A - Homework CodeForces - 101A

Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That’s why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious.

Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters.

Input
The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105).

Output
Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters.

Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them.

Examples
Input
aaaaa
4
Output
1
aaaaa
Input
abacaba
4
Output
1
aaaa
Input
abcdefgh
10
Output
0

Note
In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters “a” from 1 to 5 in length.

In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from “a” (as they are no more than four), which will result in the “aaaa” string.

In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string.

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;

char s[100200];
struct nodea 
{
    int i;
    int num;
    bool friend operator < (nodea a,nodea b){
        return a.num<b.num;
    }
}b[300];
int flag[300];
int main()
{
    scanf("%s",s);
    int n;
    scanf("%d",&n);
    int len = strlen(s);
    for(int i=0;i<len;i++){
        b[s[i]-'a'].i=s[i]-'a';
        b[s[i]-'a'].num++;
    }
    sort(b,b+26);
    int p=0;
    for(int i=0;i<26;i++){
        if(b[i].num==0) continue;
        if(b[i].num<=n){
            n-=b[i].num;
            flag[b[i].i]=b[i].num;
        }else{
            p=26-i;
            flag[b[i].i]=n;
            break;
        }
    }
    printf("%d\n",p);
    for(int i=0;i<len;i++){
        if(flag[s[i]-'a']>0){
            flag[s[i]-'a']--;
        }else{
            printf("%c",s[i]);
        }
    }
    printf("\n");

}

B - Buses CodeForces - 101B 【树状数组】
Little boy Gerald studies at school which is quite far from his house. That’s why he has to go there by bus every day. The way from home to school is represented by a segment of a straight line; the segment contains exactly n + 1 bus stops. All of them are numbered with integers from 0 to n in the order in which they follow from Gerald’s home. The bus stop by Gerald’s home has number 0 and the bus stop by the school has number n.

There are m buses running between the house and the school: the i-th bus goes from stop si to ti (si < ti), visiting all the intermediate stops in the order in which they follow on the segment. Besides, Gerald’s no idiot and he wouldn’t get off the bus until it is still possible to ride on it closer to the school (obviously, getting off would be completely pointless). In other words, Gerald can get on the i-th bus on any stop numbered from si to ti - 1 inclusive, but he can get off the i-th bus only on the bus stop ti.

Gerald can’t walk between the bus stops and he also can’t move in the direction from the school to the house.

Gerald wants to know how many ways he has to get from home to school. Tell him this number. Two ways are considered different if Gerald crosses some segment between the stops on different buses. As the number of ways can be too much, find the remainder of a division of this number by 1000000007 (109 + 7).

Input
The first line contains two space-separated integers: n and m (1 ≤ n ≤ 109, 0 ≤ m ≤ 105). Then follow m lines each containing two integers si, ti. They are the numbers of starting stops and end stops of the buses (0 ≤ si < ti ≤ n).

Output
Print the only number — the number of ways to get to the school modulo 1000000007 (109 + 7).

Examples
Input
2 2
0 1
1 2
Output
1
Input
3 2
0 1
1 2
Output
0
Input
5 5
0 1
0 2
0 3
0 4
0 5
Output
16
Note
The first test has the only variant to get to school: first on bus number one to the bus stop number one; then on bus number two to the bus stop number two.

In the second test no bus goes to the third bus stop, where the school is positioned. Thus, the correct answer is 0.

In the third test Gerald can either get or not on any of the first four buses to get closer to the school. Thus, the correct answer is 24 = 16.

可以任意时间上,只能在汽车终点站下,求到达目的地的方案数。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define LL long long int
const LL maxn = 2e5+55;
const LL mod = 1e9+7;
LL A[maxn],Rn;
void setRank(){
    sort(A+1,A+1+Rn);
    LL I = 1;
    for(LL i=2;i<=Rn;i++){
        if(A[i]!=A[i-1]) A[++I] = A[i];
    }
    Rn = I;
}
LL getRank(LL n){
    return lower_bound(A+1,A+1+Rn,n)-A;
}
struct node
{
    LL u,v;
    bool friend operator < (node a,node b){
        return a.v<b.v;
    }
}a[maxn];
LL sum[maxn];


LL lowbit(LL x)
{
    return x&-x;
}

void add(LL x,LL p)
{
    for(LL i=x;i<=Rn;i+=lowbit(i)){
        (sum[i]+=p)%=mod;
    }
}

LL query(LL x)
{
    LL ans = 0;
    for(LL i=x;i>=1;i-=lowbit(i)){

        (ans+=sum[i])%=mod;
    }
    return ans;
}

int main()
{
    memset(sum,0,sizeof sum);
    LL n,m;
    scanf("%lld %lld",&n,&m);
    A[++Rn]=0;
    A[++Rn]=n;
    for(LL i=1;i<=m;i++){
        scanf("%lld %lld",&a[i].u,&a[i].v);

        A[++Rn] = a[i].u;
        A[++Rn] = a[i].v;
    }
    setRank();

    sort(a+1,a+1+m);

    add(1,1);

    //cout<<query(1);


    for(LL i=1;i<=m;i++){
        LL u=getRank(a[i].u);
        LL v=getRank(a[i].v);
        LL summ=(query(v-1)-query(u-1)+mod)%mod;
        add(v,summ);
    }
    n=getRank(n);
    printf("%lld\n",(query(n)-query(n-1)+mod)%mod);


    return 0;
}

C - Vectors CodeForces - 101C 【math】
At a geometry lesson Gerald was given a task: to get vector B out of vector A. Besides, the teacher permitted him to perform the following operations with vector А:

Turn the vector by 90 degrees clockwise.
Add to the vector a certain vector C.
Operations could be performed in any order any number of times.

Can Gerald cope with the task?

Input
The first line contains integers x1 и y1 — the coordinates of the vector A ( - 108 ≤ x1, y1 ≤ 108). The second and the third line contain in the similar manner vectors B and C (their coordinates are integers; their absolute value does not exceed 108).

Output
Print “YES” (without the quotes) if it is possible to get vector B using the given operations. Otherwise print “NO” (without the quotes).

Examples
Input
0 0
1 1
0 1
Output
YES
Input
0 0
1 1
1 1
Output
YES
Input
0 0
1 1
2 2
Output
NO
可以将走的方式化简,其实就是那么几种,进行比对就可以了,不用搜索。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
const int maxn = 1e5 + 10;
ll a, b, x, y, p, q;
ll sc(ll a, ll b) {
    ll tmp = p * p + q * q;
    if (tmp == 0)
    {
        return a == 0 && b == 0;
    }
    else {
        return (a*p + b * q) % tmp == 0 && (b*p - a * q) % tmp == 0;
    }
}
int main()
{
    scanf("%lld%lld%lld%lld%lld%lld", &a, &b, &x, &y, &p, &q);
    puts(sc(a - x, b - y) || sc(a + x, b + y) || sc(a - y, b + x) || sc(a + y, b - x) ? "YES" : "NO");
    return 0;
}

F - Clothes CodeForces - 102A
A little boy Gerald entered a clothes shop and found out something very unpleasant: not all clothes turns out to match. For example, Gerald noticed that he looks rather ridiculous in a smoking suit and a baseball cap.

Overall the shop sells n clothing items, and exactly m pairs of clothing items match. Each item has its price, represented by an integer number of rubles. Gerald wants to buy three clothing items so that they matched each other. Besides, he wants to spend as little money as possible. Find the least possible sum he can spend.

Input
The first input file line contains integers n and m — the total number of clothing items in the shop and the total number of matching pairs of clothing items ().

Next line contains n integers ai (1 ≤ ai ≤ 106) — the prices of the clothing items in rubles.

Next m lines each contain a pair of space-separated integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi). Each such pair of numbers means that the ui-th and the vi-th clothing items match each other. It is guaranteed that in each pair ui and vi are distinct and all the unordered pairs (ui, vi) are different.

Output
Print the only number — the least possible sum in rubles that Gerald will have to pay in the shop. If the shop has no three clothing items that would match each other, print “-1” (without the quotes).

Examples
Input
3 3
1 2 3
1 2
2 3
3 1
Output
6
Input
3 2
2 3 4
2 3
2 1
Output
-1
Input
4 4
1 1 1 1
1 2
2 3
3 4
4 1
Output
-1
Note
In the first test there only are three pieces of clothing and they all match each other. Thus, there is only one way — to buy the 3 pieces of clothing; in this case he spends 6 roubles.

The second test only has three pieces of clothing as well, yet Gerald can’t buy them because the first piece of clothing does not match the third one. Thus, there are no three matching pieces of clothing. The answer is -1.

In the third example there are 4 pieces of clothing, but Gerald can’t buy any 3 of them simultaneously. The answer is -1.
求三元组,直接暴力。‘’

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define ll long long int
const int maxn=1e2+10;
#define INF 0x3f3f3f3f
int a[maxn][maxn];
int vis[maxn][maxn];
int q[maxn];
int main()
{
    memset(a,0,sizeof a);
    int n,m;int ans=INF;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&q[i]);
    }
    int u,v;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&u,&v);
        a[u][v]=1;
        a[v][u]=1;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==j) continue;
            if(a[i][j])
            {
                for(int k=1;k<=n;k++)
                {
                    if(k==i||k==j) continue;
                    if(a[j][k]&&a[i][k]){
                        ans=min(ans,q[i]+q[j]+q[k]);
                    }
                }
            }
        }
    }
    if(ans!=INF)
        cout<<ans<<endl;
    else
        cout<<"-1"<<endl;
    return 0;
}

G - Sum of Digits CodeForces - 102B
Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father’s magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a number n. How many times can Gerald put a spell on it until the number becomes one-digit?

Input
The first line contains the only integer n (0 ≤ n ≤ 10100000). It is guaranteed that n doesn’t contain any leading zeroes.

Output
Print the number of times a number can be replaced by the sum of its digits until it only contains one digit.

Examples
Input
0
Output
0
Input
10
Output
1
Input
991
Output
3
Note
In the first sample the number already is one-digit — Herald can’t cast a spell.

The second test contains number 10. After one casting of a spell it becomes 1, and here the process is completed. Thus, Gerald can only cast the spell once.

The third test contains number 991. As one casts a spell the following transformations take place: 991 → 19 → 10 → 1. After three transformations the number becomes one-digit.
指定一种操作,模拟题。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define ll long long int
const int maxn=1e5+10;
char s[maxn];
int main()
{
    scanf("%s",s);
    int tmp=0;
    int len=strlen(s);
    ll sum=0;
    if(len==1)
    {
        printf("0\n");
        return 0;
    }
    for(int i=0;i<len;i++)
    {
        sum+=1ll*(s[i]-'0');
    }
    if(sum==0)
    {
        printf("%d\n",tmp);
    }
    else{
        tmp++;
        while(sum>=10)
        {
            ++tmp;
            ll cnt=0;
            while(sum>0)
            {
                cnt+=sum%10;
                sum/=10;
            }
            sum=cnt;
            //cout<<sum<<endl;
        }
        printf("%d\n",tmp);
    }
    return 0;
}

H - Testing Pants for Sadness CodeForces - 103A
The average miner Vaganych took refresher courses. As soon as a miner completes the courses, he should take exams. The hardest one is a computer test called “Testing Pants for Sadness”.

The test consists of n questions; the questions are to be answered strictly in the order in which they are given, from question 1 to question n. Question i contains ai answer variants, exactly one of them is correct.

A click is regarded as selecting any answer in any question. The goal is to select the correct answer for each of the n questions. If Vaganych selects a wrong answer for some question, then all selected answers become unselected and the test starts from the very beginning, from question 1 again. But Vaganych remembers everything. The order of answers for each question and the order of questions remain unchanged, as well as the question and answers themselves.

Vaganych is very smart and his memory is superb, yet he is unbelievably unlucky and knows nothing whatsoever about the test’s theme. How many clicks will he have to perform in the worst case?

Input
The first line contains a positive integer n (1 ≤ n ≤ 100). It is the number of questions in the test. The second line contains space-separated n positive integers ai (1 ≤ ai ≤ 109), the number of answer variants to question i.

Output
Print a single number — the minimal number of clicks needed to pass the test it the worst-case scenario.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator.

Examples
Input
2
1 1
Output
2
Input
2
2 2
Output
5
Input
1
10
Output
10
Note
Note to the second sample. In the worst-case scenario you will need five clicks:

the first click selects the first variant to the first question, this answer turns out to be wrong.
the second click selects the second variant to the first question, it proves correct and we move on to the second question;
the third click selects the first variant to the second question, it is wrong and we go back to question 1;
the fourth click selects the second variant to the first question, it proves as correct as it was and we move on to the second question;
the fifth click selects the second variant to the second question, it proves correct, the test is finished.

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
#define ll long long int
const int maxn=1e5+10;
int a[maxn];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    ll ans=a[1];
    for(int i=2;i<=n;i++)
    {
        ans+=1ll*(a[i]-1)*(i-1)+a[i];
    }
    cout<<ans<<endl;


    return 0;
}

I - Cthulhu CodeForces - 103B
…Once upon a time a man came to the sea. The sea was stormy and dark. The man started to call for the little mermaid to appear but alas, he only woke up Cthulhu…

Whereas on the other end of the world Pentagon is actively collecting information trying to predict the monster’s behavior and preparing the secret super weapon. Due to high seismic activity and poor weather conditions the satellites haven’t yet been able to make clear shots of the monster. The analysis of the first shot resulted in an undirected graph with n vertices and m edges. Now the world’s best minds are about to determine whether this graph can be regarded as Cthulhu or not.

To add simplicity, let’s suppose that Cthulhu looks from the space like some spherical body with tentacles attached to it. Formally, we shall regard as Cthulhu such an undirected graph that can be represented as a set of three or more rooted trees, whose roots are connected by a simple cycle.

It is guaranteed that the graph contains no multiple edges and self-loops.

Input
The first line contains two integers — the number of vertices n and the number of edges m of the graph (1 ≤ n ≤ 100, 0 ≤ m ≤ ).

Each of the following m lines contains a pair of integers x and y, that show that an edge exists between vertices x and y (1 ≤ x, y ≤ n, x ≠ y). For each pair of vertices there will be at most one edge between them, no edge connects a vertex to itself.

Output
Print “NO”, if the graph is not Cthulhu and “FHTAGN!” if it is.

Examples
Input
6 6
6 3
6 4
5 1
2 5
1 4
5 4
Output
FHTAGN!
Input
6 5
5 6
4 6
3 1
5 1
1 2
Output
NO
Note
Let us denote as a simple cycle a set of v vertices that can be numbered so that the edges will only exist between vertices number 1 and 2, 2 and 3, …, v - 1 and v, v and 1.

A tree is a connected undirected graph consisting of n vertices and n - 1 edges (n > 0).

A rooted tree is a tree where one vertex is selected to be the root.

找出三个或三个以上根连成环的树,有且只有一组。

连通图+判环n=m

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
#define ll long long int
const int maxn=1e5+10;

vector<int>G[123];


void add(int u,int v)
{
    G[u].push_back(v);  

}   

int num=0;
int flag[123];
void dfs(int u,int pre)
{
    if(!flag[u]){
        num++;
        flag[u]=1;
        for(int i=0;i<G[u].size();i++){
            int v=G[u][i];
            if(v==pre) continue;
            dfs(v,u);
        }
    }

}


int main()
{

    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++){
        int u,v;
        scanf("%d %d",&u,&v);
        add(u,v);
        add(v,u);
    }
    if(m!=n){
        printf("NO\n");
    }else{
        dfs(1,-1);
        if(num==n){
            printf("FHTAGN!\n");
        }else{
            printf("NO\n");
        }
    }


    return 0;
}

J - Russian Roulette CodeForces - 103C 【博弈】
After all the events in Orlando we all know, Sasha and Roma decided to find out who is still the team’s biggest loser. Thankfully, Masha found somewhere a revolver with a rotating cylinder of n bullet slots able to contain exactly k bullets, now the boys have a chance to resolve the problem once and for all.

Sasha selects any k out of n slots he wishes and puts bullets there. Roma spins the cylinder so that every of n possible cylinder’s shifts is equiprobable. Then the game starts, the players take turns, Sasha starts: he puts the gun to his head and shoots. If there was no bullet in front of the trigger, the cylinder shifts by one position and the weapon is given to Roma for make the same move. The game continues until someone is shot, the survivor is the winner.

Sasha does not want to lose, so he must choose slots for bullets in such a way as to minimize the probability of its own loss. Of all the possible variant he wants to select the lexicographically minimal one, where an empty slot is lexicographically less than a charged one.

More formally, the cylinder of n bullet slots able to contain k bullets can be represented as a string of n characters. Exactly k of them are “X” (charged slots) and the others are “.” (uncharged slots).

Let us describe the process of a shot. Suppose that the trigger is in front of the first character of the string (the first slot). If a shot doesn’t kill anyone and the cylinder shifts, then the string shifts left. So the first character becomes the last one, the second character becomes the first one, and so on. But the trigger doesn’t move. It will be in front of the first character of the resulting string.

Among all the strings that give the minimal probability of loss, Sasha choose the lexicographically minimal one. According to this very string, he charges the gun. You have to help Sasha to charge the gun. For that, each xi query must be answered: is there a bullet in the positions xi?

Input
The first line contains three integers n, k and p (1 ≤ n ≤ 1018, 0 ≤ k ≤ n, 1 ≤ p ≤ 1000) — the number of slots in the cylinder, the number of bullets and the number of queries. Then follow p lines; they are the queries. Each line contains one integer xi (1 ≤ xi ≤ n) the number of slot to describe.

Please do not use the %lld specificator to read or write 64-bit numbers in С++. It is preferred to use cin, cout streams or the %I64d specificator.

Output
For each query print “.” if the slot should be empty and “X” if the slot should be charged.

Examples
Input
3 1 3
1
2
3
Output
..X
Input
6 3 6
1
2
3
4
5
6
Output
.X.X.X
Input
5 2 5
1
2
3
4
5
Output
…XX
Note
The lexicographical comparison of is performed by the < operator in modern programming languages. The a string is lexicographically less that the b string, if there exists such i (1 ≤ i ≤ n), that ai < bi, and for any j (1 ≤ j < i) aj = bj.
俄罗斯轮盘赌
随机转膛,求代价最小,字典序最下的方案。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
const int maxn = 1e5 + 10;
int main()
{
    long long n, k, p, a;
    cin >> n >> k >> p;
    if (n % 2 && k) { n--; k--; }
    if (2 * k > n) { 
        long long tmp1 = n, tmp2 = k;
        n = 2 * tmp1 - 2 * tmp2;
        k = tmp1 - tmp2;
    }
    while (p--)

    {
        cin >> a;
        if ((a > n) || ((a % 2 == 0) && ((n - a) < 2*k))) putchar('X'); else putchar('.');
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值