Numbers
There are n numbers A1,A2....An{A}_{1},{A}_{2}....{A}_{n}A1,A2....An,your task is to check whether there exists there different positive integers i, j, k (1≤i,j,k≤n1leq i , j , k leq n1≤i,j,k≤n) such that Ai−Aj=Ak{A}_{i}-{A}_{j}={A}_{k}Ai−Aj=Ak
There are multiple test cases, no more than 1000 cases. First line of each case contains a single integer n.(3≤n≤100)(3leq nleq 100)(3≤n≤100). Next line contains n integers A1,A2....An{A}_{1},{A}_{2}....{A}_{n}A1,A2....An.(0≤Ai≤1000)(0leq {A}_{i}leq 1000)(0≤Ai≤1000)
For each case output "YES" in a single line if you find such i, j, k, otherwise output "NO".
3 3 1 2 3 1 0 2 4 1 1 0 2
YES NO YES
题解:刚开始用vector写的,发现原来没有count。。。就改成了;
代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<set> 7 using namespace std; 8 const int INF=0x3f3f3f3f; 9 const int MAXN=1010; 10 int m[MAXN]; 11 int cnt[MAXN]; 12 set<int>st; 13 int main(){ 14 int n; 15 while(~scanf("%d",&n)){ 16 st.clear(); 17 memset(cnt,0,sizeof(cnt)); 18 for(int i=0;i<n;i++){ 19 scanf("%d",m+i); 20 st.insert(m[i]); 21 cnt[m[i]]++; 22 } 23 int su,flot=0; 24 for(int i=0;i<n;i++){ 25 for(int j=i+1;j<n;j++){ 26 su=m[i]+m[j]; 27 if(su==m[i]||su==m[j]){ 28 if(cnt[su]==1)continue; 29 } 30 if(m[i]==m[j]&&m[i]==su){ 31 if(cnt[su]==2)continue; 32 } 33 if(st.count(su)){ 34 flot=1; 35 break; 36 } 37 } 38 if(flot)break; 39 } 40 if(flot)puts("YES"); 41 else puts("NO"); 42 } 43 return 0; 44 }
Game
XY is playing a game:there are N pillar in a row,which numbered from 1 to n.Each pillar has a jewel.Now XY is standing on the S-th pillar and the exit is in the T-th pillar.XY can leave from the exit only after they get all the jewels.Each time XY can move to adjacent pillar,or he can jump to boundary ( the first pillar or the N-th pillar) by using his superpower.However,he needs to follow a rule:if he left the pillar,he no can not get here anymore.In order to save his power,XY wants to use the minimum number of superpower to pass the game.
There are multiple test cases, no more than 1000 cases. For each case,the line contains three integers:N,S and T.(1≤N≤10000,1≤S,T≤N)(1leq Nleq10000,1leq S,Tleq N )(1≤N≤10000,1≤S,T≤N)
The output of each case will be a single integer on a line: the minimum number of using superpower or output -1 if he can't leave.
4 1 4 4 1 3
0 1
题解:各种判断。。。
代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<set> 7 using namespace std; 8 const int INF=0x3f3f3f3f; 9 int FABS(int x){ 10 return x>0?x:-x; 11 } 12 int main(){ 13 int N,S,T; 14 while(~scanf("%d%d%d",&N,&S,&T)){ 15 if(N==1)puts("0"); 16 else if(S==T)puts("-1"); 17 else if(S==1||S==N){ 18 if(T==1||T==N)puts("0"); 19 else puts("1"); 20 } 21 else if(T==1||T==N){ 22 if(FABS(T-S)==1)puts("1"); 23 else 24 puts("2"); 25 } 26 else{ 27 if(FABS(T-S)==1)puts("1"); 28 else 29 puts("2"); 30 } 31 } 32 return 0; 33 }