今天做了这两道题真的好高兴啊!!我一直知道自己很渣,又贪玩不像别人那样用功,又没有别人有天赋。所以感觉在ACM也没有学到什么东西,没有多少进步。但是今天的B题告诉我,进步虽然不明显,但是只要坚持努力的学习,改变就会默默发生啦~真的好高兴,感觉自己又有了动力!自己现在虽然还不会什么算法,都是三脚猫功夫,但是已经真切的感觉到自己已经会用一点算法的思想了,哎呀我也不知道怎么说好,总之就是看到了自己的改变。

A - Array

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

Description

Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold:

  1. The product of all numbers in the first set is less than zero ( < 0).
  2. The product of all numbers in the second set is greater than zero ( > 0).
  3. The product of all numbers in the third set is equal to zero.
  4. Each number from the initial array must occur in exactly one set.

Help Vitaly. Divide the given array.

Input

The first line of the input contains integer n (3 ≤ n ≤ 100). The second line contains n space-separated distinct integers a1, a2, ..., an(|ai| ≤ 103) — the array elements.

Output

In the first line print integer n1 (n1 > 0) — the number of elements in the first set. Then print n1 numbers — the elements that got to the first set.

In the next line print integer n2 (n2 > 0) — the number of elements in the second set. Then print n2 numbers — the elements that got to the second set.

In the next line print integer n3 (n3 > 0) — the number of elements in the third set. Then print n3 numbers — the elements that got to the third set.

The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them.

Sample Input

Input
3
-1 2 0
Output
1 -1
1 2
1 0
Input
4
-1 -2 -3 0
Output
1 -1
2 -3 -2
1 0
先来一道简单的题,题意是,给出一个数组,把数组中的数分为3个部分,要求第一个部分的乘积(我一开始真的不知道product是乘积的意思啊)小于0,第二部分乘积大于0,第三部分乘积等于0.
我的做法是:先将读入的数按> < =分在3个数组中,然后再做进一步处理。不过我看到还有一种做法,将数组读入后排序,第一个数(一定小于0) 输出,最后一个数,如果是正数输出是0输出2个负数,把剩下的输出。下面是我的方法的代码。
#include<stdio.h>
int main()
{
int a1[],a2[],a3[];
int N,i,n1 = ,n2 = ,n3 = ;
int a;
scanf("%d",&N);
for(i = ;i < N;i++)
{
scanf("%d",&a);
if(a < )a1[n1++] = a;
else if(a > )a2[n2++] = a;
else a3[n3++] = a;
}
if(n1 % == )a3[n3++] = a1[--n1];
if(n2 == )
{
a2[n2++] = a1[--n1];
a2[n2++] = a1[--n1];
}
printf("%d ",n1);
for(i = ;i < n1;i++)printf("%d ",a1[i]);
printf("\n%d ",n2);
for(i = ;i < n2;i++)printf("%d ",a2[i]);
printf("\n%d ",n3);
for(i = ;i < n3;i++)printf("%d ",a3[i]);
printf("\n");
return ;
}

B - Coach

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


Description

A programming coach has n students to teach. We know that n is divisible by 3. Let's assume that all students are numbered from 1 to n, inclusive.

Before the university programming championship the coach wants to split all students into groups of three. For some pairs of students we know that they want to be on the same team. Besides, if the i-th student wants to be on the same team with the j-th one, then the j-th student wants to be on the same team with the i-th one. The coach wants the teams to show good results, so he wants the following condition to hold: if the i-th student wants to be on the same team with the j-th, then the i-th and the j-th students must be on the same team. Also, it is obvious that each student must be on exactly one team.

Help the coach and divide the teams the way he wants.

Input

