csp201409

相邻数对

思路

简单模拟

实现

#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int n;
int a[N];
int ans;
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    sort(a,a+n);
    for(int i=1;i<n;i++)
    {
        if(a[i]-a[i-1]==1) ans++;
    }
    cout<<ans;
}

画图

思路

因为数据量很小,所以暴力也可以做出来

实现

#include<bits/stdc++.h>
using namespace std;
const int N = 105;
int f[N][N];
int n,ans;
void add(int x1,int y1,int x2,int y2)
{
    for(int i = x1;i<x2;i++)
    {
        for(int j = y1;j<y2;j++)
        {
            f[i][j] = 1;
        }
    }
}
int main()
{
    int x1,y1,x2,y2;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
        add(x1,y1,x2,y2);
    }
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            if(f[i][j]) ans++;
        }
    }
    cout<<ans;
}

补充

补充一下扫描线做法:
扫描线是一种求矩形面积并/周长并的好方法。
假设有一条扫描线从一个图形的下方扫向上方(或者左方扫到右方),那么通过分析扫描线被图形截得的线段就能获得所要的结果。该过程可以用线段树进行加速。

来自洛谷题解

#include<bits/stdc++.h>
#define ll long long 
using namespace std;
const int maxn = 1e6 + 10;
int N, cnt = 0;
ll int x1, x2, y1, y2, X[maxn << 1];
struct scanline{
	ll l, r, h;
	int mark;//保存权值
	bool operator <(const scanline &te)const{
		return h  < te.h;
	} 
}line[maxn << 1];

struct tree{
	int l, r, sum;
	//sum:被完全覆盖的次数
	//len:区间被截的长度 
	ll len;	
}st[maxn << 2];

void build(int o, int l, int r){
	st[o].l	= l, st[o].r = r;
	st[o].len = 0;
	st[o].sum = 0;
	if(l == r)return;
	int mid = (l + r) >> 1;
	build(o << 1, l, mid);
	build(o << 1|1, mid + 1, r);
}
void pushup(int o){
	int l = st[o].l, r = st[o].r;
	if(st[o].sum)st[o].len = X[r + 1] - X[l];//已经被覆盖了,更新长度 
	else{st[o].len = st[o << 1].len + st[o << 1 |1].len;}
}
void change(int o, ll L, ll R, int c){
	int l = st[o].l, r = st[o].r;
	//l,r代表o这个点的范围,L,R意义是待修改的区间
	if( X[r + 1] <= L || R <= X[l] )return;
	//当右区间 + 1小于左区间, 
	if(L <= X[l] && X[r + 1] <= R){
		st[o].sum += c;
		pushup(o);
		return;
	}
	change(o << 1, L, R, c);
	change(o << 1 | 1, L, R, c);
	pushup(o);
}
int main(){
	scanf("%d",&N);
	for(int i = 1; i <= N; i++){
		cin>>x1>>y1>>x2>>y2;
		X[2 * i - 1] = x1, X[2 * i] = x2;
		line[2 * i - 1] = (scanline){ x1, x2, y1, 1};
		line[2 * i] = (scanline){ x1, x2, y2, -1};
	}
	N = N << 1;
	sort(line + 1, line + N +1);
	sort(X + 1, X + N + 1);
	int tot = unique(X + 1, X + N + 1) - (X + 1);
	build(1, 1 , tot - 1);
	//右端点的对应关系已经被修改了,我们用 
	//[1,tot - 1]描述的是[x[1],x[tot]] 
	ll ans = 0;
	for(int i = 1; i < N; i++){
		change(1, line[i].l,line[i].r,line[i].mark);
		ans += st[1].len *(line[i + 1].h - line[i].h);
	}
	cout<<ans;
}

字符串匹配

思路

字符串匹配(STL版 = =)

实现

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin >> s;
    int flag;
    cin >>flag;
    int n;
    cin >> n;
    while(n--)
    {
        string str;
        cin >> str;
        if(flag && str.find(s) != -1)
            cout << str << endl;
        if(!flag)
        {
            string l = str;
            transform(s.begin(), s.end(), s.begin(), ::toupper);
            transform(str.begin(),str.end(), str.begin(), ::toupper);
            if(str.find(s) != -1) 
                cout << l << endl;
        }
    }
}

补充

补充一下KMP做法
Pecoo的知乎文章
Pecco老师是神!很多算法都讲的通俗易懂

最优配餐

思路

多源bfs,将所有起点加入队列即可

实现

#include <bits/stdc++.h>
using namespace std;
int n,m,k,d;
const int N = 1005;
int a[N][N],dist[N][N];
int cus[N * N][3];
queue<pair<int,int> > q;
typedef long long ll;

int dx[4] = {-1,0,1,0},dy[4] = {0,1,0,-1};
void bfs()
{
    while(q.size())
    {
        auto t = q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int x = t.first+dx[i],y = t.second+dy[i];
            if(a[x][y]!=0)
            {
                if(dist[x][y]>dist[t.first][t.second]+1)
                {
                    dist[x][y] = dist[t.first][t.second]+1;
                    q.push({x,y});
                }
            }
        }
    }
}
int main()
{
    memset(dist, 0x3f, sizeof(dist));
    cin>>n>>m>>k>>d;
    int x,y;
    for(int i=0;i<m;i++)
    {
        scanf("%d %d",&x,&y);
        q.push({x,y});
        dist[x][y] = 0;
    }
    for(int i=0;i<k;i++)
    {
        scanf("%d %d %d",&cus[i][0],&cus[i][1],&cus[i][2]);
    }
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
            a[i][j] = 1;
    }
    for(int i=0;i<d;i++)
    {
        scanf("%d %d",&x,&y);
        a[x][y] = 0;
    }
    bfs();
    ll ans = 0;
    while(k--)
    {
        ans += dist[cus[k][0]][cus[k][1]] * cus[k][2];
       // cout<<cus[k][2]<<endl;
       // cout<<dist[cus[k][0]][cus[k][1]]<<endl;
    }
    cout<<ans;
}

拼图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值