跳到正文

const_chiyin

1052579

在洛谷打开
资料、奖项与版本

资料

编号
1052579
抓取

开动脑筋 积极献策 忠于实践 付诸行动 排列

n个人选m个 顺序有不同算不同情况

P^n_m = n!/(n-m)!

组合

n个人选m个 顺序不同算同种

C^n_m = n!/m!(n-m)!

计算机5大组成 cpu = 运算器 + 控制器 运算器 控制器 协调各部件 存储器 内ROM只读存储器 RAM随机存储器(内存条) 断电数据消失 外。。。 输入设备 输出设备

冯诺依曼 2禁制 计算机之父 存储原理

图灵 图灵奖 英国数学家 AI之父 出书*2

~(取反) !》 算数运算(*/% +-) 》 (<< >>)》 关系运算(< <= > >= = !=) 》 位运算(& ^ !) > 逻辑运算符(&& ||)

排序算法

C++
#include<bits/stdc++.h>
using namespace std;
int n, dep;
int maxh = 0, ans = 1;

struct tree{
	int fa,l,r,h,w;
}tre[1e6+5];

int find(int whi){
	
}

int main(){
	tree.h = 1;
	cin >> n;
	memset(tre.l, -1, 1e6+5);
	memset(tre.r, -1, 1e6+5);
	for(int i = 1; i <= n; i++){
		cin >> tre[i].w;
	}
	for(int i = 1; i <= n; i++){
		cin >> tre[i].l >> tre[i].r;
		if(tre[i].l > -1) {
			tre[tre[i].l].fa = i;
			tre[tre[i].l].h = tre[i].h + 1;
			maxh = tre[i].h + 1;
		}
		if(tre[i].r > -1) {
			tre[tre[i].r].fa = i;
			tre[tre[i].r].h = tre[i].h + 1;
			maxh = tre[i].h + 1;
		}
	}
	for(int i = h; i <= n; i++){
		if(tre[i].h = maxh){
			dep = i;
		}
	}
	
	return 0;
} 
C++
#include<bits/stdc++.h>
using namespace std;
int n,m;
const int N = 1e5+5;
int p[N], a[N], op, x, v;
void bian(int y, int vv){
	for(int i = 1; i <= n; i++){
		if(p[i]==y){
			a[i]^=vv;
			bian(p[i],vv);
		}
	}
	return ;
}
int main(){
	cin >> n >> m;
	for(int i = 2; i <= n; i++) scanf("%d", &p[i]);
	for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for(int i = 1; i <= m; i++){
		cin >> op >> x >> v;
		if(op==1){
			a[i]^=v;
			bian(x,v);
		}else{
			int y=x;
			while(p[y]!=1){
				a[y]^=v;
				y=p[y];
			}
			a[1]^=v;
		}
	} 
	for(int i = 1; i <= n; i++){
		cout << a[i] << ' ';
	}
	return 0;
}

3805

