codeforces 368(div 2)前三题

Posted by Cww97 on 2016-08-22

版权声明:本文为博主原创文章,未经博主允许不得转载。原文所在http://blog.csdn.net/cww97 https://blog.csdn.net/cww97/article/details/52270554
第一题
很多人没看到G,被坑了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<cstdio>
#include<iostream>
int main(){
int n,m;
char x,y;
scanf("%d%d\n",&n,&m);
bool ok = 1;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
scanf("%c%c",&x,&y);
if (x=='C'||x=='M'||x=='Y')ok = 0;
}
}
if (ok)puts("#Black&White");
else puts("#Color");
}

第二题,
a[i]=1表示i位置是个仓库
然后按边的权值排序,找一条边两个点一个是仓库一个不是仓库的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1e5 + 10;
struct Edge{
int x,y,w;
Edge(){}
Edge(int _x,int _y,int _w):x(_x),y(_y),w(_w){}
bool operator < (Edge a)const {
return w < a.w ;
}
}edges[N];
int a[N];

int main(){
int n,m,k,x,y,z;
scanf("%d%d%d",&n,&m,&k);
for (int i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
edges[i]=Edge(x,y,z);
}
if (k==0)puts("-1");
else {
memset(a,0,sizeof(a));
for (int i=1;i<=k;i++){
scanf("%d",&x);a[x]=1;
}
sort(edges+1,edges+m+1);
int ans = -1;
for (int i=1;i<=m;i++){
if (a[edges[i].x]^a[edges[i].y]){
ans = edges[i].w;break;
}
}
printf("%d\n",ans);
}
return 0;
}

第三题

找勾股数。。。分类讨论就行了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<cstdio>
#include<iostream>
using namespace std;

int main(){
long long n;
cin >> n;
if (n==1 ||n==2 )puts("-1");
else {
if (n&1){
n *= n ;
printf("%I64d %I64d\n",(n-1)>>1,(n+1)>>1);
}
else {
n >>= 1 ;
printf("%I64d %I64d\n",n*n-1,n*n+1);
}
}
return 0;
}

第四题

听说是主席树

可持久化数据结构
承旭老师说rope可以秒,

http://blog.csdn.net/iamzky/article/details/38348653

暴力修改版本的,,,TLE的死死的

这里写图片描述
这里写图片描述
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
const int N=1e5 + 10;
struct node{
int pos,num;
string a;
node(){}
node(int x,int y,string st){
pos=x,num=y,a=st;
}
}bo;
rope <node> *book[N];
int ans[N];

int main(){
//freopen("fuck.in","r",stdin);
int n,m,q,x,y,ord,tot=0;
scanf("%d%d%d",&n,&m,&q);
string zero = "";
for (int i=1;i<=m;i++)zero+="0";
book[0]=new rope<node>();
book[0]->push_back(node(0,0,""));
for (int i=1;i<=n;i++)
book[0]->push_back(node(i,0,zero));
for (int ti=1;ti<=q;ti++){
book[ti] = new rope <node>(*book[ti-1]);
ans[ti] = ans[ti-1];
scanf("%d%d",&ord,&x);
if (ord==1){
scanf("%d",&y);
bo = book[ti]->at(x);
if (bo.a[y-1]=='0'){
bo.a[y-1]='1';
bo.num++;
book[ti]->replace(x,bo);
ans[ti]++;
}
}else if (ord==2){
scanf("%d",&y);
bo = book[ti]->at(x);
if (bo.a[y-1]=='1'){
bo.a[y-1]='0';
bo.num--;
book[ti]->replace(x,bo);
ans[ti]--;
}
}else if (ord==3){
bo = book[ti]->at(x);
for (int i=0;i<m;i++){
bo.a[i]=(bo.a[i]=='1')?'0':'1';
}
bo.num = m - bo.num;
book[ti]->replace(x,bo);
bo = book[ti]->at(0);
ans[ti] += m - bo.num*2 ;
}else if (ord==4){
book[ti]= book[x];
ans[ti] = x;
}
printf("%d\n",ans[ti]);
}
return 0;
}

承旭老师改出个优雅的修改个版本的
MLE了

