CSU 1421 Necklace
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1421
题意:给个项链,上面的元素有不同的颜色,分别进行rotate,flip,swap,paint操作,输出C,CS查询结果。
题解:模拟题……注意是一个环,边界的计数,totate、paint的边界都有坑……还有CS查询存在i==j的情况,要放在<的一类里……基本功不行,敲了3个多小时……
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cmath> 6 #include <string> 7 #include <vector> 8 #include <list> 9 #include <map> 10 #include <queue> 11 #include <stack> 12 #include <bitset> 13 #include <algorithm> 14 #include <numeric> 15 #include <functional> 16 #include <set> 17 #include <fstream> 18 19 using namespace std; 20 21 const int maxn=1010; 22 int nec[maxn]; 23 int N; 24 25 void rotate(int k) 26 { 27 int nec2[maxn]; 28 for (int i=1; i<=N; i++) { 29 if (i+k>N) { 30 nec2[(i+k)%N]=nec[i]; 31 } 32 else nec2[i+k]=nec[i]; 33 } 34 for (int i=1; i<=N; i++) { 35 nec[i]=nec2[i]; 36 } 37 } 38 39 void flip() 40 { 41 /*for (int i=1,j=N-1; i<N/2; i++,j--) { 42 swap(nec[i], nec[j]); 43 }*/ 44 int co[maxn]; 45 co[1]=nec[1]; 46 for (int i=2; i<=N; i++) { 47 co[N-i+2]=nec[i]; 48 } 49 for (int i=1; i<=N; i++) { 50 nec[i]=co[i]; 51 } 52 } 53 54 void myswap(int a,int b) 55 { 56 swap(nec[a], nec[b]); 57 } 58 59 void paint(int begin,int end,int colour) 60 { 61 for (int i=begin; i!=end ; i=(i+1)>N?1:i+1) { 62 nec[i]=colour; 63 //cout<<"n"<<i<<" "<<nec[i]<<endl; 64 } 65 nec[end]=colour; 66 } 67 68 void parts1() 69 { 70 int parts=0; 71 int flag=0; 72 for (int i=1; i<=N; i++) { 73 if (nec[i]!=flag) { 74 parts++; 75 flag=nec[i]; 76 } 77 } 78 if (parts>1&&nec[1]==nec[N]) { 79 parts--; 80 } 81 printf("%d\n",parts); 82 } 83 84 void parts2(int begin,int end) 85 { 86 int parts2=0; 87 int flag1=0; 88 if (begin>end) { 89 for (int i=begin; i<=N; i++) { 90 if (nec[i]!=flag1) { 91 // cout<<"c"<<i<<" "<<nec[i]<<endl; 92 flag1=nec[i]; 93 parts2++; 94 } 95 } 96 for (int i=1; i<=end; i++) { 97 if (nec[i]!=flag1) { 98 //cout<<"c"<<i<<" "<<nec[i]<<endl; 99 flag1=nec[i]; 100 parts2++; 101 } 102 } 103 } 104 else if(begin<=end){ 105 int flag2=0; 106 for (int i=begin; i<=end; i++) { 107 if (nec[i]!=flag2) { 108 parts2++; 109 flag2=nec[i]; 110 } 111 } 112 } 113 printf("%d\n",parts2); 114 } 115 116 int main() 117 { 118 // freopen("/Users/apple/Desktop/csu 1421/csu 1421/in", "r", stdin); 119 // freopen("/Users/apple/Desktop/csu 1421/csu 1421/out", "w", stdout); 120 int C,M; 121 char o1[5]={'R'}; 122 char o2[5]={'F'}; 123 char o3[5]={'S'}; 124 char o4[5]={'P'}; 125 char q1[5]={'C'}; 126 char q2[5]={'C','S'}; 127 while ((scanf("%d%d",&N,&C))!=EOF) { 128 for (int i=1; i<=N; i++) { 129 scanf("%d",&nec[i]); 130 } 131 scanf("%d",&M); 132 char s[5]; 133 for (int i=0; i<M; i++) { 134 cin>>s; 135 if (strcmp(s, o1)==0) { 136 int k; 137 cin>>k; 138 rotate(k); 139 } 140 else if (strcmp(s, o2)==0) { 141 flip(); 142 } 143 else if (strcmp(s,o3)==0) { 144 int i,j; 145 cin>>i>>j; 146 myswap(i, j); 147 } 148 else if (strcmp(s, o4)==0) { 149 int i,j,x; 150 cin>>i>>j>>x; 151 paint(i, j,x); 152 } 153 else if (strcmp(s, q1)==0) { 154 parts1(); 155 } 156 else if (strcmp(s, q2)==0) { 157 int i,j; 158 cin>>i>>j; 159 parts2(i,j); 160 } 161 } 162 } 163 return 0; 164 }