C++
#include<bits/stdc++.h>
using namespace std;
int T,n,n_1=1,n_0=0,cnt_0=0,cnt_1=0;  const int N = 1e6+5;  int cun0[N], cun1[N];  string s;
inline int zh(int nn,string ss){
	int sum=0;
	for(int i = 1; i <= n; i++) if(ss[i-1]=='1')sum+=pow(2,i);
	return sum;
} 
inline int zy_1(int x, int dpp){
	int al = dpp;  al = al<<1;
	int s=pow(2,x-1);  al^=s;
	al = al+pow(2,n+1)- pow(2,x);
	return al;
}
inline int zy_0(int x, int dpp){
	int al = dpp;  al = al<<1;
	int s = pow(2,x-1);  al ^= s;return al;
}
signed main(){
	cin >> T;
	while(T--){
		cin >> n >> s;
		int dp = zh(n,s); int dp1 = dp, dp0 = dp; int n1 = pow(2,n+1)-1;
		int cun00=1; for(int i = 1; i <= n; i++) if(s[i-1] == '0'){cun0[cun00] = i; cun00++;}
		int cun11=1; for(int i = 1; i <= n; i++)  if(s[i-1] == '1'){cun1[cun11] = i; cun11++;}
		while(dp1!=n1){
			int sum=0;
			if(dp1&1==0) {dp1+=1; cnt_1++;}
			else{dp1 = zy_1(cun0[cun00-sum]-sum, dp1); cnt_1+=cun0[cun00-sum]-sum; sum++;}
		}
		while(dp0!=0){
			int sum = 0;
			if(dp0&1==1){dp0-=1; cnt_0++;}
			else{ dp0 = zy_0(cun1[cun00-sum]-sum, dp0);  cnt_0+=cun0[cun00-sum]-sum;  sum++;}
		}
		cout << (dp0<dp1 ? dp0:dp1) << '\n';
	}
	return 0;
} 
C++
#include<bits/stdc++.h>
using namespace std;
int T;
const int N = 1e6+5;
int n, b[N];
string s;
int main(){
	cin >> T;
	while(T--){
		int cnt0=0,cnt1=0, us01=0, us10=0; 
		cin >> n >> s;
		for(int i = 0; i < n; i++){ if(s[i]=='0') {b[i+1] = 0; cnt0++;} else {b[i+1] = 1; cnt1++;}}
		while(cnt0>0){//全转为1 
			if(b[1]==0) { b[1] = 1; cnt0--; us01++;}
			else{
				int zz;
				for(int i = n; i >= 2; i--){
					if(b[i]==0){
						b[i]==1;
						cnt0--;
						for(int j = 0; j < i; j++){
							b[j] = b[j+1];
						}
						b[i] = b[0];
						us01+=i;
					}
				}
			}
		}
		while(cnt0>0){//全转为0 10
			if(b[1]==1) { b[1] = 0; cnt1--; us10++;}
			else{
				int zz;
				for(int i = n; i >= 2; i--){
					if(b[i]==1){
						b[i]==0;
						cnt1--;
						for(int j = 0; j < i; j++){
							b[j] = b[j+1];
						}
						b[i] = b[0];
						us10+=i;
					}
				}
			}
		}
		cout << (us01<us10 ? us01 : us10) << '\n';
	}
	return 0;
} 
代码
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6+5;
int n, T, _0, _1; string s;
int check0(int n,string s){
	int sum = 0, l=0, cnt=0, _=0;
	for(int i = 0; i < n; i++) if(s[i]=='0') _++;
	while(_--){
		if(s[l]=='0') {
			cnt++;
			s[l] = '1'; 
		}
		else{
			bool b = true;
			while(b){
				if(s[n-1]=='0'){
					cnt+=(n-sum);
					l++;
					b=false; 
					sum++;
				}
				n--;
			}
		}
	} 
	return cnt;
}
int check1(int n,string s){
	int sum = 0, l=0, cnt=0, _=0;
	for(int i = 0; i < n; i++) if(s[i]=='1') _++;
	while(_--){
		if(s[l]=='1') {
			cnt++;
			s[l] = '0'; 
		}
		else{
			bool b = true;
			while(b){
				if(s[n-1]=='1'){
					cnt+=(n-sum);
					l++;
					b=false; 
					sum++;
				}
				n--;
			}
		}
	} 
	return cnt;
}
signed main(){
	cin >> T;
	while(T--){
		cin >> n >> s;
		_0 = check0(n,s);
		_1 = check1(n,s);
		cout << min(_0, _1) << '\n';
	}
	return 0;
} 
C++
int read() {
  int x = 0, w = 1;
  char ch = 0;
  while (ch < '0' || ch > '9') {if (ch == '-') w = -1;ch = getchar();}
  while (ch >= '0' && ch <= '9') {x = x * 10 + (ch - '0');ch = getchar();}
  return x * w;
}