这里写图片描述
这里写图片描述
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
const int N=1e5+5,M=1e3+5;
struct node{
int num;
bool a[M],re;
node(){}
}bo;
rope <node> *book[N];
int ans[N];
int main(){
//freopen("fuck.in","r",stdin);
int n,m,q,x,y,ord;
book[0]=new rope<node>();
scanf("%d%d%d",&n,&m,&q);
for (int i=0;i<n;i++)
book[0]->insert(i,bo);
for (int ti=1;ti<=q;ti++){
scanf("%d%d",&ord,&x);
if (ord==4){
book[ti] = new rope <node>(*book[x]);
ans[ti]=ans[x];
}else{
book[ti] = new rope <node>(*book[ti-1]);
ans[ti]=ans[ti-1];
}
if (ord==1){
scanf("%d",&y);
bo=book[ti]->at(x-1);
if (bo.a[y-1]^bo.re==0){
bo.a[y-1]^=1;
bo.num ++;
book[ti]->replace(x-1,bo);
ans[ti]++;
}
}
if (ord==2){
scanf("%d",&y);
bo=book[ti]->at(x-1);
if (bo.a[y-1]^bo.re==1){
bo.a[y-1]^=1;
bo.num --;
book[ti]->replace(x-1,bo);
ans[ti]--;
}
}
if (ord==3){
bo=book[ti]->at(x-1);
bo.re^=1;
ans[ti]+=m - bo.num*2;
bo.num = m - bo.num;
book[ti]->replace(x-1,bo);
}
printf("%d\n",ans[ti]);
}
return 0;
}

搞出个树套树

TLE,要疯了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
const int N=1e5 + 10,M=10e3+5;
rope <rope<bool>* > *book[N];
rope<bool> *tmp,*re[N];
rope<int> *num[N];
int ans[N];
int main(){
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
int n,m,q,x,y,ord;
book[0]=new rope<rope<bool>* >();
tmp=new rope<bool>();
num[0]=new rope<int>();
re[0]=new rope<bool>();
scanf("%d%d%d",&n,&m,&q);
for (int i=0;i<m;i++)
tmp->insert(i,false);
for (int i=0;i<n;i++){
tmp=new rope<bool>(*tmp);
book[0]->insert(i,tmp);
num[0]->insert(i,0);
re[0]->insert(i,false);
}
for (int ti=1;ti<=q;ti++){
scanf("%d%d",&ord,&x);
// printf("%d: %d %d\n",ti,ord,x);
if (ord==4){
book[ti] = new rope <rope<bool>* >(*book[x]);
num[ti] = new rope<int>(*num[x]);
re[ti] = new rope<bool>(*re[x]);
ans[ti]=ans[x];
}else{
book[ti] = new rope <rope<bool>* >(*book[ti-1]);
num[ti] = new rope<int>(*num[ti-1]);
re[ti] = new rope<bool>(*re[ti-1]);
ans[ti]=ans[ti-1];
}
if (ord==1){
scanf("%d",&y);
tmp=book[ti]->at(x-1);
bool f=tmp->at(y-1),flag=re[ti]->at(x-1);
if (f^flag==0){
tmp=new rope<bool>(*tmp);
f^=1;
tmp->replace(y-1,f);
book[ti]->replace(x-1,tmp);
int nu=num[ti]->at(x-1);
nu++;
num[ti]->replace(x-1,nu);
ans[ti]++;
}
}
if (ord==2){
scanf("%d",&y);
tmp=book[ti]->at(x-1);
bool f=tmp->at(y-1),flag=re[ti]->at(x-1);
if (f^flag==1){
tmp=new rope<bool>(*tmp);
f^=1;
tmp->replace(y-1,f);
book[ti]->replace(x-1,tmp);
int nu=num[ti]->at(x-1);
nu--;
num[ti]->replace(x-1,nu);
ans[ti]--;
}
}
if (ord==3){
re[ti]->replace(x-1,re[ti]->at(x-1)^1);
//printf("%d %d\n",bo.num,bo.cnt);
ans[ti]+=m - num[ti]->at(x-1)*2;
num[ti]->replace(x-1,m-num[ti]->at(x-1));
}
/* for (int i=0;i<n;i++){
tmp=book[0]->at(i);
for (int j=0;j<m;j++){
bool f=tmp->at(j);
printf("%d ",f^re[0]->at(i));
}
puts("");
}*/
printf("%d\n",ans[ti]);
}
return 0;
}

丧心病狂的STL
http://tieba.baidu.com/p/2215390730