Codeforces 939A題,B題(水題)

来源:https://www.cnblogs.com/biaobiao88/archive/2019/11/12/11846203.html
-Advertisement-
Play Games

題目鏈接:http://codeforces.com/problemset/problem/939/A A題 A. Love Triangle time limit per test 1 second memory limit per test 256 megabytes input standar ...


題目鏈接:http://codeforces.com/problemset/problem/939/A

A題

A. Love Triangle time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number fi, where 1 ≤ fi ≤ n and fi ≠ i.

We call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.

Input

The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of planes.

The second line contains n integers f1, f2, ..., fn (1 ≤ fi ≤ nfi ≠ i), meaning that the i-th plane likes the fi-th.

Output

Output «YES» if there is a love triangle consisting of planes on Earth. Otherwise, output «NO».

You can output any letter in lower case or in upper case.

Examples input Copy
5
2 4 5 1 3
output Copy
YES
input Copy
5
5 5 5 5 1
output Copy
NO
Note

In first example plane 2 likes plane 4, plane 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.

In second example there are no love triangles.

思路:題目大意就是1號喜歡2號,2號喜歡3號,3號喜歡1號,如何去表示呢?用數組來下標來表示,例如a[1]=2,表示1號喜歡2號,同理a[2] = 3,表示2號喜歡3號,a[3] = 1,表示3號喜歡1號。現在的遇到的困難是,如何去表示這三者的關係。請先看AC代碼:

#include<iostream>
using namespace std;
int n,a[5001];

int main()
{
    while(cin >> n)
    {
        int flag = 0;//設一個標記 
        for(int i = 1;i <= n;i++)
            cin >> a[i];
        for(int i = 1;i <= n;i++)
            if(a[a[a[i]]] == i)//只要有滿足條件的馬上跳出迴圈,立刻結束 
                flag = 1; 
        if(flag == 1)
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}

a[1]=2,a[2]=3,a[3]=1,這是一組滿足條件的三角戀關係,我們拿這個例子來分析。a[1]=2說明1號喜歡2號,我們馬上判斷2號喜歡的是誰,我們想要知道2號喜歡誰,把a[1]=2(1號喜歡2號)中的2號放入數組a中,即a[a[1]],因為a[1]=2,a[a[1]]等價於a[2],這表示的是2號喜歡的是誰,同理,a[2]=3,如何表示3號喜歡誰呢?再把a[a[1]]放入數組a中,即a[a[a[1]]](即為3號喜歡的是誰),判斷3號是否喜歡1號,如果是,則三者滿足三角戀的條件,否則不滿足,繼續判斷。好好理解下,理解後就不難了。

B題

題目鏈接:http://codeforces.com/problemset/problem/939/B

B. Hamster Farm time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

Dima has a hamsters farm. Soon N hamsters will grow up on it and Dima will sell them in a city nearby.

Hamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters.

Dima can buy boxes at a factory. The factory produces boxes of K kinds, boxes of the i-th kind can contain in themselves ai hamsters. Dima can buy any amount of boxes, but he should buy boxes of only one kind to get a wholesale discount.

Of course, Dima would buy boxes in such a way that each box can be completely filled with hamsters and transported to the city. If there is no place for some hamsters, Dima will leave them on the farm.

Find out how many boxes and of which type should Dima buy to transport maximum number of hamsters.

Input

The first line contains two integers N and K (0 ≤ N ≤ 1018, 1 ≤ K ≤ 105) — the number of hamsters that will grow up on Dima's farm and the number of types of boxes that the factory produces.

The second line contains K integers a1, a2, ..., aK (1 ≤ ai ≤ 1018 for all i) — the capacities of boxes.

Output

Output two integers: the type of boxes that Dima should buy and the number of boxes of that type Dima should buy. Types of boxes are numbered from 1 to K in the order they are given in input.

If there are many correct answers, output any of them.

Examples input Copy
19 3
5 4 10
output Copy
2 4
input Copy
28 3
5 6 30
output Copy
1 5
思路:題目給的數據範圍很大很大,註意用long long!!!判斷總倉鼠總數除以某個盒子的容量取餘(即%的就行),能被整除最好,說明這個盒子剛好能裝滿所有的倉鼠。這裡要設置一個很大的數,比題目所給的盒子容量最大值還要大1(我設為temp)
然後依次判斷總倉鼠數對盒子容量取模的值會不會大於temp,會的話執行接下來的操作,不會的話則跳過,具體在代碼中解釋。
AC代碼
#include<iostream>
using namespace std;
long long n,k;//註意用long long!!! 

int main()
{    
    while(cin >> n >> k)
    {
        long long temp = 1e18 + 1,flag = 0,heshu = 0,x;//long long!temp的初始值要設定好 
        for(int i = 1;i <= k;i++)//從第一個盒子開始 
        {
            cin >> x;
            if(temp > n % x)//目的是取最小餘數的那一項 
            {
                temp = n % x;//滿足條件則不斷更新臨時變數temp的值,一直到餘數temp最小為止 
                flag = i;//用flag記錄此時滿足條件的是第幾個盒子 
                heshu = n / x;//記錄所需要盒子的數目 
            }
        }
        cout << flag << " " << heshu << endl;
    }
    return 0;
}

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 通過日常的工作記錄NPM常用命令,不斷的學習,不斷總結,未完待續…… ...
  • 場景 Nginx入門簡介和反向代理、負載均衡、動靜分離理解: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102790862 Ubuntu Server 16.04 LTS上怎樣安裝下載安裝Nginx並啟動: https://b ...
  • Eureka作為服務註冊與發現的組件,Eureka2.0已經閉源了,但是本教程還是以Eureka為核心進行展開。 1、三個模塊 Spring Cloud Eureka是Spring Cloud Netflix微服務套件之一,基於Netflix Eureka做了二次封裝,主要負責完成微服務架構中的服務 ...
  • windows安裝python flask虛擬環境: 安裝虛擬環境主要是為了防止不同python版本之間衝突的問題,虛擬環境安裝的包包不會對外部真實環境產生任何作用,只會作用於虛擬環境。這樣,一個flask框架應用對應一個虛擬環境,虛擬環境不會幹擾外部真實環境,同時也不會受外部環境干擾。 1、安裝v ...
  • 以思維導圖的方式呈現: 鏈接: https://naotu.baidu.com/file/9b3e739442ffb9f5bc5c3cee2b1a3489 png圖片: 作者:流浪者 日期:2019-11-13 ...
  • Nginx 進程結構 這篇文章我們來看下 Nginx 的進程結構,Nginx 其實有兩種進程結構: 單進程結構 多進程結構 單進程結構實際上不適用於生產環境,只適合我們做開發調試使用。因為在生產環境中我們必須保持 Nginx 足夠健壯以及 Nginx 可以利用多核的一個特性,而單進程的 Nginx ...
  • 異常的概念 異常,就是不正常的意思。在生活中:醫生說,你的身體某個部位有異常,該部位和正常相比有點不同,該部位的功能將受影響.在程式中的意思就是:指的是程式在執行過程中,出現的非正常的情況,終會導致JVM的非正常停止 註意:在Java等面向對象的編程語言中,異常本身是一個類,產生異常就是創建異常對象 ...
  • 一、import 1.import語句用來完成導入其他類,同一個包下的類不需要再導入 不在同一個包下需要手動導入。 2.import語法格式 import 類名; import 包名.*; //import語句需要編寫到package語句之下,class語句之上。 3.java.lang.*;不需要 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...