wenbao与随机

-----------------

https://loj.ac/problem/526

比赛的时候各种知识搞都没有ac,题解说利用二分图,看到大佬用random过,佩服!

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <ctime>
 4 #include <vector>
 5 using namespace std;
 6 
 7 #define ll long long
 8 ll a[555], b[555];
 9 
10 ll g(ll x, ll y){
11     return !y ? x : g(y, x%y);
12 }
13 
14 bool ok(ll x, ll y){
15     if(g(x, y) == 1 && g(x+1LL, y+1LL) == 1) return false;
16     return true;
17 }
18 
19 int main(){
20     int n, num1 = 0, num2 = 0;
21     scanf("%d", &n);
22     for(int i = 0; i < n; ++i){
23         scanf("%lld", &a[i]);
24         if(a[i]&1) num1++;
25         else num2 ++;
26     }
27     int ma = max(num1, num2);
28     while(clock() < 0.9*CLOCKS_PER_SEC){
29         random_shuffle(a, a+n);
30         for(int k = 0; k < 2; ++k){
31             int num = 0;
32             for(int i = 0; i < n; ++i){
33                 bool flag = true;
34                 for(int j = 0; j < num; ++j){
35                     if(!ok(a[i], b[j])){
36                         flag = false;
37                         break;    
38                     }
39                 }
40                 if(flag) b[num++] = a[i];
41             }
42             ma = max(ma, num);
43         }
44     }
45     printf("%d
", ma);
46     return 0;
47 }

CLOCKS_PER_SEC表示每秒钟包含的系统时间单位数

延时循环

 1 void waiting(void) {  
 2     cout<<"Enter the delay time, in seconds: ";  
 3     float secs;  
 4     cin>>secs;  
 5     clock_t delay=secs * CLOCKS_PER_SEC; //convert to clock ticks  
 6     cout<<"startinga
";  
 7     clock_t start=clock();  
 8     while (clock()-start < delay)//wait until time elapses  
 9         ;//note the semicolon  
10     cout<<"done a
";  
11     cin.get();  
12 }  

--------------------------------------------------------------------------

http://codeforces.com/contest/862/problem/C

n个数(不超过1e6)的异或为x

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 const int maxn = 1e6;
 5 int a[maxn], n, x;
 6 bool ok(){
 7     random_shuffle(a, a+maxn);
 8     int tmp = x;
 9     for(int i = 0; i < n-1; ++i){
10         tmp ^= a[i];
11     }
12     a[n-1] = tmp;
13     for(int i = 0; i < n-1; ++i){
14         if(tmp == a[i]) return false;
15     }
16     return true;
17 }
18 int main(){
19     scanf("%d%d", &n, &x);
20     if(n == 1) {
21         printf("YES
%d
", x);
22     }else if(n == 2){
23         if(x == 0) printf("NO
");
24         else printf("YES
0 %d
", x);
25     }else{
26         for(int i = 0; i < maxn; ++i){
27             a[i] = i;
28         }
29         printf("YES
");
30         while(!ok());
31         for(int i = 0; i < n; ++i){
32             printf("%d%c", a[i], " 
"[i == n-1]);
33         }
34     }
35     return 0;
36 }

--------------------------------------------------------------------

--------------------------------------------------------------------

只有不断学习才能进步!

原文地址:https://www.cnblogs.com/wenbao/p/7472426.html