0%

Codeforces Div 2 C 天梯 21——25

http://codeforces.com/problemset/problem/459/C 这道题看上去是构造乱搞...这种题我一般都不是很懂... 然后思考了一下,发现没有思考出来,就看了题解...发现十分的巧妙。 一个非常易于理解的事情就是如果n小于k的d次方,那么就一定不会存在答案,如果n≤k的d次方,那么这堆数中一定存在d个数成立。 问题就在于,如何找到这d个数。 题解把这个问题转化为找到前n个d位的k进制数,然后按位输出 因为不会有两个d位数完全相同,所以就不会存在两个人在d天中做同一辆车... 好妙啊...

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
#include<bits/stdc++.h>
using namespace std;
int n,k,d;
int ans[1010][1010]={};
int main()
{
cin>>n>>k>>d;
int u=1;
for(int i=1;i<=d;i++)
{
if(u>=n) break;
u*=k;
}
if(u<n)
{
cout<<-1;
return 0;
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<d;j++) ans[i][j]=ans[i-1][j];
if(i!=1)
for(int j=d-1;j>=0;j--)
{
ans[i][j]=(ans[i][j]+1)%k;
if(ans[i][j]!=0) break;
}
}
for(int i=0;i<d;i++)
{
for(int j=1;j<=n;j++)
{
cout<<ans[j][i]+1<<" ";
}
cout<<endl;
}
return 0;
}

http://codeforces.com/problemset/problem/220/A 傻逼题 排一下序然后看看有几个和原数组不一样就行了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<bits/stdc++.h>
using namespace std;
int n;
int a[100010]={};
int b[100010]={};
int u=0;
bool cmp(int x,int y)
{
return x<y;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i],b[i]=a[i];
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++)
{
if(a[i]!=b[i]) u++;
}
if(u<=2) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}
http://codeforces.com/problemset/problem/377/A 还是正难则反的思想... 设所有可以走的块数为p 然后先求一个块数为p-k的联通块,然后联通块外的都变成X就行了
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
93
94
95
96
97
98
99
100
101
102
103
104
105
#include<bits/stdc++.h>
using namespace std;
int n,m,k;
char c[510][510]={};
bool v[510][510]={};
struct pp
{
int x,y;
};
queue<pp> q;
void pa()
{
int u=0;
int o=0;
int x,y;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(c[i][j]=='.')
{
u++;
x=i;
y=j;
}
}
}
pp p;
p.x=x;
p.y=y;
q.push(p);
o++;
v[x][y]=1;
if(o==u-k) return;
while(!q.empty())
{
p=q.front();
q.pop();
pp t;
x=p.x;
y=p.y;
x++;
if(v[x][y]==0&&c[x][y]=='.')
{
t.x=x,t.y=y;
q.push(t);
o++;
v[x][y]=1;
if(o==u-k) return;
}
x--;
x--;
if(v[x][y]==0&&c[x][y]=='.')
{
t.x=x,t.y=y;
q.push(t);
o++;
v[x][y]=1;
if(o==u-k) return;
}
x++;
y++;
if(v[x][y]==0&&c[x][y]=='.')
{
t.x=x,t.y=y;
q.push(t);
o++;
v[x][y]=1;
if(o==u-k) return;
}
y--;
y--;
if(v[x][y]==0&&c[x][y]=='.')
{
t.x=x,t.y=y;
q.push(t);
o++;
v[x][y]=1;
if(o==u-k) return;
}
y++;
}
}
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>c[i][j];
}
}
pa();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(c[i][j]=='.'&&v[i][j]==0) c[i][j]='X';
cout<<c[i][j];
}
cout<<endl;
}
return 0;
}
http://codeforces.com/problemset/problem/217/A 用并查集,把所有能够互相访问的点放到一个集合中 最后答案就是集合的个数-1
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
#include<bits/stdc++.h>
using namespace std;
int n;
int x[110]={};
int y[110]={};
int fa[110]={};
int ff(int x)
{
if(x==fa[x]) return x;
fa[x]=ff(fa[x]);
return fa[x];
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>x[i]>>y[i];
fa[i]=i;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(x[i]==x[j]||y[i]==y[j])
{
fa[ff(i)]=ff(j);
}
}
}
int ans=0;
for(int i=1;i<=n;i++)
{
if(fa[i]==i) ans++;
}
cout<<ans-1;
return 0;
}
http://codeforces.com/problemset/problem/371/C 和前两天比赛的一道题好像啊... 就是二分查找验证一下就行了
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
#include<bits/stdc++.h>
using namespace std;
long long nb,ns,nc;
long long b,s,c;
long long pb,ps,pc;
long long r;
string p;
bool pa(long long x)
{
long long u;
long long ans=0;
u=b*x-nb;
if(u>0) ans+=u*pb;
u=s*x-ns;
if(u>0) ans+=u*ps;
u=c*x-nc;
if(u>0) ans+=u*pc;
if(ans>r) return 0;
return 1;
}
int main()
{
cin>>p;
cin>>nb>>ns>>nc;
cin>>pb>>ps>>pc;
cin>>r;
for(int i=0;i<p.size();i++)
{
if(p[i]=='B') b++;
if(p[i]=='S') s++;
if(p[i]=='C') c++;
}
long long z,y;
z=0ll; y=1e13;
while(z<y)
{
long long mid=(z+y)/2+1;
if(pa(mid)==1) z=mid;
else y=mid-1;
}
cout<<z<<endl;
return 0;
}