链接:戳这里
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; }
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; }
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; }