EOJ Monthly 2019.3 A. 钝角三角形(大力模拟)(重载set)

题目链接

QQ 小方以前不会判断钝角三角形,现在他会了,所以他急切的想教会你。

如果三角形的三边长分别为 a, b, c (a≤b≤c),那么当满足 a2+b2<c2 且 a+b>c 的时候,这个三角形就是一个由三边长为 a, b, c 构成的钝角三角形。

单单讲给你听肯定是不够的,为了表现自己,QQ 小方现在要考考你。

现在 QQ 小方会给你一个包含 3n 个整数的集合,分别是 {2,3,4,⋯3n,3n+1} ,他想让你将这个集合里面的数分成 n 组,保证每个数都被分到其中一个组,并且每个组恰好有 3 个数。当然,你要保证每组的 3 个数作为边长所构成的三角形是一个钝角三角形。

 

赛中猜了一发结论,就是每个三元组第一个元素是2~n+1。然后每次取还没到的最小值或者最大值,然后二分一下答案就行了。

然后赛中写了一发取顺序取最小,然后炸了。就不敢写了。。。其实是离正解不远。。。

 

然后,赛后看到题解。。。太菜了。。。

  • 然后自己打了一发,发现要逆序取好一点。但是一直卡41这个点。这里卡了某个三元组无法组成三角形(可能是相对比较大的值已经被提前取了,导致C二分不出来)。一直不知道怎么解决。
  • 然后又打了一发逆序取最大值当C,二分b一下子就AC了。。。
  • 然后怎么维护是否已经取过,并且二分呢。
  • 当然是用set啦,去最大值就重载一下set的compare函数就行了。
struct cmp {
    bool operator()(int x, int y) const { return x > y; }
};
set<int, cmp> S = set<int, cmp>(cmp());

非AC代码(AC码在下面)


#include<algorithm>
#include<vector>
#include<iostream>
#include<math.h>
#include<cstring>
#include<string>
#include<stack>
#include<map>
#include<set>
#include<unordered_map>
#include<queue>
#include<assert.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>

#define qcin; ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define clr(x) memset(x,0,sizeof x)
#define fmax(x) memset(x,0x3f,sizeof x)
#define finit(x) memset(x,-1,sizeof x)
#define iio(n,m) io(n),io(m)
#define lc(p) (p<<1)
#define rc(p) ((p<<1)|1)
#define VI vector<int>

#define dis(l,r) r-l+1
#define gstr(str) scanf("%s",str)
#define glen(str) strlen(str)
using namespace std;

namespace Input{
    const int BUF = 65536;
    char buf[BUF + 1];
    char *head = buf, *tail = buf;
}
inline char inputchar(){
    using namespace Input;
    if(head == tail)
        *(tail = (head = buf) + fread(buf, 1, BUF, stdin)) = 0;
    return *head++;
}
template<class T>
inline void io(T &ret){
    ret = 0;
    char ch = inputchar();
    while((ch < '0' || ch > '9') && ch != '-')
        ch = inputchar();
    bool neg = false;
    if(ch == '-')
        neg = true, ch = inputchar();
    while(ch >= '0' && ch <= '9')
    {
        ret = ret * 10 + ch - '0';
        ch = inputchar();
    }
    if(neg)
        ret = -ret;
}

typedef long long ll;

typedef pair<int,int>pll;
const int maxn = 1e6+10;
const int mod =  1e9+7;
const ll INF = 2147483647;

typedef ll arr[maxn];
typedef char str[maxn];
void file(int x){if(x&&fopen("123.in","r")){freopen("123.in","r",stdin);}}

const long double pi = acos(-1);
const double eps=1e-10;
const double delta=0.993;