The first line of the input contains integers n and m (3 ≤ n ≤ 48, . Then follow m lines, each contains a pair of integers ai, bi (1 ≤ ai < bi ≤ n) — the pair ai, bi means that students with numbers ai and bi want to be on the same team.

It is guaranteed that n is divisible by 3. It is guaranteed that each pair ai, bi occurs in the input at most once.

Output

If the required division into teams doesn't exist, print number -1. Otherwise, print  lines. In each line print three integers xiyizi (1 ≤ xi, yi, zi ≤ n) — the i-th team.

If there are multiple answers, you are allowed to print any of them.

Sample Input

Input
3 0
Output
3 2 1 
Input
6 4
1 2
2 3
3 4
5 6
Output
-1
Input
3 3
1 2
2 3
1 3
Output
3 2 1 
就是这个题!!让我大涨信心。题意是:有一群人要组队,如果a想和b一队,b就得和a一队。给出组队愿望,打印出一组符合的组队形式,如果不能就输出-1.
我是这样想的,这样的题应该不假思索的用并查集吧,一开始我也用了,但是最后输出的时候好麻烦啊,因为,例如如果输入关系1 2 1 3 这样用并查集做f[1] = 2,f[2] = 3,f[3] = 3.输出的时候为了找到同一个一队的人就不是很方便了,而且还会有人没有发表过意见,这种情况处理起来也不是很容易(说到底还是我渣,大神们用并查集写的代码也很简单)于是我就换了一种方法,好在最大的人数只有48 3重循环也不怕!我做的改动是使得同一个队的父亲节点只有一个(暂且叫他队长好了)即f[1] = 3;f[2] = 3;f[3] = 3;然后为了处理无法组队的情况又开了一个数组q,用于记录第i个人所在的队已经有q[i]个人了,最后还是错了两次,一次是错在测试样例6 3 1 2 3 4 5 6 上,这样一组测试样例应该输出-1才对,我却输出1 2 3 4 5 6 因为我没有在最后再一次检验是否能够组队成功,一次错在 9 3 4 5 6 7 8 9上因为我一开始遇见没有意见的人就从前向后遍历找到一个没有组队的或者队里不满的就组队。这样的结果就是1 2 3 4 5 6 7 8 9显然错了,于是我又让没有意见的人先加入人数不满的队中,剩下的再自己组队。这样就过了。
另一个新发现是 CF上可以看测试数据,所以我的B题才有机会改好,但是转念一想,能看测试数据当然好,但是看了测试数句后再改代码的话难免有点亡羊补牢的感觉,也少不了拆东墙补西墙的嫌疑,就害怕会按了葫芦起了瓢,无奈测试样例毕竟有限过了也不代表方法一定就对了。但世界上又有什么是绝对正确的呢?
#include<stdio.h>
int main()
{
int q[] = {};
int f[];
int N,T,i,j,k;
scanf("%d%d",&N,&T);
int a,b;
int flag = ;
for(i = ;i <= N;i++)f[i] = i;
while(T--)
{
scanf("%d%d",&a,&b);
if(q[a] == && q[b] == )q[a] = ;
else if(q[a]== &&q[b] != )q[a] = q[b]+;
else if(q[a] != && (f[a] != b&&f[b]!= a)) q[a]++;
q[b] = q[a];
if(f[a] == a)
{
for(i = ;i <= N;i++)
{
if(f[i] == a)f[i] = b;
}
for(i = ;i <= N;i++)
{
if(f[i] == b)q[i] = q[a];
}
}
else {q[f[a]] = q[a];f[f[a]] = b;f[a] = b;}
}
for(i = ;i <= N;i++)if(q[i] > )
{flag = ;break;}
if(flag == )printf("-1\n");
else
{
for(i = ;i <= N;i++)//处理有人没有意见的情况,优先补满缺一个人的队
{
if(q[i] == )//没有意见
{
for(j = ;j <= N;j++)
{
if(j == i)continue;
if(q[j] == )//该组已经有2个人
{
q[j]=q[i] = ;
if(f[j] == j)//这个人是队长
{ for(k = ;k <= N;k++)
{
if(f[k] == j)q[k] = ;//找到队员
}
f[i] = j;
}
else {q[f[j]] = ;f[i] = f[j];}
break;
}
}
}
}
int c,num= ;
for(i = ;i <= N;i++)
{
if(q[i] == )
{
if(num == )
{num++;a = i;continue;}
if(num == )
{num++;b = i;continue;}
if(num == )
{f[a] = f[b] = i;q[a] = q[b] = q[i] = ;num = ;continue;}
}
}
for(i = ;i <= N;i++)
{
if(q[i] != ){printf("-1\n");return ;}
}
for(i = ;i <= N;i++)
{
if(f[i] == i)
{ for(j = ;j <= N;j++)
{
if(f[j] == i)
printf("%d ",j);
}
printf("\n");
}
}
}
return ;
}

递归的并查集代码是father是全局变量,最后两句是写在主函数里的维基百科讲的挺细。

int find(int x)
{
if(father[x]!=x) father[x]=find(father[x]);
return father[x];
}
scanf("%d%d",&a,&b);
a=find(a),b=find(b);father[a]=b;

OUC_Summer Training_ DIV2_#16 725的更多相关文章

  1. OUC_Summer Training_ DIV2_#13 723afternoon

    A - Shaass and Oskols Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...

  2. OUC_Summer Training_ DIV2_#12(DP1) 723

    这一次是做练习,主要了解了两个算法,最大子矩阵和,最长上升子序列. 先看题好啦. A - To The Max Time Limit:1000MS     Memory Limit:32768KB   ...

  3. OUC_Summer Training_ DIV2_#14 724

    又落下好多题解啊...先把今天的写上好了. A - Snow Footprints Time Limit:1000MS     Memory Limit:262144KB     64bit IO F ...

  4. OUC_Summer Training_ DIV2_#2之解题策略 715

    这是第一天的CF,是的,我拖到了现在.恩忽视掉这个细节,其实这一篇只有一道题,因为这次一共做了3道题,只对了一道就是这一道,还有一道理解了的就是第一篇博客丑数那道,还有一道因为英语实在太拙计理解错了题 ...

  5. OUC_Summer Training_ DIV2_#7 718

    是18号做的题啦,现在才把报告补上是以前不重视报告的原因吧,不过现在真的很喜欢写报告,也希望能写一些有意义的东西出来. A - Dragons Time Limit:2000MS     Memory ...

  6. OUC_Summer Training_ DIV2_#11 722

    企鹅很忙系列~(可惜只会做3道题T_T) A - A Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d &am ...

  7. OUC_Summer Training_ DIV2_#9 719

    其实自己只会做很简单的题,有时都不想写解题报告,觉得不值得一写,但是又想到今后也许就不会做ACM了,能留下来的东西只有解题报告了,所以要好好写,很渣的题也要写,是今后的纪念. B - B Time L ...

  8. OUC_Summer Training_ DIV2_#5

    这是做的最好的一次了一共做了4道题  嘻嘻~ A - Game Outcome Time Limit:2000MS     Memory Limit:262144KB     64bit IO For ...

  9. OUC_Summer Training_ DIV2_#4之数据结构

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26100#problem/A A - A Time Limit:1000MS     Me ...

随机推荐

  1. luogu题解 P3629 【[APIO2010]巡逻】树的直径变式

    题目链接: https://www.luogu.org/problemnew/show/P3629 分析 最近被众多dalao暴虐,这道题傻逼地调了两天才知道错哪 不过这题比较良心给你一个容易发现性质 ...

  2. CF336C-Vasily the Bear and Sequence题解--贪心

    题目链接 https://www.luogu.org/problemnew/show/CF336C 分析 一个比较妙的贪心 我们要让最后\(and\)起来的数被\(2^k\)整除且\(k\)最大,我们 ...

  3. 初学java4 编译器优化

    编译器自动转义 short/char/byte 在定义变量时,后面赋值小于边界值,即可自动转义 右侧值小于左侧范围,编译器直接强转 右侧值大于左侧范围,编译器报错 short a = 10+1; // ...

  4. jQuery获取的dom对象和原生的dom对象有何区别

    js原生获取的dom是一个对象,jQuery对象就是一个数组对象,其实就是选择出来的元素的数组集合,所以说他们两者是不同的对象类型不等价 原生DOM对象转jQuery对象 var box = docu ...

  5. iphone SprintBoard部分私有API总结(不支持iOS8)

    本文介绍iOS SrpintBoard框架的部分私有API,具体包括: 获取ios上当前正在运行的所有App的bundle id(不管当前程序是在前台还是后台都可以) 获取ios上当前前台运行的App ...

  6. vue项目前端限制页面长时间未操作超时退出到登录页

    之前项目超时判断是后台根据token判断的,这样判断需要请求接口才能得到返回结果,这样就出现页面没有接口请求时还可以点击,有接口请求时才会退出 现在需要做到的效果是:页面超过30分钟未操作时,无论点击 ...

  7. 【51nod2026】Gcd and Lcm(杜教筛)

    题目传送门:51nod 我们可以先观察一下这个$f(x)=\sum_{d|x}\mu(d) \cdot d$. 首先它是个积性函数,并且$f(p^k)=1-p \ (k>0)$,这说明函数$f( ...

  8. JavaSpring【二、IOC】

    概述: 接口及面向接口编程 接口:用于沟通的中介物的抽象,实体把自己提供给外界的方法的抽象化说明,将声明和实现分离,使其能够改变内部而不影响与外部的交互方式 面向接口编程:在结构设计中,分清层次及调用 ...

  9. 异步处理的框架Sanic的使用方法和小技巧

    Sanic是异步处理的框架,运用Sanic可以开发快速异步响应的web程序.想必大家看到这个都会比较期待和兴奋. 那么如何使用Sanic来实现快速响应呢?我们先来看一看Sanic的基本介绍. Sani ...

  10. 十九:mvc强类型声明

    落下了几节,自己很懒啊, 得找个时间补上... 1. 强类型 是指变量在定义时就已经明确指定了其类型.如: string  s; int x; 2.弱类型 赋值时才确定类型. var s; var x ...