Codeforces 51A Cheaterius's Problem

本文解析了CodeForces平台上的一道A级题目——Cheaterius的问题,该题涉及通过旋转来判断2x2矩阵(代表魔法护身符)的相似性,并统计不同堆的数量。文章详细介绍了算法实现思路,包括如何通过旋转比较矩阵以及使用哈希表记录和统计相似矩阵。

Cheaterius's Problem
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Cheaterius is a famous in all the Berland astrologist, magician and wizard, and he also is a liar and a cheater. One of his latest 

inventions is Cheaterius' amulets! They bring luck and wealth, but are rather expensive. Cheaterius makes them himself. 

The technology of their making is kept secret. But we know that throughout long nights Cheaterius glues together domino 

pairs with super glue to get squares 2 × 2which are the Cheaterius' magic amulets!

That's what one of Cheaterius's amulets looks like

After a hard night Cheaterius made n amulets. Everyone of them represents a square 2 × 2, every quarter contains 1 to 6 

dots. Now he wants sort them into piles, every pile must contain similar amulets. Two amulets are called similar if they can be 

rotated by 90, 180 or 270 degrees so that the following condition is met: the numbers of dots in the corresponding quarters 

should be the same. It is forbidden to turn over the amulets.

Write a program that by the given amulets will find the number of piles on Cheaterius' desk.

Input

The first line contains an integer n (1 ≤ n ≤ 1000), where n is the number of amulets. Then the amulet's descriptions are 

contained. Every description occupies two lines and contains two numbers (from 1 to 6) in each line. Between every pair of 

amulets the line "**" is located.

Output

Print the required number of piles.

Sample Input

Input
4
31
23
**
31
23
**
13
32
**
32
13
Output
1
Input
4
51
26
**
54
35
**
25
61
**
45
53
Output
2

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
bool t[70000000];
int s[4];
int main()
{
    int n,u,i,x,y,tem,ans;
    char r[5];
    while(scanf("%d",&n)!=EOF)
    {
        for(i=11111111; i<=66666666; i++)
            t[i]=0;
        for(u=1; u<=n; u++)
        {
            scanf("%d%d",&x,&y);
            if(u!=n) scanf("%s",r);
            s[0]=x;
            s[1]=x%10*10+y%10;
            s[2]=y%10*10+y/10;
            s[3]=y/10*10+x/10;
            sort(s,s+4);
            tem=s[0]*1000000+s[1]*10000+s[2]*100+s[3];
            t[tem]=1;
        }
        ans=0;
        for(i=11111111; i<=66666666; i++)
            if(t[i]) ans++;
        printf("%d\n",ans);
    }
    return 0;
}


### 关于 Codeforces 上二项装箱问题 #### 二项装箱问题概述 二项装箱问题是经典的组合优化问题之一,在计算机科学领域具有重要意义。该类问题通常涉及将一组不同大小的对象放入固定容量的容器中,目标是最小化使用的容器数量[^1]。 对于特定平台上的挑战实例,如Codeforces中的二项装箱问题,其核心在于设计高效算法来解决这一NP难问题。尽管找到最优解可能非常复杂,但存在多种启发式方法可以提供接近最佳的结果,并且这些方法能够在合理的时间内执行完毕。 #### 解决方案策略 一种常见的处理方式是采用贪心算法,即总是尝试把当前最大的未分配物品放置到第一个能够容纳它的箱子中;如果没有任何现有箱子能放下这件物品,则创建一个新的箱子用于装载它。这种方法简单易懂,但在某些情况下可能会导致次优解。 更复杂的近似算法包括首次适应下降(First Fit Decreasing, FFD),此技术首先按照降序排列所有项目尺寸,之后应用首次适配原则(FD)。FFD已被证明能在多项式时间内给出不超过理想最小值11/9倍数加四的解法质量保证[^2]。 此外还有其他高级求解途径比如动态规划、分支限界以及遗传算法等,它们各自适用于不同的应用场景并提供了不同程度上的性能改进。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<int> items(n); for(int i = 0; i < n; ++i){ cin >> items[i]; } sort(items.begin(), items.end(), greater<int>()); const int bin_capacity = 1000; // 假设每个bin的最大容量为1000单位体积 vector<int> bins; for(auto item : items){ bool placed = false; for(auto& b : bins){ if(b + item <= bin_capacity){ b += item; placed = true; break; } } if(!placed){ bins.push_back(item); } } cout << "Minimum number of bins required is: " << bins.size(); } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值