您现在的位置是:首页 > 编程 > 

重要的话说三遍 (2016)

2025-07-20 21:51:58
重要的话说三遍 (2016) 重要的话说三遍 (2016)这道超级简单的题目没有任何输入。你只需要把这句很重要的话 —— “I’m gonna WI!”——连续输出三遍就可以了。注意每遍占一行,除了每行的回车不能有任何多余字符。提交代码代码语言:javascript代码运行次数:0运行复制#include<bits/stdc++.h> using namespace std; in

重要的话说三遍 (2016)

重要的话说三遍 (2016)

这道超级简单的题目没有任何输入。

你只需要把这句很重要的话 —— “I’m gonna WI!”——连续输出三遍就可以了。

注意每遍占一行,除了每行的回车不能有任何多余字符。

提交代码

代码语言:javascript代码运行次数:0运行复制
#include<bits/stdc++.h>
using namespace std;

int main(){
	printf("I'm gonna WI!\n");
	printf("I'm gonna WI!\n");
	printf("I'm gonna WI!\n");
	return 0;
}

每天一道算法题

最长连续不重复子序列

给定一个长度为 n 的整数序列,请出最长的不包含重复的数的连续区间,输出它的长度。

输入格式 第一行包含整数 n。

第二行包含 n 个整数(均在 0∼105 范围内),表示整数序列。

输出格式 共一行,包含一个整数,表示最长的不包含重复的数的连续区间的长度。

数据范围 1≤n≤105 输入样例: 5 1 2 2 5 输出样例:

提交代码

代码语言:javascript代码运行次数:0运行复制
#include<bits/stdc++.h>
using namespace std;
ct int  = 100010;

int a[], s[];
int n, res;

int main()
{
    cin >> n;
    for (int i = 0; i < n; ++ i) cin >> a[i];
    for (int i = 0, j = 0; i < n; ++ i)
    {
        s[a[i]] ++; // 记录下a[i]出现的次数
        while(s[a[i]] > 1)   // 一点碰见两个重复的元素后
        {  
            s[a[j]] --;  // 这里要主要的一点是这个算法是没有回溯的
                         // 不要被for循环里面的条件误导以为会回溯、
                         // 现在遇到两个相同的元素了
                         // !!! 现在是这个算法最厉害的地方 
                         // 这个j代表的是 j可以到达最左的地方 所以在j左边的
                         // 元素的个数就需要都-- 这点很妙
                         // 每次求的是 j到i之间的符合条件的最大值
            j ++;        // 然后j++
        }
        res = max(res, i - j + 1);  // 这个res的含义是 在i这个位置、
        // 可以达到的符合题目条件的最大长度
    }
    cout << res;
    return 0;
}
代码语言:javascript代码运行次数:0运行复制
import java.io.*;
import java.util.*;

public class Main
{
    public static void main(String[] args) throws IOException{
        Scanner in = new Scanner(System.in);
        int n = ();
        int [] a = new int [n + 10];
        int [] s = new int [n + 10];
        int res = 0;
        for (int i = 0; i < n; ++ i) a[i] = ();
        for (int i = 0, j = 0; i < n; ++ i)
        {
            s[a[i]] ++;
            while(s[a[i]] > 1)
            {
                s[a[j]] --;
                j ++;
            }
            res = (res, i - j + 1);
        }
        println(res);
    }
}
合并集合

一共有 n 个数,编号是 1∼n,最开始每个数各自在一个集合中。

现在要进行 m 个操作,操作共有两种:

M a b,将编号为 a 和 b 的两个数所在的集合合并,如果两个数已经在同一个集合中,则忽略这个操作; Q a b,询问编号为 a 和 b 的两个数是否在同一个集合中; 输入格式 第一行输入整数 n 和 m。

接下来 m 行,每行包含一个操作指令,指令为 M a b 或 Q a b 中的一种。

输出格式 对于每个询问指令 Q a b,都要输出一个结果,如果 a 和 b 在同一集合内,则输出 Yes,否则输出 o。

每个结果占一行。

数据范围 1≤n,m≤105 输入样例: 4 5 M 1 2 M 4 Q 1 2 Q 1 Q 4 输出样例: Yes o Yes

提交代码

代码语言:javascript代码运行次数:0运行复制
#include<iostream>
using namespace std;

ct int  = 100010;
int n, m;
int p[];

int find(int x)                 // 到x的祖先节点
{
    if (p[x] != x) p[x] = find(p[x]);
    return p[x];
}