ll n,tmp,x;
ll res[maxn][4];
struct cmp {
    bool operator()(int x, int y) const { return x > y; }
};
//set<int, cmp> S = set<int, cmp>(cmp());
set<int>S;
int main(){
    file(1);
    io(n);
    for(int i=2;i<=3*n+1;i++){
        S.insert(i);
    }
    for(int i=0,cnt=2;i<n;i++,cnt++){
        res[i][0]=cnt;
        S.erase(cnt);
    }
    for(int i=n-1;~i;i--){
        auto mi=S.begin();
        //mi--;
        res[i][1]=*mi;
        S.erase(res[i][1]);
        tmp=res[i][1]*res[i][1]+res[i][0]*res[i][0];
        x=sqrt(tmp);
        int k=*S.upper_bound(x);
//        int k=*S.lower_bound(x);
        res[i][2]=k;
//        ll tmp=res[i][0]*res[i][0]+res[i][1]*res[i][1]-res[i][2]*res[i][2];
//        if(tmp>=0)return 0*puts("-1");
        S.erase(k);
    }
    for(int i=0;i<n;i++){
        printf("%lld %lld %lld\n",res[i][0],res[i][1],res[i][2]);
        ll tmp=res[i][0]*res[i][0]+res[i][1]*res[i][1]-res[i][2]*res[i][2];
        ll d=res[i][2]-res[i][1]-res[i][0];
        assert(tmp<0);
        assert(d<0);
    }
}

AC代码


#include<algorithm>
#include<vector>
#include<iostream>
#include<math.h>
#include<cstring>
#include<string>
#include<stack>
#include<map>
#include<set>
#include<unordered_map>
#include<queue>
#include<assert.h>
#include<iomanip>
#include<bitset>
#include<stdio.h>

#define qcin; ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp make_pair
#define clr(x) memset(x,0,sizeof x)
#define fmax(x) memset(x,0x3f,sizeof x)
#define finit(x) memset(x,-1,sizeof x)
#define iio(n,m) io(n),io(m)
#define lc(p) (p<<1)
#define rc(p) ((p<<1)|1)
#define VI vector<int>

#define dis(l,r) r-l+1
#define gstr(str) scanf("%s",str)
#define glen(str) strlen(str)
using namespace std;

namespace Input{
    const int BUF = 65536;
    char buf[BUF + 1];
    char *head = buf, *tail = buf;
}
inline char inputchar(){
    using namespace Input;
    if(head == tail)
        *(tail = (head = buf) + fread(buf, 1, BUF, stdin)) = 0;
    return *head++;
}
template<class T>
inline void io(T &ret){
    ret = 0;
    char ch = inputchar();
    while((ch < '0' || ch > '9') && ch != '-')
        ch = inputchar();
    bool neg = false;
    if(ch == '-')
        neg = true, ch = inputchar();
    while(ch >= '0' && ch <= '9')
    {
        ret = ret * 10 + ch - '0';
        ch = inputchar();
    }
    if(neg)
        ret = -ret;
}

typedef long long ll;

typedef pair<int,int>pll;
const int maxn = 1e6+10;
const int mod =  1e9+7;
const ll INF = 2147483647;

typedef ll arr[maxn];
typedef char str[maxn];
void file(int x){if(x&&fopen("123.in","r")){freopen("123.in","r",stdin);}}

const long double pi = acos(-1);
const double eps=1e-10;
const double delta=0.993;

ll n,tmp,x;
ll res[maxn][4];
struct cmp {
    bool operator()(int x, int y) const { return x > y; }
};
set<int, cmp> S = set<int, cmp>(cmp());
//set<int>S;
int main(){
    file(1);
    io(n);
    for(int i=2;i<=3*n+1;i++){
        S.insert(i);
    }
    for(int i=0,cnt=2;i<n;i++,cnt++){
        res[i][0]=cnt;
        S.erase(cnt);
    }
    for(int i=n-1;~i;--i){
        auto mi=S.begin();
        res[i][2]=*mi;
        S.erase(res[i][2]);
        tmp=res[i][2]*res[i][2]-res[i][0]*res[i][0];
        x=sqrt(tmp);
        if(x*x==tmp)x--;
        int k=*S.lower_bound(x);
        res[i][1]=k;
        S.erase(k);
    }
    for(int i=0;i<n;i++){
        printf("%lld %lld %lld\n",res[i][0],res[i][1],res[i][2]);
        ll tmp=res[i][0]*res[i][0]+res[i][1]*res[i][1]-res[i][2]*res[i][2];
        ll d=res[i][2]-res[i][1]-res[i][0];
        assert(tmp<0);
        assert(d<0);
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值