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
  • 1. 說明 /* Performs operations on System.String instances that contain file or directory path information. These operations are performed in a cross-pla ...
  • 視頻地址:【WebApi+Vue3從0到1搭建《許可權管理系統》系列視頻:搭建JWT系統鑒權-嗶哩嗶哩】 https://b23.tv/R6cOcDO qq群:801913255 一、在appsettings.json中設置鑒權屬性 /*jwt鑒權*/ "JwtSetting": { "Issuer" ...
  • 引言 集成測試可在包含應用支持基礎結構(如資料庫、文件系統和網路)的級別上確保應用組件功能正常。 ASP.NET Core 通過將單元測試框架與測試 Web 主機和記憶體中測試伺服器結合使用來支持集成測試。 簡介 集成測試與單元測試相比,能夠在更廣泛的級別上評估應用的組件,確認多個組件一起工作以生成預 ...
  • 在.NET Emit編程中,我們探討了運算操作指令的重要性和應用。這些指令包括各種數學運算、位操作和比較操作,能夠在動態生成的代碼中實現對數據的處理和操作。通過這些指令,開發人員可以靈活地進行算術運算、邏輯運算和比較操作,從而實現各種複雜的演算法和邏輯......本篇之後,將進入第七部分:實戰項目 ...
  • 前言 多表頭表格是一個常見的業務需求,然而WPF中卻沒有預設實現這個功能,得益於WPF強大的控制項模板設計,我們可以通過修改控制項模板的方式自己實現它。 一、需求分析 下圖為一個典型的統計表格,統計1-12月的數據。 此時我們有一個需求,需要將月份按季度劃分,以便能夠直觀地看到季度統計數據,以下為該需求 ...
  • 如何將 ASP.NET Core MVC 項目的視圖分離到另一個項目 在當下這個年代 SPA 已是主流,人們早已忘記了 MVC 以及 Razor 的故事。但是在某些場景下 SSR 還是有意想不到效果。比如某些靜態頁面,比如追求首屏載入速度的時候。最近在項目中回歸傳統效果還是不錯。 有的時候我們希望將 ...
  • System.AggregateException: 發生一個或多個錯誤。 > Microsoft.WebTools.Shared.Exceptions.WebToolsException: 生成失敗。檢查輸出視窗瞭解更多詳細信息。 內部異常堆棧跟蹤的結尾 > (內部異常 #0) Microsoft ...
  • 引言 在上一章節我們實戰了在Asp.Net Core中的項目實戰,這一章節講解一下如何測試Asp.Net Core的中間件。 TestServer 還記得我們在集成測試中提供的TestServer嗎? TestServer 是由 Microsoft.AspNetCore.TestHost 包提供的。 ...
  • 在發現結果為真的WHEN子句時,CASE表達式的真假值判斷會終止,剩餘的WHEN子句會被忽略: CASE WHEN col_1 IN ('a', 'b') THEN '第一' WHEN col_1 IN ('a') THEN '第二' ELSE '其他' END 註意: 統一各分支返回的數據類型. ...
  • 在C#編程世界中,語法的精妙之處往往體現在那些看似微小卻極具影響力的符號與結構之中。其中,“_ =” 這一組合突然出現還真不知道什麼意思。本文將深入剖析“_ =” 的含義、工作原理及其在實際編程中的廣泛應用,揭示其作為C#語法奇兵的重要角色。 一、下劃線 _:神秘的棄元符號 下劃線 _ 在C#中並非 ...