http://codeforces.com/contest/545
好水的一场div2.
A. Toy Cars
a[i][j] = 1,表示第i辆车坏掉了,=2 表示第j辆车坏掉了,=3表示i和j都坏掉了。求有几辆车是好的
#include <bits/stdc++.h>
#define foreach(v,i) for(__typeof((v).begin()) i=(v).begin();i!=(v).end();++i)
const int MAXN = 110;
using namespace std;
vector<int> vi;
bool vis[MAXN];
int n;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.cpp","r",stdin);
freopen("out.cpp","w",stdout);
#endif // ONLINE_JUDGE
int x;
scanf("%d",&n);
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
scanf("%d",&x);
if(x==1) vis[i]=true;
if(x==2) vis[j]=true;
if(x==3){
vis[i]=vis[j]=true;
}
}
}
int res=0;
for(int i=1;i<=n;++i){
res+=vis[i];
if(!vis[i]) vi.push_back(i);
}
cout<<n-res<<endl;
foreach(vi,i){
cout<<*i<<" ";
}
return 0;
}
B. Equidistant String
给两个由0,1构成的a,b串,求c串使得c到a的距离=c到b的距离。
距离为每个位置上数字差的绝对值的和
#include<bits/stdc++.h>
const int MAXN = 100010;
using namespace std;
char a[MAXN],b[MAXN];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.cpp","r",stdin);
freopen("out.cpp","w",stdout);
#endif // ONLINE_JUDGE
scanf("%s%s",a,b);
int n=strlen(a);
int cnt=0;
for(int i=0;i<n;++i){
if(a[i]!=b[i]) cnt++;
}
if(cnt&1){
puts("impossible");
return 0;
}
string s;
cnt=0;
for(int i=0;i<n;++i){
if(a[i]!=b[i]){
if(cnt&1) s+=a[i];
else s+=b[i];
cnt++;
}
else s+=a[i];
}
cout<<s<<endl;
return 0;
}
C. Woodcutters
n棵树并排在同一排,每棵树有个横坐标和高度,伐木工人砍树可以让树向左倒或者向右倒(必须有足够的区间长度),求最多可以砍倒多少棵树
最左边的树一定要向左倒,结果不会更差,然后对于每一棵树,能砍就砍
#include <bits/stdc++.h>
const int MAXN = 100010;
const int INF = 0x3f3f3f3f;
using namespace std;
int x[MAXN],h[MAXN];
int len[MAXN];
int n;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.cpp","r",stdin);
freopen("out.cpp","w",stdout);
#endif // ONLINE_JUDGE
scanf("%d",&n);
x[0]=0;
for(int i=1;i<=n;++i){
scanf("%d%d",&x[i],&h[i]);
len[i]=x[i]-x[i-1];
}
int cnt=n>=2 ? 2:1;
for(int i=2;i<=n-1;++i){
if(len[i]>h[i]) cnt++;
else if(len[i+1]>h[i]){
len[i+1]-=h[i];
cnt++;
}
}
cout<<cnt<<endl;
return 0;
}
D. Queue
n个人在超市排队,每个人都有一个服务时间,当一个人的等待时间大于服务时间,这个人就不买东西了。然后可以随意交换每个人的位置,求最多可以让多少个人买东西
排个序,枚举就行了
#include <bits/stdc++.h>
typedef long long LL;
const int MAXN = 100010;
using namespace std;
int n;
LL t[MAXN];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.cpp","r",stdin);
freopen("out.cpp","w",stdout);
#endif // ONLINE_JUDGE
while(scanf("%d",&n)==1) {
for(int i=1; i<=n; ++i) {
scanf("%I64d",&t[i]);
}
sort(t+1,t+n+1);
LL sum=0;
int cnt=0;
for(int i=1; i<=n; ++i) {
if(sum>t[i]){
cnt++;
}else sum+=t[i];
}
cout<<n-cnt<<endl;
}
return 0;
}
E. Paths and Trees
一个带权无向图,求一个新图G’=(V,E’),使得源点s到新图各个点的最短距离等于在原图中的最短距离,输出边权值最小的新图
dijkstra,求一次单源最短路,在距离相同的情况下,维护边权值小的
#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define mk make_pair
#define clr(a,b) memset(a,b,sizeof(a))
#define foreach(v,i) for(__typeof((v).begin()) i=(v).begin();i!=(v).end();++i)
const int MAXN = 300010;
const LL INF = ~0uLL>>1;
using namespace std;
set<LL> si;
LL d[MAXN];
int p[MAXN];
bool vis[MAXN];
int n,m;
struct HeapNode{
LL d;
int u;
bool operator<(const HeapNode& rhs)const{
return d>rhs.d;
}
};
struct Edge{
int to,next;
LL w;
int id;
}edge[MAXN<<1];int head[MAXN],tot;
void init()
{
tot=0;
clr(head,0xff);
}
void addedge(int u,int v,LL w,int id)
{
edge[tot].to=v;
edge[tot].w=w;
edge[tot].id=id;
edge[tot].next=head[u];
head[u]=tot++;
}
void dijkstra(int s){
priority_queue<HeapNode> q;
for(int i=1;i<=n;++i) d[i]=INF;
d[s]=0;
clr(vis,false);
clr(p,0xff);
q.push((HeapNode){0,s});
while(!q.empty()){
HeapNode x=q.top();q.pop();
int u=x.u;
if(vis[u]) continue;
vis[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
LL w=edge[i].w;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
p[v]=i;
q.push((HeapNode){d[v],v});
}
else if(d[v]==d[u]+w&&edge[p[v]].w>w){
p[v]=i;
}
}
}
}
void print(int s)
{
LL ans=0;
for(int i=1;i<=n;++i){
if(p[i]>=0) ans+=edge[p[i]].w;
}
cout<<ans<<endl;
for(int i=1;i<=n;++i){
if(p[i]>=0) si.insert(edge[p[i]].id);
}
foreach(si,i){
printf("%d ",*i);
}
puts("");
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.cpp","r",stdin);
freopen("out.cpp","w",stdout);
#endif // ONLINE_JUDGE
int u,v,s;
LL w;
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=m;++i){
scanf("%d%d%I64d",&u,&v,&w);
addedge(u,v,w,i);
addedge(v,u,w,i);
}
scanf("%d",&s);
dijkstra(s);
print(s);
return 0;
}