codeforces 1500分题目(持续更新)

题解等有空再补(拖延症晚期)

580A - Kefa and First Steps

https://codeforces.com/contest/580/problem/A

最长连续子序列的在线处理。

#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<stdio.h>
#include<string>
#include<math.h>
#include<map>
#include<stdlib.h>
#ifndef NULL
#define NULL 0
#endif
#define maxn 10000
using namespace std;

typedef long long ll;
int a[100001],f[100001];
int main()
{
	int n,maxx=0,ans=1;
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 1; i < n; i++) {
		if (a[i] < a[i - 1]) {
			maxx = max(maxx, ans);
			ans = 0;
		}
		ans++;
	}
	if (a[n - 1] >= a[n - 2])
		maxx = max(ans, maxx);
	cout << maxx << endl;
	return 0;
}

580C - Kefa and Park

https://codeforces.com/contest/580/problem/C

前向星+dfs。
连续的m个点就return
到达新的节点且该节点无可以继续访问的节点,就认为它是叶子节点。

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

typedef struct vec* nd;
struct vec{
	int e;
	nd next;
};
nd head[100010];
int n,m,f[100010],visit[100010],ans=0,cnt;
int read()
{
	int f=1,x=0;char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')	f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}
void dfs(int s)
{
	nd p=head[s];
    int flag=0;
	for(;p!=0;p=p->next)
		if(!visit[p->e]){
			int t=cnt;
			flag=1;
			f[p->e]?cnt+=1:cnt=0;
			if(cnt>m){
				cnt=t;
				continue;
			}
			visit[p->e]=1;
			dfs(p->e);
			cnt=t;
			visit[p->e]=0;
		}
    if(flag==0)
        ans++;
}
void close(nd p)
{
	if(p==0)
		return ;
	close(p->next);
	delete(p);
}
int main()
{
	n=read(),m=read();
	memset(head,0,sizeof(head));
	for(int i=1;i<=n;i++)
		f[i]=read();
	for(int i=1;i<n;i++){
		nd p=new(vec ),ip=new(vec );
		int s=read();p->e=read();
		p->next=head[s];
		head[s]=p;
    	ip->e=s;s=p->e;
		ip->next=head[s];
		head[s]=ip;
	}
	cnt=f[1];
    visit[1]=1;
	dfs(1);
	cout<<ans<<endl;
	for(int i=1;i<=n;i++)
		close(head[i]);
	return 0;
}

479C - Exams

http://codeforces.com/problemset/problem/479/C

排序后贪心

/**********************
479C
**********************/
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;

typedef long long ll;
struct vec{
	int a,b;
}p[5010];

ll read()
{
	ll f=1,x=0;char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c<='9'&&c>='0'){
		x=x*10+c-'0';
		c=getchar();
	}
	return x;
}
bool cmp(vec x,vec y)
{
	if(x.a!=y.a)
		return x.a<y.a;
	return x.b<y.b;
}
int main()
{
	int time=0,n=read();
	for(int i=0;i<n;i++)
		p[i].a=read(),p[i].b=read();
	sort(p,p+n,cmp);
	time=p[0].b;
	for(int i=0;i<n;i++)
		if(time>p[i].b)
			time=p[i].a;
		else
			time=p[i].b;
	cout<<time<<endl;
}

550C - Divisibility by Eight

http://codeforces.com/problemset/problem/550/C

神仙题目,暴力枚举,大于四位的数只需要考虑后三位即可。
列入数abcd,可分解为(a000+bcd)%8==a000%8+bcd%8,则必然a000整除8。
在s前加两个零,以保证枚举的bcd是从1位到3位数字。

/**********************
550C
**********************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;

typedef long long ll;
string s1,s;
int a[110]={0};

ll read()
{
	ll f=1,x=0;char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c<='9'&&c>='0'){
		x=x*10+c-'0';
		c=getchar();
	}
	return x;
}
int main()
{
	cin>>s;
	s="00"+s;
	int len=s.length();
	for(int i=0;i<len;i++)
		for(int j=i+1;j<len;j++)
			for(int k=j+1;k<len;k++){
				int x=(s[i]-'0')*100+(s[j]-'0')*10+s[k]-'0';
				if(x%8==0){
					printf("YES\n%d\n",x);
					return 0;
				}
			}
	cout<<"NO"<<endl;
	return 0;
}

545C - Woodcutters

http://codeforces.com/problemset/problem/545/C

特判一下两个端点和只有一棵树的时候。
贪心。

/**********************
545C
**********************/
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;

typedef long long ll;
struct vec
{
	int a,b	;
} p[1000010];

ll read()
{
	ll f=1,x=0;char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c<='9'&&c>='0'){
		x=x*10+c-'0';
		c=getchar();
	}
	return x;
}
int main()
{
	int ans=0,n;
	n=read();
	for(int i=0;i<n;i++)
		p[i].a=read(),p[i].b=read();
	if(n==1)
		ans=1;
	else
		ans=2;
	for(int i=1;i<n-1;i++)
		if(p[i].a-p[i-1].a>p[i].b)
			ans++;
		else if(p[i+1].a-p[i].a>p[i].b){
			ans++;
			p[i].a+=p[i].b;
		}
	cout<<ans<<endl;
	return 0;
}

1152B - Neko Performs Cat Furrier Transform

https://codeforces.com/contest/1152/problem/B

先异或
再加一
问循环这两步,是否有可能让x变成2n-1的数.

#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#ifndef NULL
#define NULL 0
#endif
const int MAX = 10100;
const int MaxTableSize = 50100;
using namespace std;

long long a[] = { 0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535,131071,262143,524287,1048575,2097151,4194303,8388607,16777215,33554431,67108863,134217727,268435455,536870911,1073741823,2147483647,4294967295,8589934591,17179869183,34359738367,68719476735,137438953471,274877906943,549755813887,1099511627775,2199023255551,4398046511103,8796093022207,17592186044415,35184372088831,70368744177663,140737488355327,281474976710655,562949953421311,1125899906842623,2251799813685247,4503599627370495,9007199254740991,18014398509481983,36028797018963967,72057594037927935,144115188075855871,288230376151711743,576460752303423487,1152921504606846975,2305843009213693951,4611686018427387903,9223372036854775807 };
long long num[41];

long long read()
{
	int x = 0, f = 1; char c = getchar();
	while (c<'0' || c>'9') {
		if (c == '-')
			f = -1;
		c = getchar();
	}
	while (c >= '0'&&c <= '9') {
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * f;
}
int main()
{
	long long x = read(),tot=0,ans=0;
	while (1) {
		int pos1 = lower_bound(a, a + 63, x) - a, tp=-1;
		if (a[pos1] == x) {
			cout << ans << '\n';
			break;
		}
		for (long long t = x, i = 0; t > 0; i++, t >>= 1)
			if (!(t & 1))
				tp = i;
		tp++,ans++;
		num[++tot] = tp;
		x ^= a[tp];
		pos1 = lower_bound(a, a + 63, x) - a;
		if (a[pos1] == x) {
			cout << ans << '\n';
			break;
		}
		x++,ans++;
	}
	for (int i = 1; i <= tot; i++)
		cout << num[i] << ' ';
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值