Snuke's Coloring 2-1

Snuke's Coloring 2-1 是一道编程竞赛题目,主要考察参赛者的逻辑思维和算法应用能力。题目提供时间限制和内存限制,目前已有297次提交,123次成功解决。具体内容包括题目描述、输入输出格式、样例输入输出及解题提示。

问题 H: Snuke's Coloring 2-1

时间限制: 1 Sec   内存限制: 128 MB
提交: 297   解决: 123
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

There is a rectangle in the xy-plane, with its lower left corner at (0,0) and its upper right corner at (W,H). Each of its sides is parallel to the x-axis or y-axis. Initially, the whole region within the rectangle is painted white.
Snuke plotted N points into the rectangle. The coordinate of the i-th (1≤i≤N) point was (xi,yi).
Then, he created an integer sequence a of length N, and for each 1≤i≤N, he painted some region within the rectangle black, as follows:
If ai=1, he painted the region satisfying x<xi within the rectangle.
If ai=2, he painted the region satisfying x>xi within the rectangle.
If ai=3, he painted the region satisfying y<yi within the rectangle.
If ai=4, he painted the region satisfying y>yi within the rectangle.
Find the area of the white region within the rectangle after he finished painting.

Constraints
1≤W,H≤100
1≤N≤100
0≤xi≤W (1≤i≤N)
0≤yi≤H (1≤i≤N)
W, H (21:32, added), xi and yi are integers.
ai (1≤i≤N) is 1,2,3 or 4.

输入

The input is given from Standard Input in the following format:
W H N
x1 y1 a1
x2 y2 a2
:
xN yN aN

输出

Print the area of the white region within the rectangle after Snuke finished painting.

样例输入

5 4 2
2 1 1
3 3 4

样例输出

9

提示

The figure below shows the rectangle before Snuke starts painting.

First, as (x1,y1)=(2,1) and a1=1, he paints the region satisfying x<2 within the rectangle:

Then, as (x2,y2)=(3,3) and a2=4, he paints the region satisfying y>3 within the rectangle:

Now, the area of the white region within the rectangle is 9.

题意:给一个点和四种操作方式,问最后操作结束之后,白色区域的面积是多少。
分析:一开始想开数组模拟然后求标记点,后来发现用不着,直接根据输入的数据不断缩小白色区域的范围即可,用x1,x2分别代表白色区域的上、下两边,用y1,y2分别代表左右两边。结果就很容易求了。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
 
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
struct node
{
    int x,y,a;
}p[105];
int w,h,n;
int mp[105][105]={0};
int main()
{
    int i,j,k;
    int x1=0,y1=0,x2=0,y2=0;
    scanf("%d%d%d",&w,&h,&n);
    for(i=1;i<=n;i++)
        scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].a);
    x1=w;
    y1=h;
    x2=y2=0;
 
    for(i=1;i<=n;i++)
    {
        if(p[i].a==1)
            x2=max(x2,p[i].x);
        else if(p[i].a==2)
            x1=min(x1,p[i].x);
        else if(p[i].a==3)
            y2=max(y2,p[i].y);
        else if(p[i].a==4)
            y1=min(y1,p[i].y);
    }
    //printf("%d %d %d %d\n",x1,x2,y1,y2);
    if((x1-x2)>=0 && (y1-y2)>=0)
        printf("%d\n",(x1-x2)*(y1-y2));
    else
        printf("0\n");
    return 0;
}

### C++ 实现寻找字符串在左循环移位操作后的字典序最小值 以下是实现寻找字符串在左循环移位操作后的字典序最小值的 C++ 示例代码。此算法基于 Booth 算法,其时间复杂度为 O(n)[^2]。 ```cpp #include <iostream> #include <vector> #include <string> using namespace std; // Booth's Algorithm to find the lexicographically smallest rotation int computeLexSmallestRotation(const string& s) { int n = s.size(); vector<int> f(n, -1); int k = 0; for (int j = 1; j < n; ++j) { int i = f[j - k - 1]; while (i != -1 && s[j] != s[k + i + 1]) { if (s[j] < s[k + i + 1]) { k = j - i - 1; } i = f[i]; } if (i == -1 && s[j] != s[k]) { if (s[j] < s[k]) { k = j; } f[j - k] = -1; } else { f[j - k] = i + 1; } } return k; } int main() { string s = "abacaba"; int k = computeLexSmallestRotation(s); cout << "The lexicographically smallest rotation starts at index: " << k << endl; cout << "The smallest rotation is: " << s.substr(k) + s.substr(0, k) << endl; return 0; } ``` 上述代码实现了 Booth 算法[^2],该算法用于计算字符串的所有循环移位中字典序最小的一个。通过构建一个辅助数组 `f` 和维护变量 `k`,可以在 O(n) 时间内找到字典序最小的旋转起点。 --- ### C++ 实现字符串左循环移位后的字典序最小值(暴力解法) 如果需要一个更简单的实现方法,可以使用暴力解法,尽管其时间复杂度为 O(n^2),但在小规模数据上仍然有效。 ```cpp #include <iostream> #include <string> #include <algorithm> using namespace std; string findLexSmallestRotation(const string& s) { int n = s.size(); string smallest = s; for (int i = 1; i < n; ++i) { string rotated = s.substr(i) + s.substr(0, i); if (rotated < smallest) { smallest = rotated; } } return smallest; } int main() { string s = "abacaba"; string smallest = findLexSmallestRotation(s); cout << "The lexicographically smallest rotation is: " << smallest << endl; return 0; } ``` 此代码通过遍历所有可能的左循环移位,并比较每个移位后的字符串来找到字典序最小的字符串[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值