int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; ++i) p[i] = i;
    
    while (m--)
    {
        char op;
        int a, b;
        scanf (" %c%d%d", &op, &a, &b);
        if (op == 'M') p[p[find(a)]] = find(b);        // 让a的祖先节点指向b的祖先节点
        else
        {
            if (find(a) == find(b)) puts("Yes");
            else puts("o");
        }
    }
    return 0;
}
代码语言:javascript代码运行次数:0运行复制
import java.io.*;

public class Main
{
    static int  = 100010;
    static int n, m;
    static int [] p = new int [];
    
    static int find(int x)
    {
        if (p[x] != x) p[x] = find(p[x]);
        return p[x];
    }
    
    public static void main(String[] args) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader (System.in));   
        String [] str = reader.readLine().split(" ");
        n = Integer.parseInt(str[0]);
        m = Integer.parseInt(str[1]);
        
        for (int i = 1; i <= n; ++ i) p[i] = i;
        while (m -- > 0)
        {
            String op;
            int a, b;
            str = reader.readLine().split(" ");
            op = str[0];
            a = Integer.parseInt(str[1]);
            b = Integer.parseInt(str[2]);
            if (("M")) p[find(a)] = find(b);
            else 
            {
                if (find(a) == find(b)) println("Yes");
                else println("o");
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2022-12-29,如有侵权请联系 cloudcommunity@tencent 删除数据算法intstatic集合

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1146038.html

相关标签:无
上传时间: 2025-07-19 13:34:50
留言与评论(共有 16 条评论)
本站网友 数字小区
18分钟前 发表
b; scanf (" %c%d%d"
本站网友 很便宜
26分钟前 发表
// 现在遇到两个相同的元素了 // !!! 现在是这个算法最厉害的地方 // 这个j代表的是 j可以到达最左的地方 所以在j左边的 // 元素的个数就需要都-- 这点很妙 // 每次求的是 j到i之间的符合条件的最大值 j ++; // 然后j++ } res = max(res
本站网友 胶原蛋白好处
9分钟前 发表
输入格式 第一行包含整数 n
本站网友 翼校通家校互动平台
14分钟前 发表
最开始每个数各自在一个集合中
本站网友 米库
12分钟前 发表
表示最长的不包含重复的数的连续区间的长度
本站网友 广州市越秀区
1分钟前 发表
如果两个数已经在同一个集合中
本站网友 龙骧虎步
9分钟前 发表
&m); for (int i = 1; i <= n; ++i) p[i] = i; while (m--) { char op; int a
本站网友 凯德广场
0秒前 发表
提交代码代码语言:javascript代码运行次数:0运行复制#include<bits/stdc++.h> using namespace std; int main(){ printf("I'm gonna WI!\n"); printf("I'm gonna WI!\n"); printf("I'm gonna WI!\n"); return 0; }每天一道算法题最长连续不重复子序列给定一个长度为 n 的整数序列
本站网友 什么是正确的价值观
7分钟前 发表
m; static int [] p = new int []; static int find(int x) { if (p[x] != x) p[x] = find(p[x]); return p[x]; } public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader (System.in)); String [] str = reader.readLine().split(" "); n = Integer.parseInt(str[0]); m = Integer.parseInt(str[1]); for (int i = 1; i <= n; ++ i) p[i] = i; while (m -- > 0) { String op; int a
本站网友 李成明
28分钟前 发表
表示最长的不包含重复的数的连续区间的长度
本站网友 薛中行
5分钟前 发表
重要的话说三遍 (2016) 重要的话说三遍 (2016)这道超级简单的题目没有任何输入
本站网友 同仁妇科医院
27分钟前 发表
原始发表:2022-12-29
本站网友 prefetch
18分钟前 发表
数据范围 1≤n≤105 输入样例: 5 1 2 2 5 输出样例: 提交代码代码语言:javascript代码运行次数:0运行复制#include<bits/stdc++.h> using namespace std; ct int = 100010; int a[]
本站网友 韩式假体隆胸痛吗
1分钟前 发表
m≤105 输入样例: 4 5 M 1 2 M 4 Q 1 2 Q 1 Q 4 输出样例: Yes o Yes提交代码代码语言:javascript代码运行次数:0运行复制#include<iostream> using namespace std; ct int = 100010; int n
本站网友 前期物业管理方案
5分钟前 发表
如有侵权请联系 cloudcommunity@tencent 删除前往查看数据算法intstatic集合