首頁 資料庫 mysql教程 Codeforces Round #FF (Div. 2) 题解

Codeforces Round #FF (Div. 2) 题解

Jun 07, 2016 pm 03:31 PM
round

比赛链接:http://codeforces.com/contest/447 A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output DZY has a hash table with p buckets, numbered from 0 to p ?-?1 . He

比赛链接:http://codeforces.com/contest/447

A. DZY Loves Hash

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

DZY has a hash table with p buckets, numbered from 0 to p?-?1. He wants to insert n numbers, in the order they are given, into the hash table. For the i-th number xi, DZY will put it into the bucket numbered h(xi), where h(x) is the hash function. In this problem we will assume, that h(x)?=?x mod p. Operation a mod b denotes taking a remainder after division a by b.

However, each bucket can contain no more than one element. If DZY wants to insert an number into a bucket which is already filled, we say a "conflict" happens. Suppose the first conflict happens right after the i-th insertion, you should output i. If no conflict happens, just output -1.

Input

The first line contains two integers, p and n (2?≤?p,?n?≤?300). Then n lines follow. The i-th of them contains an integer xi (0?≤?xi?≤?109).

Output

Output a single integer — the answer to the problem.

Sample test(s)

input

10 5
0
21
53
41
53
登入後複製

output

4
登入後複製

input

5 5
0
1
2
3
4
登入後複製

output

-1
登入後複製

链接:http://codeforces.com/contest/447

题意:找出hash时第一次产生冲突的位置。

解题思路:用一个数组表示是否存放有hash之后的元素,0表示这个位置还没有使用过,1表示这个位置上有hash之后的元素(即产生了冲突)。

代码:

#include <cstdio>
#include <cstring>

const int MAXN = 305;
int a[MAXN], p, n, ans = -1;

