This question is also very watery, But I wasted five submissions on the second question. The penalty is so serious that TT is still too watery. Just write a question... A
This question is also very watery, But I wasted five submissions on the second question. The penalty is serious. T is still too watery. Just write a question... A-Difference Row hand speed questions, I also egg pain to extract the largest and the smallest out and re-order... In fact, you only need to sort all the data so that the largest and smallest positions can be changed. The complexity is sort's O (nl
This question is also very watery, But I wasted five submissions on the second question. The penalty is severe. T
Sure enough, it's still too watery. Just write a question...
A-Difference Row
I also extracted the maximum and minimum values and sorted them again...
In fact, you only need to sort all the data so that the largest and smallest positions can be changed.
The complexity is sort's O (nlogn)
Code:
/** Author: illuz
* Blog: http://blog.csdn.net/hcbbt* File: a.cpp* Create Date: 2013-09-20 23:32:37* Descripton: a */#include
#include using namespace std;#define rep(i, n) for (int i = 0; i < (n); i++)const int MAXN = 1001;int n, ra, ri, a[MAXN];int Max = -1000, Min = 1001;int main() {scanf("%d", &n);rep(i, n) {scanf("%d", &a[i]);if (Max < a[i]) {Max = a[i];ra = i;}if (Min > a[i]) {Min = a[i];ri = i;}}a[ra] = 1001;a[ri] = 1001;sort(a, a + n);if (n == 2)printf("%d %d\n", Max, Min);else {printf("%d", Max);rep(i, n - 2)printf(" %d", a[i]);printf(" %d\n", Min);}return 0;}
B-Fixed Points
Define a full arrangement. If the number at the position is the same as the serial number as the fixed point, for example, 0, 1, 2, there are three, 0, 2, and 1, there is only one. (This sequence must be arranged)
A sequence is given, where a maximum of two numbers can be exchanged at a time, and the maximum number of fixed points after the switch is obtained.
If the upper limit is reached, you do not need to switch.
Otherwise, we can find that if a number is a fixed point, there is no need for it to be exchanged, because this will not produce better results, as long as we switch the wrong number of locations.
If the number of enumerated values is incorrect, it will definitely time out.
If the position is incorrect, you can directly find the corresponding position of the number in the position. The position is definitely not correct, and you can get the fixed point increment of 1 or 2 in exchange with it.
Then, traverse the wrong number and try to switch with the corresponding position. If the increment is 2 after the switch, you can exit directly; otherwise, the increment is 1.
The complexity is O (n ).
Code:
/** Author: illuz
* Blog: http://blog.csdn.net/hcbbt* File: b.cpp* Create Date: 2013-09-20 23:43:46* Descripton: b */#include
#define rep(i, n) for (int i = 0; i < (n); i++)const int MAXN = 100100;int a[MAXN], n, cnt;int rec[MAXN], r;int main() {scanf("%d", &n);rep(i, n) {scanf("%d", &a[i]);if (a[i] == i)cnt++;elserec[r++] = i;}int cg = r > 0 ? 1 : 0;rep(i, r)if (a[a[rec[i]]] == rec[i]) {cg = 2;break;}printf("%d\n", cnt + cg);return 0;}
C-Alice and Bob
A and B play games. A starts each time. game rules: find two numbers in A set. Their difference is not in the set, and then add them to the set. If they cannot be found, they will lose.
The simulation definitely times out...
So I simulated the sample on the paper and a simple sample, and found that there was only 1-Max in the last set.
Later I thought it would be different if they were all multiples of 10.
Then the question is displayed, and The gcd of all values is obtained. The final game operand is max/gcd-n, and then you will know who wins.
Code:
/** Author: illuz
* Blog: http://blog.csdn.net/hcbbt* File: c.cpp* Create Date: 2013-09-21 00:41:09* Descripton: gcd */#include
#include
#include using namespace std;#define rep(i, n) for (int i = 0; i < (n); i++)typedef long long LL;LL g, t;LL gcd(LL a, LL b) {if(b == 0)return a;return gcd(b, a % b);}int main() {int n;LL Max = 0;cin >> n;rep(i, n) {cin >> t;if (i)g = gcd(g, t);elseg = t;Max = max(Max, t);}LL cnt = Max / g - n;if (cnt % 2 == 0) printf("Bob\n");else printf("Alice\n");return 0;}