IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A B C




链接:戳这里

A. Bear and Three Balls
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Limak is a little polar bear. He has n balls, the i-th ball has size ti.

Limak wants to give one ball to each of his three friends. Giving gifts isn't easy — there are two rules Limak must obey to make friends happy:

No two friends can get balls of the same size.
No two friends can get balls of sizes that differ by more than 2.
For example, Limak can choose balls with sizes 4, 5 and 3, or balls with sizes 90, 91 and 92. But he can't choose balls with sizes 5, 5 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 30, 31 and 33 (because sizes 30 and 33 differ by more than 2).

Your task is to check whether Limak can choose three balls that satisfy conditions above.

Input
The first line of the input contains one integer n (3 ≤ n ≤ 50) — the number of balls Limak has.

The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 1000) where ti denotes the size of the i-th ball.

Output
Print "YES" (without quotes) if Limak can choose three balls of distinct sizes, such that any two of them differ by no more than 2. Otherwise, print "NO" (without quotes).

Examples
input
4
18 55 16 17


output
YES


input
6
40 41 43 44 44 44


output
NO


input
8
5 972 3 4 1 4 970 971


output
YES


Note
In the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes 18, 16 and 17.

In the second sample, there is no way to give gifts to three friends without breaking the rules.

In the third sample, there is even more than one way to choose balls:

Choose balls with sizes 3, 4 and 5.
Choose balls with sizes 972, 970, 971.


题意:需要给三个朋友发球,每个球有大小,要求三个球的大小连续


思路:暴力


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#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 a[110],num[10010];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        num[a[i]]++;
    }
    for(int i=1;i<=998;i++){
        if(num[i] && num[i+1] && num[i+2]){
            cout<<"YES"<<endl;
            return 0;
        }
    }
    cout<<"NO"<<endl;
    return 0;
}


B. Bear and Compressing
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Limak is a little polar bear. Polar bears hate long strings and thus they like to compress them. You should also know that Limak is so young that he knows only first six letters of the English alphabet: 'a', 'b', 'c', 'd', 'e' and 'f'.

You are given a set of q possible operations. Limak can perform them in any order, any operation may be applied any number of times. The i-th operation is described by a string ai of length two and a string bi of length one. No two of q possible operations have the same string ai.

When Limak has a string s he can perform the i-th operation on s if the first two letters of s match a two-letter string ai. Performing the i-th operation removes first two letters of s and inserts there a string bi. See the notes section for further clarification.

You may note that performing an operation decreases the length of a string s exactly by 1. Also, for some sets of operations there may be a string that cannot be compressed any further, because the first two letters don't match any ai.

Limak wants to start with a string of length n and perform n - 1 operations to finally get a one-letter string "a". In how many ways can he choose the starting string to be able to get "a"? Remember that Limak can use only letters he knows.

Input
The first line contains two integers n and q (2 ≤ n ≤ 6, 1 ≤ q ≤ 36) — the length of the initial string and the number of available operations.

The next q lines describe the possible operations. The i-th of them contains two strings ai and bi (|ai| = 2, |bi| = 1). It's guaranteed that ai ≠ aj for i ≠ j and that all ai and bi consist of only first six lowercase English letters.

Output
Print the number of strings of length n that Limak will be able to transform to string "a" by applying only operations given in the input.

Examples
input
3 5
ab a
cc c
ca a
ee c
ff d


output
4


input
2 8
af e
dc d
cc f
bc b
da b
eb a
bb b
ff c


output
1


input
6 2
bb a
ba a


output
0


Note
In the first sample, we count initial strings of length 3 from which Limak can get a required string "a". There are 4 such strings: "abb", "cab", "cca", "eea". The first one Limak can compress using operation 1 two times (changing "ab" to a single "a"). The first operation would change "abb" to "ab" and the second operation would change "ab" to "a".

Other three strings may be compressed as follows:

"cab"  "ab"  "a"
"cca"  "ca"  "a"
"eea"  "ca"  "a"
In the second sample, the only correct initial string is "eb" because it can be immediately compressed to "a".


题意:给定n,q。n代表字符串的长度   q代表q次操作  (2<=n<=6) 组成的长度为n的串只能从(a,b,c,d,e,f)里面任意选

接下来q个  s ,p      |s|==2 && |p|==1  表示p字符可以代替s串

要求从q次操作里任意选择(n-1)次操作使长度为n的字符串变成长度为1的串  并且最终只剩下‘a’

问满足情况的长度为n的串有多少种  这里需要注意一个地方,也是我看题目没有看到的地方,就是代替的时候必须是从1-n的顺序代替过去   意思是如果12不能被代替的话那么后面的3456即使可以代替也不行