int main()
{
    bool flag = true;
    scanf("%d%d", &p, &n);
    for(int i = 0; i <br>


<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
</p>
<p>
B. DZY Loves Strings</p>
<p>
</p>
<p>
time limit per test</p>
1 second
<p>
</p>
<p>
memory limit per test</p>
256 megabytes
<p>
</p>
<p>
input</p>
standard input
<p>
</p>
<p>
output</p>
standard output

<p>
</p>
<p>DZY loves collecting special strings which only contain lowercase letters. For each lowercase letter<span> </span><span><em>c</em></span><span> </span>DZY
 knows its value<span> </span><span><em>w</em><sub><em>c</em></sub></span>.
 For each special string<span> </span><span><em>s</em>?=?<em>s</em><sub>1</sub><em>s</em><sub>2</sub>...<span> </span><em>s</em><sub>|<em>s</em>|</sub></span><span> </span>(<span>|<em>s</em>|</span><span> </span>is
 the length of the string) he represents its value with a function<span> </span><span><em>f</em>(<em>s</em>)</span>, where</p>
<center><img  src="/static/imghw/default1.png" data-src="/inc/test.jsp?url=http%3A%2F%2Fespresso.codeforces.com%2F47c9783b69409ca6ade8e93f7d51bed11f430539.png&refer=http%3A%2F%2Fblog.csdn.net%2Fu010084308%2Farticle%2Fdetails%2F37758201" class="lazy" alt="Codeforces Round #FF (Div. 2) 题解" ></center>
<p>Now DZY has a string<span> </span><span><em>s</em></span>. He wants to
 insert<span> </span><span><em>k</em></span><span> </span>lowercase letters into this string in order to get the largest possible value of the resulting
 string. Can you help him calculate the largest possible value he could get?</p>

<p>
</p>
<p>
Input</p>
<p>The first line contains a single string<span> </span><span><em>s</em> (1?≤?|<em>s</em>|?≤?10<sup>3</sup>)</span>.</p>
<p>The second line contains a single integer<span> </span><span><em>k</em> (0?≤?<em>k</em>?≤?10<sup>3</sup>)</span>.</p>
<p>The third line contains twenty-six integers from<span> </span><span><em>w</em><sub><em>a</em></sub></span><span> </span>to<span> </span><span><em>w</em><sub><em>z</em></sub></span>.
 Each such number is non-negative and doesn't exceed<span> </span><span>1000</span>.</p>

<p>
</p>
<p>
Output</p>
<p>Print a single integer — the largest possible value of the resulting string DZY could get.</p>

<p>
</p>
<p>
Sample test(s)</p>
<p>
</p>
<p>
</p>
<p>
input</p>
<pre class="brush:php;toolbar:false">abc
3
1 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
登入後複製

output

41
登入後複製

Note

In the test sample DZY can obtain "abcbbc", value?=?1·1?+?2·2?+?3·2?+?4·2?+?5·2?+?6·2?=?41.


链接:http://codeforces.com/contest/447/problem/B

题意:在给出的字符串中增加k个字符,求可以得到的最大权值和。

解题思路:找出最大的位权,将k个有最大位权的字符放到原字符串的末尾,求权值和。ps:答案可能会超出int,要用long long

代码:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s;
    int k, a[27], imax = -1;
    cin >> s >> k;
    for(int i = 0; i > a[i];
        if(a[i] > imax)
        {
            imax = a[i];
        }
    }
    long long ans = 0;
    int len = s.length();
    for(int i = 0; i <br>

<p>
</p>
<p>
C. DZY Loves Sequences</p>
<p>
</p>
<p>
time limit per test</p>
1 second
<p>
</p>
<p>
memory limit per test</p>
256 megabytes
<p>
</p>
<p>
input</p>
standard input
<p>
</p>
<p>
output</p>
standard output

<p>
</p>
<p>DZY has a sequence<span> </span><span><em>a</em></span>, consisting of<span> </span><span><em>n</em></span><span> </span>integers.</p>
<p>We'll call a sequence<span> </span><span><em>a</em><sub><em>i</em></sub>,?<em>a</em><sub><em>i</em>?+?1</sub>,?...,?<em>a</em><sub><em>j</em></sub></span><span> </span><span>(1?≤?<em>i</em>?≤?<em>j</em>?≤?<em>n</em>)</span><span> </span>a
 subsegment of the sequence<span> </span><span><em>a</em></span>. The value<span> </span><span>(<em>j</em>?-?<em>i</em>?+?1)</span><span> </span>denotes
 the length of the subsegment.</p>
<p>Your task is to find the longest subsegment of<span> </span><span><em>a</em></span>,
 such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.</p>
<p>You only need to output the length of the subsegment you find.</p>

<p>
</p>
<p>
Input</p>
<p>The first line contains integer<span> </span><span><em>n</em> (1?≤?<em>n</em>?≤?10<sup>5</sup>)</span>.
 The next line contains<span> </span><span><em>n</em></span><span> </span>integers<span> </span><span><em>a</em><sub>1</sub>,?<em>a</em><sub>2</sub>,?...,?<em>a</em><sub><em>n</em></sub> (1?≤?<em>a</em><sub><em>i</em></sub>?≤?10<sup>9</sup>)</span>.</p>

<p>
</p>
<p>
Output</p>
<p>In a single line print the answer to the problem — the maximum length of the required subsegment.</p>

<p>
</p>
<p>
Sample test(s)</p>
<p>
</p>
<p>
</p>
<p>
input</p>
<pre class="brush:php;toolbar:false">6
7 2 3 1 5 6
登入後複製

output

5
登入後複製

Note

You can choose subsegment a2,?a3,?a4,?a5,?a6 and change its 3rd element (that is a4) to 4.

链接:http://codeforces.com/contest/447/problem/C

题意:从一串数字中选出一个子串,可以改变子串中一个数字的值得到一个新的子串,求最大的递增新子串的长度。

解题思路:

将原数组分割成递增的子串,记录下每个子串的开始和结束位置,以及长度。接下来要分几种情况讨论:1.相邻的两个子串改变一个数字之后,可以合并形成新的递增子串,2.相邻的3个子串,中间子串长度为1,改变中间的数字后可以形成新的递增子串,3.相邻的子串不能合并形成新的递增子串,但是可以在原串的基础上,得到一个长度增加1的新的递增子串(在子串开头位置前有数字,或是结束位置后有数字)。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 100010;
int a[MAXN];
struct P
{
    int l, len, r;
};
P p[MAXN];
int n;

int main()
{
    memset(p, 0, sizeof(p));
    scanf("%d", &n);
    int t = 0;
    for(int i = 0; i  a[p[i - 1].r - 1] + 1 ||
           a[p[i].l + 1] > a[p[i - 1].r] + 1)
        {
            ans = max(ans, p[i].len + p[i - 1].len);
        }
        if(i >= 2 && 1 == p[i - 1].len &&
           a[p[i].l] > a[p[i - 2].r + 1])
        {
            ans = max(ans, p[i].len + p[i - 2].len + 1);
        }
     //   printf("%d \n", p[i].len);
    }
    printf("%d\n", ans);
    return 0;
}
</algorithm></cstring></cstdio>
登入後複製


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1670
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1274
29
C# 教程
1256
24
如何用PHP的round()函數除以四捨五入 如何用PHP的round()函數除以四捨五入 Mar 21, 2023 pm 04:32 PM

round() 函數是PHP數字格式化函式庫中一個非常實用的函數,可以將浮點數四捨五入到指定的小數位數。但是,由於PHP的除法運算可能會出現無限小數或精度遺失的問題,因此對除數進行四捨五入也很必要。接下來,我們會詳細講解如何使用PHP的round()函數進行除以四捨五入。

php中的round是什麼意思 php中的round是什麼意思 Mar 10, 2023 am 10:04 AM

在php中,round的意思為“四捨五入”,是一個內建函數,作用是將浮點數轉換為整數;該函數可以對浮點數進行四捨五入,並返回一個float類型的整數值,語法“round(number, precision,mode);」。

MySQL中如何使用ROUND函數截取小數位數 MySQL中如何使用ROUND函數截取小數位數 Jul 13, 2023 pm 09:21 PM

MySQL中如何使用ROUND函數截取小數位數在MySQL中,可以使用ROUND函數來截取小數的位數。 ROUND函數可以把一個數字四捨五入到指定的小數位數。以下將為您詳細介紹ROUND函數的使用方法,並提供程式碼範例。語法:ROUND(X,D)X表示要四捨五入的數字,D表示要保留的小數位數。使用ROUND函數截取小數位數的範例:假設有一個表格名為produc

top10數字貨幣交易所排名 top10數字貨幣交易所排名 Mar 14, 2025 pm 05:36 PM

2024年十大數字資產交易平台推薦,助您安全便捷地進入加密貨幣世界!本文基於交易量、安全性、用戶體驗及手續費等多維度指標,評選出十家優秀平台(排名不分先後),涵蓋現貨、合約等多種交易類型,並針對新手和專業投資者提供不同選擇。 選擇平台需考慮自身投資目標、風險承受能力、經驗水平及平台安全性和客戶支持等因素。 本文僅供參考,數字資產投資高風險,請謹慎決策,並諮詢專業人士。

選擇合適的滑鼠為筆記本配備 選擇合適的滑鼠為筆記本配備 Jan 02, 2024 pm 09:54 PM

筆記本配什麼滑鼠回最好是配上無線滑鼠。 1.無線滑鼠不會有線纏繞在一起的問題,操作更加方便。 2.配備無線滑鼠可避免電纜雜亂的場面,且移動時更自由。 3.無線滑鼠和筆記本之間不需要使用線纜來連接,也不會出現線材容易拔出的情況,使用體驗更佳。 4.在商務旅行等情況下,無線滑鼠更方便攜帶。滑鼠配合筆記本使用,應該選擇無線滑鼠。因為無線滑鼠不需要連接線,使用起來更方便,而且可以避免連接線的糾纏。同時,無線滑鼠的靈敏度和反應速度也比有線滑鼠更好,可以提高工作效率。如果需要長時間使用,建議選擇有充電

2025十大比特幣交易平台推薦,數位貨幣交易app新手入門推薦 2025十大比特幣交易平台推薦,數位貨幣交易app新手入門推薦 Dec 14, 2024 am 09:09 AM

本指南將深入探討 2025 年十大會員選擇、費用結構和安全措施對創建者至關重要的最值得信賴的比特幣交易平台,同時還將提供初學者的數位貨幣交易簡介。

DLC是什麼幣 DLC幣前景怎麼樣 DLC是什麼幣 DLC幣前景怎麼樣 Apr 24, 2025 pm 12:03 PM

DLC幣是基於區塊鏈的加密貨幣,旨在提供高效、安全的交易平台,支持智能合約和跨鏈技術,適用於金融和支付領域。

大專生應該選擇哪種筆記型電腦比較適合(大專生應該選擇哪種筆記型電腦比較合適) 大專生應該選擇哪種筆記型電腦比較適合(大專生應該選擇哪種筆記型電腦比較合適) Dec 30, 2023 pm 09:27 PM

大專生用什麼筆記型電腦合適遊戲本可以選擇正常的配置,如果你在大專期間購買一款基本配置的遊戲本,價格大約在五千多塊左右,像拯救者、飛行堡壘、暗影精靈等品牌都有適合的低配版。在選擇時要注意,最好選擇英特爾的CPU,因為它更穩定可靠。另外,記憶體也要選擇低配版的16GB,這樣的記憶體容量可以讓你運行任何軟體都沒有壓力。關於顯示卡,我建議選擇一款正常普通的產品,例如去年20年的1650型號。我不推薦購買今年的筆記型電腦,因為目前市面上的電腦性價比並不高。為什麼推薦選擇入門級的遊戲本呢?因為畢業後,你可能會

See all articles