void write(int x) {
  if (x < 0) { x = -x;putchar('-');}
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
C++
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
int aa[N];
char cc[N];
pair<int,int> p;
queue<p> que;
struct Node{
	int a, cha;
	char c;
	bool b = false;
	int index;
	Node *next;
};
struct Sort{
	int index;
	int cha;
	Sort *next;
};
int n;
void insert(int i, int value, char x, Node *p){
	Node *node = new Node;
	node->a = value;
	node->c = x;
	node->index = i;
	node->next = p->next;
	p->next = node;
}
void insert2(int index, int cha, Node *p){
	Sort *sor = new Sort;
	sor->index = index;
	sor->cha = cha; 
	sor->next = p->next;
	p->next = sor;
}
int main(){
	cin >> n;
	Node* head = new Node;
    head->a = 0xcfcfcfcf;
    head->c = 'L';
    head->next = nullptr;
    Sort* head2 = new Sort;
    head2->next = NULL;
	for(int i = 1; i <= n; i++) cin >> aa[i];
	for(int i = 1; i <= n; i++) cin >> cc[i];
	for(int i = n; i >= 1; i--){
		insert(i, aa[i], cc[i], head);
		if(cc[i-1] != cc[i]) head->next->b = true, head->next->cha = abs(aa[i]-aa[i-1]);
	}
	Node* position=head->next;
	for(int i = 1; i <= n; i++){
		if(position->b){
			Sort* s_p = head2;
			while(s_p->next!=NULL){
				if(position->cha > s_p->next->cha){
					Sort* sor = new Sort;
					sor->cha = position->cha;
					sor->index = position->index;
					sor->next = s_p->next;
					s_p->next = sor;
				} 
				break;
			}
		}
		position = position->next;
	}
	
	return 0;
}

static

C++
#include<bits/stdc++.h>
using namespace std;
int n, m;
int a;
int force_up[105], force_off[105];
queue<int> q;
int f[1<<10];
int main(){
	cin >> n >> m;
	for(int i = 1; i <= m; i++) force_off[i] = (1<<n)-1;
	for(int i = 1; i <= m; i++) 
		for(int j = 1; j <= n; j++){
			cin >> a;
			if(a==1) force_up+=(1<<j-1);
			if(a==-1) force_off-=(1<<j-1);
		}
	f[1<<n-1] = 0;
	while(!q.empty()){
		if(f[0]!=-1) break;
		int now = q.front();
		q.pop();
		
	}
	cout << f[0];
	return 0;
}
C++
#include<bits/stdc++.h>
using namespace std;
int p, x, y, q;
const int N = 5e5+5;
int nxt[N];
void put_in(int now, int next){
	if(nxt[now]==N-1) nxt[now] = next;
	else nxt[next] = nxt[now], nxt[now] = next;
}
void put_out(int l, int r){
	int l1 = l, l2 = r, r1 = r, r2 = l;
	int cnt1 = 0, cnt2 = 0;
	while(l1!=r1 && l2!=r2){
		cnt1+=nxt[l1];
		cnt2+=nxt[l2];
		l1 = nxt[l1];
		l2 = nxt[l2];
	}
	if(l1==r1){
		cnt1-=r;
		nxt[l] = r;
		cout << cnt1 << endl;
		return ;
	}
	nxt[r] = l;
	cnt2-=l;
	cout << cnt2 << endl;
	return ;
}
int main(){
	fill(nxt, nxt+N, N-1);
	cin >> q;
	for(int i = 1; i <= q; i++){
		cin >> p;
		if(p==1){
			cin >> x;
			put_in(x, i);
		}else{
			cin >> x >> y;
			put_out(x, y);
		}
	}
	return 0;
} 
C++
#include<bits/stdc++.h>
using namespace std;
const int N = 200005;
vector<int> nxt[N];
int fa[N], col[N]; 
int n, m, s;
int t, f;
bool ok;
void dfs(int u){
    col[u] = 1;
    for(int v : nxt[u]){
        if(ok) return;
        if(col[v] == 0){
            fa[v] = u;
            dfs(v);
        }else if(col[v] == 2){
            ok = true;
            t = v;
            f = u;
            return;
        }
    }
    col[u] = 2;
}
vector<int> get_path(int x){
    vector<int> res;
    while(x != -1){
        res.push_back(x);
        x = fa[x];
    }
    reverse(res.begin(), res.end());
    return res;
}
int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> m >> s;
    for(int i = 1; i <= m; ++i){
        int u, v; cin >> u >> v;
        nxt[u].push_back(v);
    }
    fa[s] = -1;
    dfs(s);
    if(!ok){
        cout << "Impossible\n";
        return 0;
    }
    vector<int> p1 = get_path(t);
    vector<int> p2 = get_path(f);
    p2.push_back(t);
    cout << "Possible\n";
    cout << p1.size() << '\n';
    for(int x : p1) cout << x << ' ';
    cout << '\n';
    cout << p2.size() << '\n';
    for(int x : p2) cout << x << ' ';
    cout << '\n';
    return 0;
}

还没有收录这位用户的文章。