思路:全排列也才6^6   暴力+模拟  DFS跑满足的情况累加就可以了。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#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;
vector<int>V;
int a[55],b[55],c[55];
int n,q,num=0;
void DFS(int x){
    if(x==n){
        int now=V[0],t=0;
        for(int i=1;i<V.size();i++){
            int flag=0;
            for(int j=1;j<=q;j++){
                if(now==a[j] && V[i]==b[j]){
                    flag=1;
                    now=c[j];
                    break;
                }
            }
            if(!flag) {
                t=1;
                break;
            }
        }
        if(now==1 && t==0) num++;
        return ;
    }
    for(int i=1;i<=6;i++){
        V.push_back(i);
        DFS(x+1);
        V.pop_back();
    }
}
int main(){
    scanf("%d%d",&n,&q);
    for(int i=1;i<=q;i++) {
        string t;
        char C;
        cin>>t>>C;
        a[i]=t[0]-'a'+1;
        b[i]=t[1]-'a'+1;
        c[i]=C-'a'+1;
    }
    DFS(0);
    cout<<num<<endl;
    return 0;
}




C. Bear and Up-Down
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The life goes up and down, just like nice sequences. Sequence t1, t2, ..., tn is called nice if the following two conditions are satisfied:

ti < ti + 1 for each odd i < n;
ti > ti + 1 for each even i < n.
For example, sequences (2, 8), (1, 5, 1) and (2, 5, 1, 100, 99, 120) are nice, while (1, 1), (1, 2, 3) and (2, 5, 3, 2) are not.

Bear Limak has a sequence of positive integers t1, t2, ..., tn. This sequence is not nice now and Limak wants to fix it by a single swap. He is going to choose two indices i < j and swap elements ti and tj in order to get a nice sequence. Count the number of ways to do so. Two ways are considered different if indices of elements chosen for a swap are different.

Input
The first line of the input contains one integer n (2 ≤ n ≤ 150 000) — the length of the sequence.

The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 150 000) — the initial sequence. It's guaranteed that the given sequence is not nice.

Output
Print the number of ways to swap two elements exactly once in order to get a nice sequence.

Examples
input
5
2 8 4 7 7


output
2


input
4
200 150 100 50


output
1


input
10
3 2 1 4 1 4 1 4 1 4


output
8


input
9
1 2 3 4 5 6 7 8 9


output
0


Note
In the first sample, there are two ways to get a nice sequence with one swap:

Swap t2 = 8 with t4 = 7.
Swap t1 = 2 with t5 = 7.
In the second sample, there is only one way — Limak should swap t1 = 200 with t4 = 50.


题意:给出一个长为n的序列,nice序列的定义是

当i为奇数时  a[i]严格小于左右两边

当i为偶数时  a[i]严格大于左右两边

给定一个不是nice的序列  ,要求只能交换一次元素swap(a[i],a[j])  的情况下使序列nice的种数有多少


思路:当你的序列中属于坏点的元素过多时,不管你怎么换一次,也不行。坏点的意思是破坏nice序列

所以只能在少数坏点的情况下才能变成nice序列,先找出坏点,如果坏点超出6个 GG

那么直接暴力坏点去交换n个点,交换之后变成nice就++


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#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,tot;
int a[1000100];
int so[1000100];
set< pair<int,int> > S;
bool check(){
    for(int i=0;i<tot;i++){
        for(int j=-1;j<=1;j++){
            int pos=so[i]+j;
            if(pos==0 || pos==n+1) continue;
            if(pos%2==1){
                if(a[pos]>=a[pos-1] || a[pos]>=a[pos+1])
                    return false;
            }else {
                if(a[pos]<=a[pos-1] || a[pos]<=a[pos+1])
                    return false;
            }
        }
    }
    return true;
}
int main(){
    tot=0;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    a[0]=1e9;
    if(n%2==1) a[n+1]=1e9;
    else a[n+1]=-1;
    int x,y;
    for(int i=1;i<=n;i++){
        if(i%2==1){
            if(a[i]>=a[i+1] || a[i]>=a[i-1]){
                so[tot++]=i;
            }
        } else {
            if(a[i]<=a[i+1] || a[i]<=a[i-1]){
                so[tot++]=i;
            }
        }
    }
    if(tot>6) cout<<0<<endl;
    else {
        ll ans=0;
        for(int i=0;i<tot;i++){
            for(int j=1;j<=n;j++){
                if(so[i]==j) continue;
                swap(a[so[i]],a[j]);
                bool flag=check();
                if(j%2==1){
                    if(a[j]>=a[j-1] || a[j]>=a[j+1])
                        flag=false;
                } else {
                    if(a[j]<=a[j-1] || a[j]<=a[j+1])
                        flag=false;
                }
                if(flag){
                    pair<int,int> tmp=make_pair(min(so[i],j),max(so[i],j));
                    if(S.count(tmp)==0){
                        S.insert(tmp);
                        ans++;
                    }
                }
                swap(a[so[i]],a[j]);
            }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A B C)